Skip to content

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.

An Excel table named Palaces with columns Palace, Post Code and MET Office Code, listing Buckingham Palace, Windsor Castle, Sandringham and Holyrood Palace

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.

A Met Office forecast page showing today’s summary, a six-day outlook and an hourly table with weather symbols, chance of precipitation, temperature and feels-like temperature

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.

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.

The From Web dialog with the Basic option selected and a Met Office forecast address in the URL box

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 Navigator with Table 0 selected, previewing rows for weather symbol, chance of precipitation, temperature and wind for the current hour

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.

The Power Query Editor showing Table 0, with a Time column of measure names, columns for Now, 17, 18 and 19, and label rows interleaved with value rows

  • 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.

The reshaped forecast, with one row per hour from Now to 23 and columns for time, weather symbol, chance of precipitation, temperature and feels-like temperature

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
GetWeather

When 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.

The Enter Parameter pane of a function query, with a text box, Invoke and Clear buttons, and the signature function (Postcode1 as text) as table

  • Click Close & Load to close the Power Query Editor.
  • 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.

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.

The palaces query with a new Forecast column whose cells each contain the word Table

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

The expand menu for the Forecast column, with Expand selected and every forecast column ticked, from Time to UV

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

An Excel table with the hourly forecast for all four palaces, one row per palace and hour, with columns from Palace and Post Code through to Humidity and UV

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"