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

A new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.

Reply
PowerUser123
Helper II
Helper II

Adjust List.Generate Code to limit Loop

I have the below code:

    Pages = List.Generate(() => 
            CallAPI(endpoint, "1"),
            each HasContToken(Value.Metadata(_)),
            each try CallAPI(
                        endpoint, 
                        Text.Split(Text.Split(Value.Metadata(_)[next_page], "?page="){1}, "&"){0}
                    ) 
            otherwise null
        ),

 

I know the condition step is checking if certain metadata exists. That works well for my final query. But for testing purposes, I don't want it to loop until the check for metadata returns false. I just want it to loop say x amount of times and then finish. 

 

I tried changing the condition to each _ <10 but it just spins at evaluating when executing.

1 REPLY 1
OwenAuger
Super User
Super User

Hi there,

Try something along these lines:

Pages = List.Generate(
	() => [APIResult = CallAPI(endpoint, "1"), Counter = 1],
	each [Counter] < 10,
	each
	  [
		APIResult =
		  try CallAPI(
			endpoint, 
			Text.Split(Text.Split(Value.Metadata([APIResult])[next_page], "?page="){1}, "&"){0}
		  )
		  otherwise null,
		Counter = [Counter] + 1
	  ],
	each [APIResult]
  ),
  • The basic idea is to change the "initial" & "next" arguments so that they return records containing two fields, APIResult and Counter.
  • In the "condition" argument, [Counter] can then be referenced as a field.
  • In the "next" argument
    • [Counter] is also referenced in order to increment it.
    • The reference to _ is changed to [APIResult] since we just want that field.
  • You then add a 4th "selector" argument to List.Generate which selects [APIResult] as the value to actually return as an item of the list.

Hopefully the above works and I haven't made syntax errors!

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Helpful resources

Announcements
May Power BI Update Carousel

Power BI Monthly Update - May 2026

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

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

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.