Power Automate: range function

Power Automate: range function

by: Manuel ⏱️ 📖 5 min read 💬 0

Power Automate provides the "range" function to generate an array of consecutive integers. It is the cleanest way to repeat an action a fixed number of times, build a numeric sequence, or replace the for-next loop that other languages take for granted.

If you have ever wished you could simply say "do this ten times" without initializing a counter variable and incrementing it by hand, the "range" function is exactly what you have been looking for.

Usage

The function takes two parameters and always returns an array of consecutive integers, each one greater than the previous by exactly one.

  1. The starting integer (where the array begins)
  2. The count of integers to produce (not the ending value)

Here is the simplest example:

range(1, 5)

will return

[1, 2, 3, 4, 5]

The second parameter is the count, not the last value. This is the single most common source of confusion with this function:

range(4, 12)

will return

[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

Twelve items starting at four, ending at fifteen.

As a for-next loop

The most popular use is feeding the result into the "Apply to each" action so that the loop runs a fixed number of times. To repeat ten times:

range(1, 10)

Inside the loop, items('Apply_to_each') gives you the current number, so you have a working counter without ever initializing a variable. That avoids the well-known performance penalty of variable locking inside loops.

Negative starting integers

The starting integer can be zero or negative. Only the count must be non-negative.

range(-3, 7)

will return

[-3, -2, -1, 0, 1, 2, 3]

This is handy when you need offsets around a reference point, for example "three days before and three days after today".

Edge Cases

A count of zero returns an empty array

The minimum allowed count is zero, and zero is valid. The result is simply an empty array:

range(10, 0)

will return

[]

This is occasionally useful when the count comes from another expression and may legitimately be zero. The flow does not error out, and the loop using the result simply does not execute.

Decimals are not allowed

Both parameters must be integers. If a starting value or count arrives as a float, for example from a calculation or from the "Parse JSON" action, the expression will fail with a template error. Wrap the value in the "int" function first:

range(int(variables('startValue')), int(variables('count')))

Same goes for strings that look like numbers. The "range" function does not auto-convert "5" to 5.

Off-by-one when reading documentation

Tutorials and screenshots online sometimes label the second parameter as "end" or "stop". It is neither. It is the number of items in the resulting array. If you want the sequence "1 to 10", write range(1, 10), not range(1, 9).

Negative counts fail

A negative second parameter throws an "InvalidTemplate" error at runtime. If your count is the result of a subtraction, guard it with the "if" function and the "max" function so it never goes below zero.

Limitations

The step is always one

There is no third parameter for step size. Every value in the resulting array is exactly one greater than the previous. To get even numbers, odd numbers, or any custom step, you have two options. Either generate the base range and multiply each item with a follow-up Compose, or skip "range" entirely and build the array with the "createArray" function.

Always ascending

The "range" function only counts up. To produce a descending sequence, generate the array first and then pass it through the "reverse" function:

reverse(range(1, 5))

will return

[5, 4, 3, 2, 1]

Integers only

You cannot generate a range of decimals like [1.0, 1.5, 2.0]. The function is strictly for integer arrays. Build decimal sequences with a "range" of integers and a follow-up transformation that divides each item.

Practical maximum count

The documented maximum count is 2,147,483,647, but you will hit timeouts and memory issues long before that. In practice, keep counts under a few thousand. For very large iteration needs, look at the "Do Until" action with proper pagination instead.

Recommendations

Here are some things to keep in mind.

Prefer it over a counter variable

If you need to loop a fixed number of times, "range" plus "Apply to each" is faster and clearer than initializing an integer variable and incrementing it. There is no race condition, no extra step, and no concurrency tax.

Pair it with addDays for date sequences

A common pattern is generating a list of dates for a report, for instance the seven days leading up to today. Combine "range" with the "addDays" function inside a "Compose" action or "Apply to each" action:

addDays(utcNow(), sub(items('Apply_to_each'), 6), 'yyyy-MM-dd')

With range(0, 7) as the input, this produces the last seven dates in order.

Always add a comment

Adding a comment will help others understand your formula. Indicate what the "range" function is doing and why, especially when the count comes from another expression rather than a literal number.

Final Thoughts

The "range" function is one of the most useful utilities in Power Automate, simple on the surface, surprisingly powerful in practice. Once you stop fighting with counter variables and start thinking in arrays, almost every "do this N times" problem becomes a one-liner. Just remember the second parameter is a count, not an end value, and you will be in great shape.

Sources

Microsoft's "range" Function Reference

Back to the Power Automate Function Reference

Photo by Bradyn Trollip on Unsplash

Comments

💬

No comments yet

Be the first to share your thoughts on this article!

Leave a Comment

All comments are reviewed for spam before being displayed 5000 left
Replying to