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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Twitter
LinkedIn

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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