Use Power Query to Load Many Web Pages
February 24, 2023 - by Bill Jelen
Problem: I need to load data from a series of web pages. The Web Query technique from Excel 2003 rarely works with modern web pages.
Strategy: Use a hack with Power Query to create a Query of Queries.
Power Query is a new tool that is a free download for Excel 2010 & Excel 2013 and is built in to the Excel 2016 Data ribbon.
In this example, you are trying to pull data from a website for Dallas for January 2, 2015. You can see there are paremeters in the URL that specify the city and the date.
Use Data, New Query, From Other Sources, From Web. Enter the URL. Use Power Query tools to isolate the information from the web page. In my case, the query returns one row with Min & Max temperature and precipitation.
Once the query for a single web page is working, you will convert this query to a function. Edit the query. use the View tab in Power Query and choose Advanced. You will see the code behind the query. Insert a new line above the word Let. Type (MyURL)=>. Scan through the remaining code and replace the actual “URL” with the word MyURL.
When you close the Advanced view, you will notice several alarming things. The list of Applied Steps will be replaced with a single step. Power Query will be asking you to enter the Parameter for MyURL. Ignore these apparent problems. Type a new name for the query such as fxWeather. (Note that the fx prefix is geek-speak for Function).
When you choose Home, Close & Load, your original returned results will disappear. Don’t be alarmed. Nothing will load, but you will see a query panel indicating there is a connection-only query called Weather.
Next, you will build a table of all of the URL’s. In my case, I created a worksheet where I could change the airport code and the start date. Formulas in the table then generate the list of URLs. Make sure to Format as Table using Ctrl+T.
Select one cell in the date table. Select Data, Get & Transform, From Table. In Power Query, choose Add Column, Add Custom Column.
Because the function is returning an array of three values, you have to click the Expand icon and choose to add all three columns. Make sure to unselect the Prefix option.
When you close & load, you will notice that Power Query is taking a few seconds to update each row. If you were returning thousands of rows, you would want to load just before going to lunch.
This article is an excerpt from Power Excel With MrExcel
Title photo by Christopher Burns on Unsplash