DEV Community

Cover image for Why You Should Love Do Untils in Power Automate
david wyatt
david wyatt Subscriber

Posted on

Why You Should Love Do Untils in Power Automate

Do Untils are a bit of an enigma to me, when comparing them to programming they are obviously 'Do Until/While' (normally the least used), but with the limit setter they are also like 'For Condition' (e.g i=0;i<10;i++). That leaves ForEach, which is obviously a 'For Each Item'. So Do Until in theory covers 2 loop types, making it more flexible, yet I see it very rarely used.

The question is why, why are Do Untils so unloved, I suspect there and are probably 3 reasons:

The auto action is to create a ForEach (if you add an array element into an input it automatically puts that action into ForEach action). So every developers first interaction with a loop will most likely be a ForEach, setting that behaviour.

ForEachs work, 99 times out of 100 they do what we need well.

People don't know the benefits of a 'Do Until'.

So what are the benefits:

Limit Loop Interactions

Yes you can limit the number of returns to set a size of an array, indirectly setting the loop size But what happens if its 2 arrays stacked together or if its the last x items. With a Do Until you can set the Count, which is the maximum iterations (like the for i=0;i<100;i++).

Image description

Time Box

This is more of a niche scenario but you can set the timeout of the Do Until, so like the count, you can set the loop to end after x mins/hours. You can also use the condition by checking utcNow against a future timestamp.

Image description

Rolling Total

Filters & queries are great for finding a set value, but what happens if you need to find the first x rows that add up to a set value. This is where a Do Until is perfect again.

Image description

Offset Lists

For Each only iterates in 1s, where Do Until can iterate in any amount (e.g. what if you wanted only even numbers, then you could use an array position and double increment)

Image description

And remember, a Do Until can be used for every item, just like a For Each (So a Do Until can do everything a For Each can do).


My favourite example (and my most used) of a Do Until is with API limits. If your connector has return limits without built in pagination a Do Until can help. We can use to create our own pagination, with the Get call inside a Do Until. The Get has a filter set to return IDs/count greater than a variable (defaulted to 0). The results are appended to an array variable, and the Do Until condition checks if the return limit was returned, if it was then it sets the ID/count variable to greater then the last ID)count and repeats.
SharePoint has built in pagination so wouldnt need this solution but I used it for demonstration purposes

Image description

Append GetItems to array
union(variables('aItems'),outputs('Get_items')?['body/value'])
Get last ID
last(outputs('Get_items')?['body/value'])?['ID']


I still find it hard not to just put an array value as an input and let Power Automate auto create the loop (I'm far to lazy), which I said is always a For Each. But I am using DoUntils more and more, and I'm finding an added benefit in planning my loops, not just auto creating them, as you often find better, simpler, more efficient solutions when planned.

In most cases For Each will do, but that doesn't mean it's the best solution.

Top comments (0)