Passing a variable to Power Query

127bit

New Member
Joined
Nov 9, 2021
Messages
10
Office Version
  1. 2013
Platform
  1. Windows
The following Power Query successfully downloads a table of statistics for NHL player Auston Matthews through the Get Data From Web function:

Power Query:
= Web.Page(Web.Contents("https://www.quanthockey.com/hockey-stats-nhl/en/profile.php?player=25238"))

'25238' is his primary record number. I would like to pass the primary record number as a variable. For example, change it to '23497' which would instead download the statistics of Connor McDavid, #23497. Placing "23497" into cell 'A3' and executing the following Power Query was unsuccessful:

Power Query:
= Web.Page(Web.Contents("https://www.quanthockey.com/hockey-stats-nhl/en/profile.php?player=(=a3)"))

Can you tell me what syntax/commands would perform this?
Thank-you.
 
Last edited by a moderator:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Here is a video from Mr E on how to build a parameter query

 
Upvote 0
A worksheet cell cannot be accessed directly in Power Query by using the cell address like "A3". However, it is perfectly possible to refer to a named range.

Name the A3 cell as "playerId" and then use the following code.
The first line defined with the identifier id retrieves the value of the named range "playerId" value (A3), and then you can use this id identifier to build the dynamic URL in the second line.

Power Query:
let
  id = Excel.CurrentWorkbook(){[Name="playerId"]}[Content]{0}[Column1],
  Source = Web.Page(Web.Contents("https://www.quanthockey.com/hockey-stats-nhl/en/profile.php?player=" & Text.From(id)))
in
  Source
 
Upvote 0
A worksheet cell cannot be accessed directly in Power Query by using the cell address like "A3". However, it is perfectly possible to refer to a named range.

Name the A3 cell as "playerId" and then use the following code.
The first line defined with the identifier id retrieves the value of the named range "playerId" value (A3), and then you can use this id identifier to build the dynamic URL in the second line.

Power Query:
let
  id = Excel.CurrentWorkbook(){[Name="playerId"]}[Content]{0}[Column1],
  Source = Web.Page(Web.Contents("https://www.quanthockey.com/hockey-stats-nhl/en/profile.php?player=" & Text.From(id)))
in
  Source
smozgur - thanks for your reply.
Table 0 downloaded by the query I generated contains 9 rows of 48 data columns; here is that code with most of the columns removed to save space:

Power Query:
let
    Source = Web.Page(Web.Contents("https://www.quanthockey.com/hockey-stats-nhl/en/profile.php?player=25238")),
    Data0 = Source{0}[Data],
    #"Changed Type" = Table.TransformColumnTypes(Data0,{{"Season", type text}, {"Team", type text}, ...
             * columns removed *
        ...{"Faceoffs FO%", Percentage.Type}})
in
    #"Changed Type"

I can't figure out precisely where to replace the generated code with yours, please describe further.
Thanks very much
 
Last edited by a moderator:
Upvote 0
Select A3, click on the name box, and set the name as playerId as shown below.

1708480094208.png


Then your code should start like the following including the Source step.

Power Query:
let
    id = Excel.CurrentWorkbook(){[Name="playerId"]}[Content]{0}[Column1],
    Source = Web.Page(Web.Contents("https://www.quanthockey.com/hockey-stats-nhl/en/profile.php?player=" & Text.From(id)))
    // The rest of your code
 
Upvote 0
Solution
Select A3, click on the name box, and set the name as playerId as shown below.

View attachment 107197

Then your code should start like the following including the Source step.

Power Query:
let
    id = Excel.CurrentWorkbook(){[Name="playerId"]}[Content]{0}[Column1],
    Source = Web.Page(Web.Contents("https://www.quanthockey.com/hockey-stats-nhl/en/profile.php?player=" & Text.From(id)))
    // The rest of your code
Got it. Thanks very much for your help.
 
Upvote 0

Forum statistics

Threads
1,215,096
Messages
6,123,074
Members
449,093
Latest member
ripvw

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