Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Issue with ‘Scheduled Refresh’ for api calls

Hi all: Good day. I have developed a PBI dashboard that have 3 api calls [2 of them get called dynamically passing ‘Id’] out of several data sources . It works in Desktop env and with manual data refresh in Service env. But issues happen as Publish team is trying to implement ‘scheduled refresh’. The team told me I need to change the code for the api connection to make it happen!


#1 ‘Notice’ api url:

https://api.xyz.com/natgas/events/v1/notices?priorities=critical&limit=50&offset=0&format=json

There is a max limit of 50 notices I can pull. I changed the code in Advanced Editor as shown below:

----------------------------------------

let GetData = () =>

    let

        pageSize = 50,

        sendRequest = (page)

            let

                Response = Web.Contents(

                    "https://api.xyz.com/natgas/events/v1/notices",

                  [

                      Query = [

                            priorities=”critical”,

limit= Number.ToText(pageSize),                          

                            format="json",

                            offset= Number.ToText(page * pageSize)

                        

                        ]

                  ,

                 

                      Headers = [

                          #"Gen-Api-Key"= "………." [not showing the key here]

                          ]

                  ]

                ),

                Data = Json.Document(Response)[data]

            in

                Data,

        Loop = (page as number, AccumData as list) =>

            let

                Data = Function.InvokeAfter(()=>sendRequest(page), #duration(0,0,0,2)),        

                Result =     

                    if List.Count(Data) < pageSize

                    or page >= 50

                    then Table.FromRecords(List.Combine({AccumData, Data}))

                    else @Loop(page + 1, List.Combine({AccumData, Data}))

            in

                Result

        in

            Loop(0, {})

in

    GetData

-----------------------------

 

 

 It works to pull the data and show up in a table visual as expected!

 

Now, I am not sure how to update code for the other api, ‘Notice details’.

I  get 50 notices [‘Id’ is the unique key] from api#1, then I need to get notice details for each notice. I call #2 ‘notice details’ api sending the ‘Id’ [dynamically created]

#2 ‘Notice details’ api url->

https://api.xyz.com/natgas/events/v1/notices/details?ids={ids}&format=json

-I created a parameter NoticeId and

-then a query, ‘query_notice_details’ using New source-> Web-> Advanced-> steps using ‘NoticeId’ parameter

-I then created a Function on the query->NoticeDetailsFunction

-I copied #1 data source and removed columns except ‘Id’, invoked the above function to get  ‘details_notices’ data source

‘query_notice_details’: code From advanced editor:

let

    Source = Json.Document(Web.Contents("https://api.xyz.com/natgas/events/v1/notices/details?ids=" & NoticeId & "&format=json", [Headers=[#"Gen-Api-Key"="……"]]))

in

    Source

-----------------------------------------------------------------

 

‘details_notices’: code

------------------------------------

let

    Source = notices_api(),

    #"Removed Columns" = Table.RemoveColumns(Source,{"pipelineName", "pipelineId", "priority", "type", "status", "subject", "postDate", "effDate", "endDate", "insertDate"}),

    #"Invoked Custom Function" = Table.AddColumn(#"Removed Columns", "NoticeDetailsFunction", each NoticeDetailsFunction([id])),

    #"Expanded NoticeDetailsFunction" = Table.ExpandRecordColumn(#"Invoked Custom Function", "NoticeDetailsFunction", {"data"}, {"data"}),

    #"Expanded data" = Table.ExpandListColumn(#"Expanded NoticeDetailsFunction", "data"),

    #"Expanded data1" = Table.ExpandRecordColumn(#"Expanded data", "data", {"pipelineName", "priority", "type", "subject", "body", "postDate", "effDate", "endDate"}, {"pipelineName", "priority", "type", "subject", "body", "postDate", "effDate", "endDate"})

in

    #"Expanded data1"

-----------------------------------------------

Not sure of the code change to make for invoking the function, NoticeDetailsFunction and iterate through for each NoticeDetailsFunction([id])?

---------------------------------------------------------------------------

I have already checked the link: https://eu001-sp.shell.com/sites/AAFAA3947/SitePages/SOURCES%20INFO%20-%20REST%20API.aspx

 

Sorry if I have overwhelmed you all the info! ☹ Many thanks in advance for your patience. 

 

  • Anonymous's avatar
    Anonymous
    5 years ago

    Hi Anonymous,



    It seems like the power query not allow you direct do merge operations between different types(number and text), please try to use the following code of 'add custom column' step, I added text type convert in the formula.

     

    	#"Added Custom" = Table.AddColumn(#"Converted to Table1", "Source", each Json.Document(Web.Contents("https://api.xyz.com/",	
    	[
    		Headers=[#"Gen-Api-Key"="...."],
    		RelativePath="natgas/events/v1/notices/details",
    		Query=
    		[
    			ids=Text.From([Column1]),
    			format="json"
    		]
    	])))

     

    Regards,

    Xiaoxin Sheng

  • Anonymous's avatar
    Anonymous
    5 years ago

    Hi Anonymous and all: Good day! I found checking the data in the data base table and Api portal that for some pipes there will be same event posted for different locations [as they are impacted for all those locations for a pipeline]. That's why the multiple location rows are coming for those pipes! So, it's actually not an error/issue, but a fact! So, I am closing this thread. Thanks all for your patience. 🙂

13 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous,

    >>Now, I am not sure how to update code for the other api, ‘Notice details’.

    You can extract the id list from the 'query 1' to a new query table and convert them to the table.
    Then you can add a custom column to the new table to use the 'notice detail' function with id field values to looping each row to get detailed records instead of using query parameters to manually invoke.

    Regards,

    Xiaoxin Sheng

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Anonymous: Thanks for your response. I am not sure I can follow you clearly!  I am quite new to Power BI world. Could you please show the code to do it, would really appreciate it a lot. 

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi Anonymous,

        >>You can extract the id list from the 'query 1' to a new query table and convert them to the table.

        Select one column and right click on it to choose 'Add as new query', then this column values will be extracted as a new 'query' with 'list' format.

        Right-click on the new query list created by the above step, choose 'to table' to convert it to table format.

        >>add a custom column to the new table to use the 'notice detail' function with id field values to looping each row to get detailed records

        Add a new column to the current table and use the connector function to invoke data based on current id: (use id column name as a parameter in the connector)

            #"Added Custom" = Table.AddColumn(#"Converted to Table", "Source", each Json.Document(Web.Contents("https://api.xyz.com/natgas/events/v1/notices/details?ids=" & [NoticeId] & "&format=json", [Headers=[#"Gen-Api-Key"="……"]])))

        Notice: [NoticeId] is the id column name, you can replace it with your field name.

        Power query operators#list-of-m-operators 

        Regards,

        Xiaoxin Sheng