简体   繁体   中英

How to dynamically retrieve data from web to excel using Power Query

I have never used Power Query before so I'm not sure if what I'm asking is even possible. I want to find out a way in excel to dynamically import data from a website based on the value given in another cell, something like importhtml in google sheets. For example, here's what I created using google sheets:

If you notice,

  • the first column is an NPI number which is what I'd provide (copy and paste to the column in excel).
  • the second column is the url from where the data to is to be downloaded. The url number value is referenced to column A.
  • importHTML then imports the data and puts them in the column following URL.

So, is it possible to do something like this using Excel Power Query?

Although I used the regular url to pull down the data in html table format, I prefer to use JSON if possible. The only reason I didn't use JSON in google sheets is because I don't know how to. The URL for NPI lookup API is https://npiregistry.cms.hhs.gov/api?number= where we can enter any NPI number to lookup.

Yes, Power Query's Web.Contents function has a parameter for the URL - this can be an expression or a literal, or a concatenation of both. You can get a jump start on the syntax by using the "From Web" button (in Excel 2016+, this is (sadly) a bit buried under Data / New Query / From Other Sources) - this will generate the code including the Web.Contents function with a literal URL. Power Query will also look at the contents returned and wrap the appropriate function around Web.Contents eg Json.Document.

You could start from your existing Google Sheets file (they are also easy to consume using Get Data / From Web), and use Add Custom Column to paste in your Web.Contents code. You can insert a column reference to make the URL dynamic - using & for string concatenation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM