Dynamic Qurey

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I am no expert in this field so I simply will demonstrate how to parametrise a query. I hope this is on point here.

I suppose you are trying to use Power Query with a web address that resides in your spreadsheet. You would like to somehow alter a cell in the spreadsheet and have PQ use that new web address as the parameter. If that is so, here is my example you can try out. It completes a query to download the schedule for an NHL team conditional upon what team's name is in the Table.

Step one: I created a Table named T_SchedTeam. The first cell has a data validation for each of the NHL's 31 team names (but you can just type in things like CGY or EDM or VAN for purposes of our experiment). The adjacent cell has a concatenated formula that reports the web address. Like this:

Cell Formulas
RangeFormula
R2="https://www.hockey-reference.com/teams/"&Q2&"/2019_games.html"



Step two: create a query called QText_HR_Sched and paste this as the M-code. Close and load it to a connection only.
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="T_SchedTeam"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Team", type text}, {"HR_Address", type text}}),
    SchedTeam = Record.Field(Source{0},"HR_Address")
in
    SchedTeam

Step three: create a query called Q_SchedTable and paste this as the M-code.
Code:
let
    Source = Web.Page(Web.Contents(Text.From(QText_HR_Sched))),
    Data1 = Source{1}[Data],
    #"Removed Columns" = Table.RemoveColumns(Data1,{"GF", "GA", "2", "3", "W", "L", "OL", "Streak", "Att.", "LOG", "Notes"})
in
    #"Removed Columns"

Step four: Select 'Close and Load to...' and put it in your spreadsheet. Change CGY to EDM, refresh the queries and see how the parameter feeds into the other query.

This should show the schedule for the team you've selected in the parameter table from step one.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,923
Messages
6,175,404
Members
452,640
Latest member
steveridge

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top