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
ErisedAlurem
Frequent Visitor

Power Query Recursive Function returning error API

Hello!

 

I'm trying to retrieve data from an API, and I'm having a hard time with what seems to be a simple task:

The API returns the information in the following format:

ErisedAlurem_1-1661783997843.png

 

A record with two informations: hasMore, wich defines if there are still items left to retrieve and items, a list with what I wanted to retrieve. Since I need all data (until hasMore = FALSE), my goal is to get a List with all items (not the list limited to the max defined by the API).

 

(The list gives me 100 records)

ErisedAlurem_4-1661784867404.png

 

With this goal in mind, I've tried the following:

 

 

 

Ticket_Query = (optional _after)=> 
let
  //definir quantos tickets pular
    _skip = ",startingAfter=" & _after,

  //obter fonte de dados
   Source = Json.Document(Web.Contents(urlgerada & _skip,
    [Headers=[token="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"]])),
    StepInter = Text.From(Record.Field(Source,"hasMore")),
    teste2 = Record.Field(Source,"items"),
    teste3 = Record.Field(Source,"items"){99}[id],
    Nextstep = 
      if Text.From(Record.Field(Source,"hasMore")) = "true" and List.Count(teste2)<=198 then 
      teste2 & Record.Field(@Ticket_Query(Record.Field(Source,"items"){99}[id]),"items")
     else teste2
in
    Nextstep
    in  
   Ticket_Query("")

 

 

I created a function (Ticket_query) with an optional parameter, that retrieves the id from the last record in the previous consoult and uses it as the API's parameter "startingAfter".

I retrieve this information with the following query:

teste3 = Record.Field(Source,"items"){99}[id], wich appears to work properly:

ErisedAlurem_5-1661785377617.png

(wich is correct)

Then I create the query, with the base URL and the skip _parameter.

I then created the variable teste2, that retrieves only the list of items (ignoring "hasMore").

Finally I use the following logic: if the API returns "hasMore" = TRUE, the query should return to me teste2 appended to a list similar to teste2, but now using Ticket_query with the parameter recieved from the first consoult (the recursion is using the id from the last item - equivalent to teste3, as showed before.

Record.Field(Source,"items"){99}[id]

).

When I try to get only the first page ( List.Count(teste2)<=99 ) the query works, but when I try to retrieve more data ( List.Count(teste2)<=198 or bigger) the query crashes. I imagine that I'm doing something wrong in the recursion, but I can't find what.

ErisedAlurem_3-1661784827889.png

Any input would be appretiated.

Unfortunaly the API is private, so I can't show all of the code (nor give access to the token)

1 REPLY 1
Anonymous
Not applicable

Hi @ErisedAlurem ,

Please review the following blog, hope it can help you.

Recursive Functions in Power BI / Power Query

Best Regards

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.

Top Solution Authors