Dynamic Power Query (web)

Abdmujib

Board Regular
Joined
May 15, 2022
Messages
123
Office Version
  1. 2021
Platform
  1. Windows
I want to use power query to extract a table from the web.

In the picture below, I loaded a table from this website Premier League Form Table
there are multiple websites that has the same table structure and with the same table position, that I will love to switch to without having to go to power query, maybe by pasting the website in the cell B2 and then the table for the particular link load.


1694328888118.jpeg


so, I paste the link for spain in Cell B2 , the table loads for , spain without having to go to power query to put spain link into it.

pls kindly help. thanks
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Would something like this work for you ?
Note: A9 has the range name SelectedCountry (used in the PQ datasource)
The country table used in the validation drop down box is called tblCountry
You will still need to hit refresh on the table / query after selecting the country.

Rich (BB code):
let
    WebURL = Excel.CurrentWorkbook(){[Name="SelectedCountry"]}[Content]{0}[Column1],
    Source = Web.Page(Web.Contents(WebURL)),
    Data4 = Source{4}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Data4, [PromoteAllScalars=true]),
    #"Removed Columns" = Table.RemoveColumns(#"Promoted Headers",{"Opponents PPG", "Opponents PPG_2"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"Pts", Int64.Type}, {"GP", Int64.Type}, {"W", Int64.Type}, {"D", Int64.Type}, {"L", Int64.Type}, {"GF", Int64.Type}, {"GA", Int64.Type}, {"GD", Int64.Type}, {"", Int64.Type}})
in
    #"Changed Type"

Book1
ABCDEFGHIJKLM
1
2
3Input Select CountryColumn1_1GPWDLGFGAGDPts
4Italy1Inter Milan33008089
52AC Milan33008269
63Juventus32106157
74Lecce32106337
8Concatenated URL Used By Query Range Name SelectedCountry5Atalanta32016246
9https://www.soccerstats.com/formtable.asp?league=Italy6Napoli32016336
107Hellas Verona32014406
11Base URL8Fiorentina311167-14
12https://www.soccerstats.com/formtable.asp?league=9Bologna311134-14
1310Frosinone311134-14
1411Torino311124-24
15Country12Lazio310234-13
16England13Sassuolo310235-23
17Italy14Genoa310225-33
18Spain15Monza310225-33
1916Salernitana302135-22
2017Udinese302114-32
2118AS Roma301246-21
2219Cagliari301214-31
2320Empoli300305-50
24
Sheet1
Cell Formulas
RangeFormula
A9A9=$A$12&$A$4
Cells with Data Validation
CellAllowCriteria
A4List=INDIRECT("tblCountry")
 
Last edited:
Upvote 1
Solution
Thank you for this.

It will surely work, but the problem I might have with that is that,

1. I might decide to duplicate the sheet into multiple sheets. I am not sure if the each sheet will have it's own macro.

2. I might decide to have two or three tables on a sheet from different link and different table structures, so it might be tedious.


I would have prefer to be able to use Power Query instead if it's possible.
 
Upvote 0
I don't understand what you are asking.
There is no macro here the code I gave you is for Power Query.

You asked for:
I will love to switch to without having to go to power query, maybe by pasting the website in the cell B2 and then the table for the particular link load.
Picking a country is the switch and you don't need to go into PQ to after the switch, you just need to refresh the table/query
 
Upvote 0
Wow, I thought it was a macro code. Now I realized it is the source code for power query. Thank you so much
 
Upvote 0
Would something like this work for you ?
Note: A9 has the range name SelectedCountry (used in the PQ datasource)
The country table used in the validation drop down box is called tblCountry
You will still need to hit refresh on the table / query after selecting the country.

Rich (BB code):
let
    WebURL = Excel.CurrentWorkbook(){[Name="SelectedCountry"]}[Content]{0}[Column1],
    Source = Web.Page(Web.Contents(WebURL)),
    Data4 = Source{4}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Data4, [PromoteAllScalars=true]),
    #"Removed Columns" = Table.RemoveColumns(#"Promoted Headers",{"Opponents PPG", "Opponents PPG_2"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"Pts", Int64.Type}, {"GP", Int64.Type}, {"W", Int64.Type}, {"D", Int64.Type}, {"L", Int64.Type}, {"GF", Int64.Type}, {"GA", Int64.Type}, {"GD", Int64.Type}, {"", Int64.Type}})
in
    #"Changed Type"

Book1
ABCDEFGHIJKLM
1
2
3Input Select CountryColumn1_1GPWDLGFGAGDPts
4Italy1Inter Milan33008089
52AC Milan33008269
63Juventus32106157
74Lecce32106337
8Concatenated URL Used By Query Range Name SelectedCountry5Atalanta32016246
9https://www.soccerstats.com/formtable.asp?league=Italy6Napoli32016336
107Hellas Verona32014406
11Base URL8Fiorentina311167-14
12https://www.soccerstats.com/formtable.asp?league=9Bologna311134-14
1310Frosinone311134-14
1411Torino311124-24
15Country12Lazio310234-13
16England13Sassuolo310235-23
17Italy14Genoa310225-33
18Spain15Monza310225-33
1916Salernitana302135-22
2017Udinese302114-32
2118AS Roma301246-21
2219Cagliari301214-31
2320Empoli300305-50
24
Sheet1
Cell Formulas
RangeFormula
A9A9=$A$12&$A$4
Cells with Data Validation
CellAllowCriteria
A4List=INDIRECT("tblCountry")
I was unable to replicate how you did it. i got the rest except for imported table
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

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