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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
Anonymous
Not applicable

List.Generate from recursive loop

Hello PBI friends,

 

I would like to get help with the following. I'm creating a loop for the first time. It's working for the first pages, but at some time it's causing a stack overflow. Now I know that a List.Generate could help with this but I don't know how to transform my code to work with List.Generate. Can anybody help me out? This is the code:

 

let
AuthKey = "Basic XXXX",
url = URL&"/api/oauth/v1/token",
body = "{ ""grant_type"": ""password"", ""username"": ""XXXX"", ""password"": ""XXXX""}",

tokenResponse = Json.Document(Web.Contents(url,[

Headers = [#"Authorization"=AuthKey ,
#"Content-Type"="application/json"],
Content = Text.ToBinary(body)]
)),
access_token = tokenResponse[access_token],
token = "Bearer " & tokenResponse[access_token],

baseuri = URL&"/api/rest/v1/products?pagination_type=search_after&search_after=&limit=100",
headers =[Headers = [#"Authorization"=token, #"Accept"="application/json"]],
initReq = Json.Document(Web.Contents(baseuri,headers)),
initData = List.Buffer(initReq[_embedded][items]),
gather = (data as list, baseuri) =>

let
newOffset = Json.Document(Web.Contents(baseuri, headers))[_links][next][href],
newUri = newOffset,
newReq = Json.Document(Web.Contents(newUri, headers)),
newdata = List.Buffer([_embedded][items]),
data = List.Combine({data, newdata}),
check = if newReq[_links][next] = null then data else @gather(data, newUri)
in check,

outputList = if initReq[_links][next] = null then initData else gather(initData, baseuri),
expand = Table.FromRecords(outputList)
in
expand

 

 

Thanks in advance.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous ,

 

There's a parctical example in the following blog, hope it can solve your problem:

Recursive Functions in Power BI / Power Query — The Power User

The recursive function

fxTranslate = (x as table, n as number, ColName as text ) as table =>

       let

           Replace = Table.ReplaceValue(x, Old{n}, New{n}, Replacer.ReplaceText,{ColName}),
Checking = if n = List.Count(Old)-1 then Replace else @fxTranslate(Replace, n+1, ColName )
in
Checking

 

 

Best Regards,

Stephen Tao

 

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

View solution in original post

1 REPLY 1
Anonymous
Not applicable

Hi @Anonymous ,

 

There's a parctical example in the following blog, hope it can solve your problem:

Recursive Functions in Power BI / Power Query — The Power User

The recursive function

fxTranslate = (x as table, n as number, ColName as text ) as table =>

       let

           Replace = Table.ReplaceValue(x, Old{n}, New{n}, Replacer.ReplaceText,{ColName}),
Checking = if n = List.Count(Old)-1 then Replace else @fxTranslate(Replace, n+1, ColName )
in
Checking

 

 

Best Regards,

Stephen Tao

 

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

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.