Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Function call fails when automatically invoking it, but works when manually invoking

I am trying to create a function that I can upload to the Power BI service to then schedule refreshes.

My current function (with changed references) is called data_labels:

(page as number, access_key as text) as table =>
let
    Source = Json.Document(Web.Contents("https://api.company.com/api/webhooks/labels/?key="& access_key & "&page=" & Number.ToText(page))),    
    results = Source[results],
    #"Converted to Table" = Table.FromList(results, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"id", "name", "color"}, {"id", "name", "color"})
in
    #"Expanded Column1"

Which works fine when called in this manner

let
     client_Key    = "xyz987"

    Source = Json.Document(Web.Contents("https://api.company.com/api/webhooks/labels/?key=" & client_Key & "&page=1")),
    RecordCount = Source[count],
    Source2 = {1..Number.RoundUp(RecordCount/10)},

    #"Converted to Table" = Table.FromList(Source2, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Renamed Columns" = Table.RenameColumns(#"Converted to Table",{{"Column1", "Page"}}),
    #"Added Custom" = Table.AddColumn(#"Renamed Columns", "Custom", each data_Labels([Page],client_Key)),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Page"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns1", "Custom", {"id", "name", "color"}, {"label.id", "label.name", "label.color"}),
in
    #"Expanded Custom"

The metadata returns a record count, which I then use to determine the number of pages required (the API sends back 10 records per page)

When loaded to the service, I can't refresh because the I get the error "Unable to refresh the model because it references an unsupported data source"

 

I changed the function to:

(page as number, access_key as text) as table =>
let
    Source = Json.Document(Web.Contents("https://api.company.com/api/webhooks/labels/?key=abc123&page=1",
                [
		Query = [key= access_key, page= Number.ToText(page)]
                ])),
    
    results = Source[results],
    #"Converted to Table" = Table.FromList(results, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"id", "name", "color"}, {"id", "name", "color"})
in
    #"Expanded Column1"

This works when I manually invoke it, with the key and a page number, but when I invoke it using the code, I get an erro ron the desktop: "Operation is not valid due to the current state of the object"

 

I cannot work out why it works manually, but not automatically. 

Any help would be greatly appreciated.

2 Replies

  • v-juanli-msft's avatar
    v-juanli-msft
    Icon for Community Support rankCommunity Support

    Hi Anonymous 

    Change the code as this article suggested to make the Power BI Service support refresh for the data source.

    Besides, these articles also may help you with the refresh problem for web data source:

    https://sqlserverbi.blog/2018/10/21/web-api-data-sources-with-power-query-and-scheduling-data-refresh-in-the-power-bi-service/

    http://blog.datainspirations.com/2018/02/17/dynamic-web-contents-and-power-bi-refresh-errors/

     

    Check if this code works on your side

       

    Source = Json.Document(Web.Contents("https://api.****.com/v1/applicants?apikey= xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        [RelativePath="page/" & Number.ToText(page),
        Query=[apikey=" xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"]]))
    

    Best Regards
    Maggie
    Community Support Team _ Maggie Li
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks Maggie!

       

      The code you suggested didn't work (the key and the page had to be the other way around), but following the link you provided, I changed the code to:

          BaseURL = "https://api.company.com/api/webhooks/labels/",
          QueryRecord = 
          [Query =
              [   key= company_key,
                  page = Number.ToText(pageNo)
              ]
          ],
          Source = Json.Document(Web.Contents(BaseURL,QueryRecord)),

      and it works!

      I think I was also using page twice, so I altered the incoming parameter name to "PageNo"

      Thanks again