Introduction
Did you know Power Automate has two types of loops, but one is often forgotten ?
Power Automate can execute a set of actions repetitively using loop controls. There are two types of loops in Power Automate: For Each (Apply to Each) and Do Until. However, there are significant differences between them that developers should understand before building a workflow.
Most developers are more familiar with the For Each (Apply to Each) loop because it is simple and requires minimal effort to set up, while Do Until is often overlooked. In this article, we’ll break down when to use each loop to optimize workflow performance and efficiency.
Understanding Loops in Power Automate
1️⃣ For Each (Apply to Each) — Collection-Based Loop
executes actions for each item in a collection (array, list or dataset).
Example Use Case: Sending a reminder to multiple users
Example Scenario:
- you have a list user in sharepoints.
- you want to remind all users about their training that needs to be finished until a certain period of time.
- you read the list in power automate and iterate using For Each loop to send a reminder to each user.
2️⃣ Do Until — Condition-Based Loop
Executes actions until a specific condition is met.
Example Use Case: Fetching Paginated Data from an API Until All Records Are Retrieved.
Example Scenario:
- You are working with an API that returns paginated results, meaning only a limited number of records are retrieved per request (e.g., 100 items per page).
- you make multiple API requests, increasing the page number each time or update url with next page.
- Stop when there are no more pages left.
Key Differences: Do Until vs. For Each
When working with loops in Power Automate, choosing between Do Until and For Each (Apply to Each) depends on your workflow requirements. While both loops help automate repetitive tasks, they serve different purposes and function differently.
When to Use Each?
Developers often use For Each (Apply to Each) by default, regardless of the workflow’s actual needs, while Do Until is often overlooked. However, choosing the right loop can significantly improve efficiency and optimize automation processes. Understanding when to use each loop is essential for building effective workflows.
Use For Each when:
- You need to process a predefined collection (emails, records, files, etc.).
- The loop should run a fixed number of times based on data count.
Use Do Until when:
The number of iterations is unknown.
- You need to wait for a condition to be met (e.g., API response, approval, system update).
Best Practices
- For Each: Avoid unnecessary loops by filtering data before processing.
- Do Until: Set a timeout or max iteration limit to prevent infinite loops.
- Use logging and monitoring to track loop performance.
What’s your experience? Share your thoughts in the comments!