SOLVED
Home

Power Query and dynamic URL

%3CLINGO-SUB%20id%3D%22lingo-sub-363111%22%20slang%3D%22en-US%22%3EPower%20Query%20and%20dynamic%20URL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-363111%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20been%20beating%20my%20head%20against%20the%20wall%20for%202%20weeks%20trying%20to%20figure%20out%20how%20to%20get%20the%20power%20query%20from%20web%20to%20work%20by%20updating%20a%20dynamic%20URL%20by%20data%20input%20from%20a%20cell.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBelow%20is%20the%20code%20I%20attempted%20to%20use%20in%20order%20to%20update%20web%20query%20based%20on%20dynamic%20URL%20from%20a%20cell.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Elet%3CBR%20%2F%3ETemp%3D%20Excel.CurrentWorkbook()%7B%5BName%3D%22URL%22%5D%7D%5BContent%5D%2C%3CBR%20%2F%3EURL%3D%20Temp%7B0%7D%5BValue%5D%2C%3CBR%20%2F%3ESource%20%3D%20Json.Document(Web.Contents(URL))%2C%3CBR%20%2F%3Edata%20%3D%20Source%5Bdata%5D%2C%3CBR%20%2F%3E%23%22Converted%20to%20Table%22%20%3D%20Table.FromList(data%2C%20Splitter.SplitByNothing()%2C%20null%2C%20null%2C%20ExtraValues.Error)%2C%3CBR%20%2F%3E%23%22Expanded%20Column1%22%20%3D%20Table.ExpandRecordColumn(%23%22Converted%20to%20Table%22%2C%20%22Column1%22%2C%20%7B%22assists%22%2C%20%22gamesPlayed%22%2C%20%22goals%22%2C%20%22playerName%22%2C%20%22playerPositionCode%22%2C%20%22points%22%7D%2C%20%7B%22assists%22%2C%20%22gamesPlayed%22%2C%20%22goals%22%2C%20%22playerName%22%2C%20%22playerPositionCode%22%2C%20%22points%22%7D)%2C%3CBR%20%2F%3E%23%22Reordered%20Columns%22%20%3D%20Table.ReorderColumns(%23%22Expanded%20Column1%22%2C%7B%22playerName%22%2C%20%22playerPositionCode%22%2C%20%22gamesPlayed%22%2C%20%22goals%22%2C%20%22assists%22%2C%20%22points%22%7D)%2C%3CBR%20%2F%3E%23%22Renamed%20Columns%22%20%3D%20Table.RenameColumns(%23%22Reordered%20Columns%22%2C%7B%7B%22playerName%22%2C%20%22Player%22%7D%2C%20%7B%22playerPositionCode%22%2C%20%22Pos%22%7D%2C%20%7B%22gamesPlayed%22%2C%20%22GP%22%7D%2C%20%7B%22goals%22%2C%20%22G%22%7D%2C%20%7B%22assists%22%2C%20%22A%22%7D%2C%20%7B%22points%22%2C%20%22Pts%22%7D%7D)%3CBR%20%2F%3Ein%3CBR%20%2F%3E%23%22Renamed%20Columns%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENote%20all%20conversions%20and%20renaming%20columns%20was%20done%20previous%20to%20attempting%20to%20change%20the%20URL.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%20would%20be%20greatly%20appreciated.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%3C%2FP%3E%3CP%3EMike%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-363111%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ENeed%20Help%20-%20Dynamic%20URL%20from%20cell%20for%20Power%20Query%20from%20Web%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-363657%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%20and%20dynamic%20URL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-363657%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Sergei%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20for%20the%20help.%26nbsp%3B%20That%20did%20it.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20again.%3C%2FP%3E%3CP%3EMike.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-363384%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%20and%20dynamic%20URL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-363384%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Mike%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESorry%2C%20but%20I%20didn't%20catch%20what%20exactly%20doesn't%20work.%20Based%20on%20your%20code%20you%20have%20in%20your%20sheet%20table%20URL%20with%20the%20column%20named%20Value%20from%20first%20row%20of%20which%20you%20take%20your%20url%20as%20parameter.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20only%20one%20cell%20which%20you%20use%20as%20parameter%20I'd%20not%20use%20table%2C%20just%20name%20that%20cell%20(let%20say%20as%20URL)%20and%20get%20parameter%20as%3C%2FP%3E%0A%3CPRE%3E...%20%20%20%20%0A%20%20%20%20GetParameter%20%3D%20Excel.CurrentWorkbook()%7B%5BName%3D%22URL%22%5D%7D%5BContent%5D%2C%0A%20%20%20%20URL%20%3D%20GetParameter%5BColumn1%5D%7B0%7D%2C%0A...%0A%3C%2FPRE%3E%0A%3CP%3Ebut%20in%20general%20that's%20only%20cosmetic%3C%2FP%3E%3C%2FLINGO-BODY%3E
msabrooks
New Contributor

Hi,

 

I have been beating my head against the wall for 2 weeks trying to figure out how to get the power query from web to work by updating a dynamic URL by data input from a cell.

 

Below is the code I attempted to use in order to update web query based on dynamic URL from a cell.

 

let
Temp= Excel.CurrentWorkbook(){[Name="URL"]}[Content],
URL= Temp{0}[Value],
Source = Json.Document(Web.Contents(URL)),
data = Source[data],
#"Converted to Table" = Table.FromList(data, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"assists", "gamesPlayed", "goals", "playerName", "playerPositionCode", "points"}, {"assists", "gamesPlayed", "goals", "playerName", "playerPositionCode", "points"}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded Column1",{"playerName", "playerPositionCode", "gamesPlayed", "goals", "assists", "points"}),
#"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"playerName", "Player"}, {"playerPositionCode", "Pos"}, {"gamesPlayed", "GP"}, {"goals", "G"}, {"assists", "A"}, {"points", "Pts"}})
in
#"Renamed Columns"

 

Note all conversions and renaming columns was done previous to attempting to change the URL.

 

Any help would be greatly appreciated.

 

Thanks,

Mike

8 Replies
Solution

Hi Mike,

 

Sorry, but I didn't catch what exactly doesn't work. Based on your code you have in your sheet table URL with the column named Value from first row of which you take your url as parameter.

 

For only one cell which you use as parameter I'd not use table, just name that cell (let say as URL) and get parameter as

...    
    GetParameter = Excel.CurrentWorkbook(){[Name="URL"]}[Content],
    URL = GetParameter[Column1]{0},
...

but in general that's only cosmetic

Hi Sergei,

 

Thanks for the help.  That did it.

 

Thanks again.

Mike.

Ok thanks
My cod

@msabrooks 

Hi, I am trying to take the problem below to a new level.

I am able to create 1 table by adding two queries as shown in example below.  How can I do this using a loop or perhaps the List.Generate function to do many queries into 1 table?

 

This code works:

Index = 1,
Source1 = Json.Document(Web.Contents("https://statsapi.web.nhl.com/api/v1/teams/" & Number.ToText(Index) & "/roster")),
roster1 = Source1[roster],
Source2 = Json.Document(Web.Contents("https://statsapi.web.nhl.com/api/v1/teams/" & Number.ToText(Index+1) & "/roster")),
roster2 = Source2[roster],
roster = roster1 & roster2,

 

Thanks in advance,

Mike

@msabrooks 

Hi Mike,

As variant you may create list of Indexes, convert it to table, add custom column to connect JSON pages for each index, after that sequentially combine/expand result. Generated script is

let
    PagesIndex = {1..7},
    #"Converted to Table" = Table.FromList(
        PagesIndex,
        Splitter.SplitByNothing(), null, null, ExtraValues.Error
    ),
    #"Renamed Columns" = Table.RenameColumns(
        #"Converted to Table",
        {{"Column1", "Index"}}
    ),
    #"Added Custom" = Table.AddColumn(
        #"Renamed Columns",
        "Custom",
        each Json.Document(
                Web.Contents("https://statsapi.web.nhl.com/api/v1/teams/" & Number.ToText([Index]) & "/roster")
        )
    ),
    #"Expanded Custom" = Table.ExpandRecordColumn(
        #"Added Custom",
        "Custom",
        {"roster"}, {"roster"}
    ),
    #"Expanded roster" = Table.ExpandListColumn(
        #"Expanded Custom",
        "roster"),
    #"Expanded roster1" = Table.ExpandRecordColumn(
        #"Expanded roster",
        "roster",
        {"person", "jerseyNumber", "position"},
        {"person", "jerseyNumber", "position"}
    ),
    #"Expanded person" = Table.ExpandRecordColumn(
        #"Expanded roster1",
        "person",
        {"id", "fullName", "link"},
        {"id", "fullName", "link"}
    ),
    #"Expanded position" = Table.ExpandRecordColumn(
        #"Expanded person",
        "position",
        {"code", "name", "type", "abbreviation"},
        {"code", "name", "type", "abbreviation"}
    )
in
    #"Expanded position"

 

 

@Sergei Baklan

 

Hi Sergei,

 

Thanks so much.  I wish I knew how to derive that on my own.  It works like a charm.

 

I modified your list of 1..7 to 1..30 and noticed there are a few bad points as there is no data for items such as 11, 27 etc.

 

Is it possible to pull in an array from excel rather than do the following:

PagesIndex = {1..10, 12..26, 28..30} 

 

I tried something like this and it did not work:

GetParameter = Excel.CurrentWorkbook(){[Name="Index"]}[Content],
Index = GetParameter[Column1]{..},

 

If you know a quick way around that it helps from the manual building of PagesIndex.

 

Thanks again.  Its much appreciated.

Mike.

 

 

Few more tries and I got it.

GetParameter = Excel.CurrentWorkbook(){[Name="Teams"]}[Content],
PagesIndex = GetParameter[ID],

 

Thanks again.

Works beautifully

Highlighted

@msabrooks 

 

Hi Mike,

 

In the middle, after adding the column with connections to JSON pages, you may insert one more step to remove records with errors which indicate that related page was not found

image.png

Please check in attached.

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
46 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies