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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

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
v-yiruan-msft
Community Support
Community Support

Hi @ErisedAlurem ,

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

Recursive Functions in Power BI / Power Query

Best Regards

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

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code FABINSIDER for a $400 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

March2025 Carousel

Fabric Community Update - March 2025

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

Top Solution Authors