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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

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
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.