Power Apps – ForEach loops in Canvas apps using the ForAll and Sequence function

Hey all,

Before 2020 it was possible to create ForEach loops in Power Apps by using a Timer. The Timer would be used to execute an action until a certain counter value is reached. An example can be found in, for example, this blog post: https://powerusers.microsoft.com/t5/News-Announcements/How-to-do-ForEach-or-Do-while-loops-in-power-apps-canvas/ba-p/346392. This approach works but feels a little bit inconvenient. Regular programming languages have a ForEach function which can be used to iterate for a defined amount of this. When googling on how to create ForEach loops in Power Apps, most search results state you’ll have to use a Timer to create a loop. The next time you Google, I hope you’ll find this blog and see there’s also a different approach.

However, since July 2020 is it possible to create ForEach loops in Power Apps using the ForAll function in combination with Sequence! 😃 The Microsoft documentation states that ForAll/Sequence can be used as follows: “Use Sequence with the ForAll function to iterate a specific number of times.”: https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-sequence. Shane Young also has a great video about this: https://www.youtube.com/watch?v=oKJkVx2gehw.

Anyhow, a small example from the Microsoft documentation on how to create a ForEach loop is:

This code does the following:

  • Iterate 4 times.
  • Generate a random number.
  • The result is a table with 4 random numbers.

If you combine the function above with a Collect function the random numbers will be added to a collection.
By replacing the Rand() function in the sample code below with your own code you can create your own For Each loops!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.