Forum Discussion
Daily changing web query and how to handle in power query
- Jun 09, 2017
Wyn, you usually explain much better, I'll try to start. I loaded your file and received query firewall alert for using date query. Modified a bit. Anyway
1) Within the excel is the cell with date which we would like to use as parameter. At any place, the only point we name this cell as Date
https://www.computerhope.com/issues/ch000704.htm
2) After that create the query which returns this date. Stay on above cell and in Ribbon Data->From Table/Range. The only one additional step is convert result to text type.
I converte that query to function, thus finally
let fnDate = () => let // Here we take the content of the cell named "Date" in current workbook Source = Excel.CurrentWorkbook(){[Name="Date"]}[Content] , // Result is returned as the table with one row, convert it to text type ChangeTypeToText = Table.TransformColumnTypes(Source,{{"Column1", type text}}), TakeDataValue = ChangeTypeToText{0}[Column1] in TakeDataValue in fnDate
3) After that starting point in Ribbon Data->From Web, enter site URL, select desirable table from menu and after that edit. Actually remove from text with URL last part with date and add instead date received on previous step.
I changed a bit the query for better parametrization:
let // here we define parameters for our query // all constants except the date returned by fnDate() pUrl = "http://www.fangraphs.com/leaders.aspx", pFirstSeason = "2016", pLastSeason = "2016", pType = "8", pNothing = "0", pDate = fnDate(), // This function reads content of pUrl page with parameters in Query Source = Web.Page(Web.Contents(pUrl, [Query = [ #"pos" = "all", #"stats" = "pit", #"lg" = "all", #"qual" = pNothing, #"type" = pType, #"season" = pFirstSeason, #"month" = pNothing, #"season1" = pLastSeason, #"ind" = pNothing, #"team" = pNothing, #"rost" = pNothing, #"age" = pNothing, #"players" = "p" & pDate ] ] )), // from list of tables select our one by ClassName SelectMasterTable = Table.SelectRows(Source, each ([ClassName] = "rgMasterTable")), // Pickup data from selected table TakeIt = SelectMasterTable{0}[Data], // No need in column with headers NoNeed = Table.RemoveColumns(TakeIt,{"Header"}) in NoNeed
Then Close & Load the query to desirable place in Excel sheet (see Sheet2 in attached file)
Wyn, you usually explain much better, I'll try to start. I loaded your file and received query firewall alert for using date query. Modified a bit. Anyway
1) Within the excel is the cell with date which we would like to use as parameter. At any place, the only point we name this cell as Date
https://www.computerhope.com/issues/ch000704.htm
2) After that create the query which returns this date. Stay on above cell and in Ribbon Data->From Table/Range. The only one additional step is convert result to text type.
I converte that query to function, thus finally
let fnDate = () => let // Here we take the content of the cell named "Date" in current workbook Source = Excel.CurrentWorkbook(){[Name="Date"]}[Content] , // Result is returned as the table with one row, convert it to text type ChangeTypeToText = Table.TransformColumnTypes(Source,{{"Column1", type text}}), TakeDataValue = ChangeTypeToText{0}[Column1] in TakeDataValue in fnDate
3) After that starting point in Ribbon Data->From Web, enter site URL, select desirable table from menu and after that edit. Actually remove from text with URL last part with date and add instead date received on previous step.
I changed a bit the query for better parametrization:
let // here we define parameters for our query // all constants except the date returned by fnDate() pUrl = "http://www.fangraphs.com/leaders.aspx", pFirstSeason = "2016", pLastSeason = "2016", pType = "8", pNothing = "0", pDate = fnDate(), // This function reads content of pUrl page with parameters in Query Source = Web.Page(Web.Contents(pUrl, [Query = [ #"pos" = "all", #"stats" = "pit", #"lg" = "all", #"qual" = pNothing, #"type" = pType, #"season" = pFirstSeason, #"month" = pNothing, #"season1" = pLastSeason, #"ind" = pNothing, #"team" = pNothing, #"rost" = pNothing, #"age" = pNothing, #"players" = "p" & pDate ] ] )), // from list of tables select our one by ClassName SelectMasterTable = Table.SelectRows(Source, each ([ClassName] = "rgMasterTable")), // Pickup data from selected table TakeIt = SelectMasterTable{0}[Data], // No need in column with headers NoNeed = Table.RemoveColumns(TakeIt,{"Header"}) in NoNeed
Then Close & Load the query to desirable place in Excel sheet (see Sheet2 in attached file)
- Wyn HopkinsJun 09, 2017MVPHi Sergei, really like your breakdown of the query into it's elements.
I'm not a fan of using custom functions in this scenario ( when grabbing a single cell named parameter) when the steps are quick and easy to do it without one. Especially when sharing with non-experienced folk...
Click on Named Cell
Data > From Table
Remove the automatic Promoted Headers and Change Type steps
Change Type to Text
Right Click in Text and Drill Down
Done- Stephen SmorolFeb 28, 2018Copper Contributor
I saw this question in here, and I am still trying to figure it out...
I also have a question on how you can do a date range? Some of the queries I want are for 14 days, 30 days, etc.
Here are a couple examples, funny, they are from the same website as the original question.
It seems some of the 14 day ones use actual dates and some may be based on the current date (9/26/17) in this example.
Thanks in advance if you can help!
https://www.fangraphs.com/leaderssplits.aspx?splitArr=1&strgroup=season&statgroup=2&startDate=2018-2-13&endDate=2018-2-26&filter=&position=B&statType=team&autoPt=false&players=&sort=15,1&pg=0 https://www.fangraphs.com/leaders.aspx?pos=all&stats=pit&lg=all&qual=0&type=1&season=2017&month=2&season1=2017&ind=0&team=0&rost=0&age=0&filter=&players=p2017-09-26 https://www.fangraphs.com/leaders.aspx?pos=all&stats=pit&lg=all&qual=0&type=1&season=2017&month=0&season1=2017&ind=0&team=0&rost=0&age=0&filter=&players=p2017-09-26 - SergeiBaklanJun 09, 2017Diamond Contributor
Hi Wyn,
Agree, better to avoid custom functions with beginners, didn't think about that. The only reason why i reverse the query into the function is the error i received with your query
Instead of sorting this out i shifted on the function.
- Wyn HopkinsJun 09, 2017MVPI have my Privacy Settings set to ignore to avoid those sorts of annoyances. I'm surprised that a custom function gets around the issue - interesting.
- John SmithJun 09, 2017Copper Contributor
SergeiBaklan wrote:Wyn, you usually explain much better, I'll try to start. I loaded your file and received query firewall alert for using date query. Modified a bit. Anyway
1) Within the excel is the cell with date which we would like to use as parameter. At any place, the only point we name this cell as Date
https://www.computerhope.com/issues/ch000704.htm
2) After that create the query which returns this date. Stay on above cell and in Ribbon Data->From Table/Range. The only one additional step is convert result to text type.
I converte that query to function, thus finally
let fnDate = () => let // Here we take the content of the cell named "Date" in current workbook Source = Excel.CurrentWorkbook(){[Name="Date"]}[Content] , // Result is returned as the table with one row, convert it to text type ChangeTypeToText = Table.TransformColumnTypes(Source,{{"Column1", type text}}), TakeDataValue = ChangeTypeToText{0}[Column1] in TakeDataValue in fnDate
3) After that starting point in Ribbon Data->From Web, enter site URL, select desirable table from menu and after that edit. Actually remove from text with URL last part with date and add instead date received on previous step.
I changed a bit the query for better parametrization:
let // here we define parameters for our query // all constants except the date returned by fnDate() pUrl = "http://www.fangraphs.com/leaders.aspx", pFirstSeason = "2016", pLastSeason = "2016", pType = "8", pNothing = "0", pDate = fnDate(), // This function reads content of pUrl page with parameters in Query Source = Web.Page(Web.Contents(pUrl, [Query = [ #"pos" = "all", #"stats" = "pit", #"lg" = "all", #"qual" = pNothing, #"type" = pType, #"season" = pFirstSeason, #"month" = pNothing, #"season1" = pLastSeason, #"ind" = pNothing, #"team" = pNothing, #"rost" = pNothing, #"age" = pNothing, #"players" = "p" & pDate ] ] )), // from list of tables select our one by ClassName SelectMasterTable = Table.SelectRows(Source, each ([ClassName] = "rgMasterTable")), // Pickup data from selected table TakeIt = SelectMasterTable{0}[Data], // No need in column with headers NoNeed = Table.RemoveColumns(TakeIt,{"Header"}) in NoNeed
Then Close & Load the query to desirable place in Excel sheet (see Sheet2 in attached file)
Sergei,
You really helped me out i was able to figure how to load more columns. I have a stupid question how did you get the table too display on sheet2? Thanks again for the help.
Thanks
John
- SergeiBaklanJun 09, 2017Diamond Contributor
Hi John,
1) When you create new query and save, it by default create the table in new Excel worksheet. You may select another option from Query Editor menu
and generate the table in any suitable place or don't load the table at all keeping the query as connection only one:
2) For the existing query right click on its name in list of shown queries within the Excel, select Only create connection (existing table will disappear) and when repeat above to load the query into the new table
3) Modifying the query better to keep initial one until you are sure what modified one is working. One of the options from the same above menu Duplicate existing query, make all changes in duplicated one and after that copy/paste M script to initial query; or delete initial and rename new one.
As usual above is only one of the ways.