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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
acoyne
Frequent Visitor

Looping through a list generate function to get necessary API status to return its contents

Hi,

 

I am using query editor to make an API call to one of our internal API services. The script I have developed consists of 3 api calls, the 1st is to obtain a token id which is straightforward, 2nd api call is a post request to pass it some parameters by end users. A task id is retrievable from this 2nd call. This task id is used then as part of the 3rd and final api call (get request) to return the results. The problem I have is the 2nd api call execution time can vary so if I call the 3rd api call too early, the results will not be returned. The task status can be one of 3 statuses - null, in-progress and finished. I can only get the results once the status is in "finished" state. 

 

The variable "url_main" (seen below) is dyamanic and contains the task id generated from the 2nd api call. I need some type of loop on the 3rd api call (get request) to keep calling the api and only return the results once the task status changes to finished. 

 

Here is a sample snippet of the 3rd api call where I am trying to apply a list generate and web.contents together. The below keeps returning an emtly list where I want to keep looping until the task status turns to finished. 

 

 

 

 

 

 

 

 

 

 

let

url_main = "https://xxxxxxxx"
mytok = "xxxxxxxx"

// funciton to make api call
fetchData = () => // No argumens to pass into function
        let
            source = Json.Document(Web.Contents(url_main, [Headers = [//IsRetry = "true", 
                                                                clearCahceTry = Text.From(Number.Random()), 
                                                                            #"Authorization"= "Bearer " & mytok]]))
        in
            source,


// loop 
let    
mysource = List.Generate(() => 
        [counter = 1, Result = fetchData()],
        //each [Result][taskStatus] = Text.ToList("FINISHED"), // condition if true 
        each [Result][taskStatus] = "FINISHED", // condition if true 
        //each not List.IsEmpty( [Result] ), // condition if true 
        each [
              counter = [counter] + 1, 
              Result = try fetchData() otherwise null],
        each [Result] // selects part of the result to return if condition is met
    )
in
    mysource

 

 

 

 

 

 

 

 

 

 

Any help would be great. 

 

Thanks in advance

Alan

 

1 REPLY 1
lbendlin
Super User
Super User

What should happen if it never finishes?

 

Read about Function.InvokeAfter()

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.