Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
kridue
New Member

Building a looping API Post call based on query results

Hi!

 

I am using two POST api calls to get data. 

The first call, "Start ExecuteAsyncSingleQuery" starts my "session" and returns my first batch of data. It also tells me wether or not there are more batches of data availible, through a field "HasRemainingRows" which is either true or false, and lastly it gives me a query session ID that I use to query the rest of my data.
Start ExecuteAsyncSingleQuery:

 

 

 

 

let
url = "redactedurl",

authorization = "Basic redacted username:pw",

body = "{
    ""Query"": {
        ""CategoryNo"": 43,
        ""Conditions"": [],
        ""Mode"": 0,
        ""RowBlockSize"": 100
    }
}",



Source = Json.Document(Web.Contents(url, [Headers=[Method="POST", #"Content-Type"="application/json", Authorization=authorization, TenantName="redactedTenant"], Content=Text.ToBinary(body)])),
    #"Converted to Table" = Record.ToTable(Source)
in
    #"Converted to Table"

 

 

 

 


This returns

kridue_0-1669098950371.png

 

The next query "Get Next Rows" does exactly this: based on my QueryID it returns the next batch of data, in addition to info on wether there are remaining rows still.
Get Next Rows:

 

 

 

let
url = "Redacted url2",

authorization = "Basic redacted username:pw",

body = "{
    ""QueryID"""&":"&Number.ToText(QueryID)&  "}" ,


Source = Json.Document(Web.Contents(url, [Headers=[Method="POST", #"Content-Type"="application/json", Authorization=authorization, TenantName="RedactedTenant"], Content=Text.ToBinary(body)])),
    #"Converted to Table" = Record.ToTable(Source)
in
    #"Converted to Table"

 

 

 


Query result:

kridue_1-1669099310075.png

The result looks the same wether HasRemainingRows is TRUE or FALSE, but if there are no more rows the "QueryResult" record will be blank.


My question is, how should I go about building a function that keeps running the second query, "Get Next Rows" until it returns false?

Any suggestions is greatly appreciated.




2 REPLIES 2
v-jingzhang
Community Support
Community Support

Hi @kridue 

 

When you want to loop the second query, which part in the POST request will be changed in every loop? I saw in the second query, it has QueryId in the body. I think it is from the response of the first query. But in the response of the second query, it doesn't have a QueryId. We can know whether or not to continue looping according to that HasRemainingRows is true or false. But some part in the url or body should also be updated to send a different POST request. Can you clarify which part needs to be updated in a new loop? 

vjingzhang_0-1669182339996.png

 

Best Regards,
Community Support Team _ Jing

Hi, and thank you for your reply!

You are correct in that I store the Query ID from the first request as the underlined "QueryID" and use this for the next request ("Get Next Rows").
However, I think the API itself (or the function the API calls upon) keeps track of what data I have recieved and what I haven't so that the QueryID is the only thing I send when I use the "Get Next Rows" request. So to answer your question the request for the looping part should be identical, and using the "original" QueryID and only stop when the request returns "HasRemainingRows=False"

 

Best,

K

Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

Top Solution Authors
Top Kudoed Authors