EDIT: Added range method. Thanks Natraj Yegnaraman for fixing my brain fart!
When makers need a loop with a counter in Power Automate, say from 1 to 20 (or 0 to 19 for all of you developers out there), the immediate reaction I’ve observed is to reach for a gun variable:
You don’t have to because there are at least two methods that avoid those variables and associated extra actions (that you pay for!).
Method 1: createArray
Use createArray function as the input for the Apply to each loop. This method works for small number of iterations (upper limit is how many comma-separated numbers you can type without getting carpal tunnel syndrome). Big advantage is ability to use custom values for iterations. For example, if you want to loop over the number of days in all months, you can use createArray(31,28,31,30,31,30,31,31,30,31,30,31)
. Using other data types is also possible, e.g. createArray('One', 'Two', 'Three')
or createArray('🤘', '📃', '✂️')
. Get the current index value as item()
or items('Loop_name')
(latter works for nested loops as well).
Method 2: range
If all you want to generate are the sequential integer numbers, easiest way is to use range
instead of createArray. For example, to loop from 1 to 20 enter range(1, 20)
. Use item()
or items('Loop_name')
to get the current index.
Method 3: endless loop with the limit
Expand Change limits and set the Count to whatever is needed. Brevity is the winner here. The downside is pro-style zero-based indexing. Get the current index value as iterationIndexes('Loop_name')
(yes, it’s now documented). In fact, you case use iterationIndexes
at any time in Do Until loops giving you a zero-based sequential iteration index.