A real loop function in Power BI

Today, I will go through how to create a loop function using List.Generate() and a Custom Function. I will show how to loop through pages in an API call. This would normally be solved using a for loop in e.g. Python.

There is no function in Power Query that resembles For loop, so I had to look for an alternative. After doing a thorough research I learned that some developers use List.Generate() to create lists with numbers. This is later used in List.Transform(), which calls a function for each number. Pretty similar to a loop, isn’t it.

As an example, I will be using API with information about beer recipies. I know that it has 10 pages. The page number could’ve also been a result of another API call.

Getting the data from the API is fairly easy:

But how would I tackle the problem of pages? First, I have to create a custom function. It will return a table with information about recipies for a specific page. I just edit the code above:

Now that it is ready, I can create a list with numbers from 1 to 10, a call the function for each.

I could have used List.Numbers(1,10) instead and it would have worked fine too. Let me know what you think!

Thanks

One thought on “A real loop function in Power BI

  1. Hi michal, i seem to get an error “Too many requests” when i try this example. is there another example i could try this looping with?

    thnanks

Leave a Reply

Your email address will not be published. Required fields are marked *