Web weather data
This page builds a single table of weather forecasts for several places at once. It turns one web query into a function, and then calls that function for every row of an Excel table.
The example starts from an Excel table of four royal palaces in the United Kingdom, with each palace’s post code and its Met Office location code.

The Met Office website gives a forecast for any UK post code. Typing a post code such as SW1A 1AA into the forecast search at https://www.metoffice.gov.uk/weather/forecast/ opens a page like the one below.

The address of that page ends with the location’s Met Office code, as in https://www.metoffice.gov.uk/weather/forecast/gcpvhbpyk. Because only the code changes from one location to the next, a single query can fetch the forecast for any location once the code is made a parameter.
1. Query one forecast page
Section titled “1. Query one forecast page”The work needs two queries. The first fetches the forecast from one page, and it later becomes a function.
- On Excel’s Data tab, click From Web, and enter the address of one forecast page, such as
https://www.metoffice.gov.uk/weather/forecast/gcq0k18t5, in the From Web dialog.

When you click OK, the Navigator lists every table that Power Query found on the page.
- Select Table 0, and then click Transform Data to open the Power Query Editor.

The Power Query Editor shows the forecast for the rest of the day. Each hour is a column, and each measure takes two rows, because a row holding the label alternates with a row holding the value.

- Reshape the table so that each hour is a row and each measure is a column. The steps in the code below do this by demoting the headers, transposing the table, removing the label columns and promoting the first row back to headers.

2. Turn the query into a function
Section titled “2. Turn the query into a function”The query now returns the forecast for one fixed location. To make it work for any location, it is turned into a function that takes the Met Office code as its input.
- On the View tab of the Power Query Editor, click Advanced Editor. The M code for the query should look something like this:
let Source = Web.Page(Web.Contents("https://www.metoffice.gov.uk/weather/forecast/gcq0k18t5")), Data0 = Source{0}[Data], #"Demoted Headers" = Table.DemoteHeaders(Data0), #"Transposed Table" = Table.Transpose(#"Demoted Headers"), #"Removed Columns" = Table.RemoveColumns(#"Transposed Table",{"Column3", "Column5", "Column7", "Column9", "Column11", "Column13", "Column15", "Column17"}), #"Promoted Headers" = Table.PromoteHeaders(#"Removed Columns", [PromoteAllScalars=true]), #"Changed Type2" = Table.TransformColumnTypes(#"Promoted Headers",{{"Time", type text}, {"Weather symbol", type text}, {"Chance of precipitation", type text}, {"Temperature (°C)", type text}, {"Feels like temperature (°C)", type text}, {"Wind direction and speed (miles per hour)", type text}, {"Wind gust (miles per hour)", Int64.Type}, {"Visibility", type text}, {"Humidity", Percentage.Type}, {"UV", type text}})in #"Changed Type2"- Edit the code as below. The original steps are wrapped in a function named
GetWeather, which takes a text parameter, and the fixed code at the end of the address is replaced by that parameter.
let GetWeather = (MetCode as text) as table =>let Source = Web.Page(Web.Contents("https://www.metoffice.gov.uk/weather/forecast/" & MetCode)), Data0 = Source{0}[Data], #"Demoted Headers" = Table.DemoteHeaders(Data0), #"Transposed Table" = Table.Transpose(#"Demoted Headers"), #"Removed Columns" = Table.RemoveColumns(#"Transposed Table",{"Column3", "Column5", "Column7", "Column9", "Column11", "Column13", "Column15", "Column17"}), #"Promoted Headers" = Table.PromoteHeaders(#"Removed Columns", [PromoteAllScalars=true]), #"Changed Type2" = Table.TransformColumnTypes(#"Promoted Headers",{{"Time", type text}, {"Weather symbol", type text}, {"Chance of precipitation", type text}, {"Temperature (°C)", type text}, {"Feels like temperature (°C)", type text}, {"Wind direction and speed (miles per hour)", type text}, {"Wind gust (miles per hour)", Int64.Type}, {"Visibility", type text}, {"Humidity", Percentage.Type}, {"UV", type text}})in #"Changed Type2"in GetWeatherWhen you close the Advanced Editor, the Power Query Editor no longer shows a table. It shows the function’s signature, with a box for entering the parameter and an Invoke button. Rename the query fnGetWeather.

- Click Close & Load to close the Power Query Editor.
3. Query the table of palaces
Section titled “3. Query the table of palaces”- Click inside the table of palaces.
- On Excel’s Data tab, click From Table/Range.
The Power Query Editor opens with a new query that shows the data from the table.
4. Call the function for each palace
Section titled “4. Call the function for each palace”The second query now calls the function once for each row of the table.
- On the Add Column tab, click Custom Column.
- Name the new column
Forecast, and enter this formula:
= fnGetWeather([MET Office Code])- Click OK. Each row of the new Forecast column holds a table, which is the forecast for that palace.

- Click the expand icon in the header of the Forecast column.
- Leave every column ticked in the list that opens, and click OK.

The query now has one row for each hour of each palace’s forecast. Loaded to Excel, it looks something like this:

5. The M code
Section titled “5. The M code”The full code for the fnGetWeather function is in step 2. The code below is the query that calls the function for each palace in the Excel table.
let Source = Excel.CurrentWorkbook(){[Name="Palaces"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{{"Palace", type text}, {"Post Code", type text}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Forecast", each fnGetWeather([MET Office Code])), #"Expanded Forecast" = Table.ExpandTableColumn(#"Added Custom", "Forecast", {"Time", "Weather symbol", "Chance of precipitation", "Temperature (°C)", "Feels like temperature (°C)", "Wind direction and speed (miles per hour)", "Wind gust (miles per hour)", "Visibility", "Humidity", "UV"}, {"Time", "Weather symbol", "Chance of precipitation", "Temperature (°C)", "Feels like temperature (°C)", "Wind direction and speed (miles per hour)", "Wind gust (miles per hour)", "Visibility", "Humidity", "UV"})in #"Expanded Forecast"