Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 38 ยท Approvals and Control Flow

Apply to Each: Looping Over Lists

Process every item in a list with Apply to each: the current item, nested loops, concurrency control, the variable traps, and faster loop-free options like Filter array and Select.

Many connectors return lists: rows from a spreadsheet, files in a folder, attachments on an email, items in a SharePoint list. To do something for each of them, Power Automate uses the Apply to each action. It is the main loop of the product, and it is also the most common source of slow flows, so it pays to understand exactly what it does and when to skip it.

This lesson covers building a loop, reading the current item, the concurrency setting, the traps with variables, and the loop-free actions that often do the job faster. The runnable examples are stand-ins written in JavaScript.

Building a loop

Add Apply to each and choose the array to loop over in Select an output from previous steps. Inside the loop, put the actions to run for every item. The designer often creates the loop for you: when you pick a property that belongs to an item of a list (for example the Title of an item returned by "Get items"), Power Automate wraps the action in an Apply to each automatically.

Inside the loop the current element is available as dynamic content, and in expressions as:

TEXT
@item()                                   // the current element, in the innermost loop
@items('Apply_to_each')                   // the current element of a named loop
@items('Apply_to_each')?['Amount']        // one property of it

Use items('Loop_name') when loops are nested, because item() always means the innermost one. Rename the loop to something meaningful ("For each order line") so the expression stays readable.

flowchart TB
    L["Get items<br/>returns 3 rows"] --> E{"Apply to each row"}
    E --> A1["Row 1: send email"]
    E --> A2["Row 2: send email"]
    E --> A3["Row 3: send email"]
    A3 --> Z["After the loop"]

By default rows are handled one after another, in order. The actions after the loop wait until all rows are done.

JS Stand-in: a loop with a running total

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Nested loops

If each item holds its own list (an order with many lines), you nest a second Apply to each inside the first. The total number of inner actions is the product of the two sizes, so ten orders with fifty lines each means five hundred inner runs. Nesting is allowed but expensive; ask whether Select or Filter array can flatten the work first.

Concurrency: running iterations in parallel

By default an Apply to each handles items one at a time. Open the loop's Settings and turn on Concurrency control to run several iterations at once. The degree of parallelism can be set up to 50; the default when enabled is 20. For independent items that each call a slow API this can cut the run time dramatically.

Concurrency has real costs:

  • Order is lost. Iteration 7 may finish before iteration 2.
  • Shared variables break. Two iterations incrementing the same variable at the same moment can overwrite each other. Do not use Set variable, Increment variable or Append to array variable on a shared variable inside a concurrent loop; collect results with Select after the loop instead.
  • You may hit service limits. Calling a throttled API with twenty parallel requests produces 429 errors (see retry policies).

Rule of thumb: keep the default for anything that depends on order or shared state, and use concurrency for independent, read-only or idempotent work.

Limits and the "Initialize variable" rule

  • Initialize variable is only allowed at the top level of a flow, not inside a loop, condition or scope. Create variables before the loop and change them with Set, Increment or Append inside it.
  • A single Apply to each can process a large but finite number of items per run (the limit depends on the plan, up to 100,000). Connectors also return limited pages of data; turn on Pagination in a connector action's settings to get more than the default page (see flow patterns).
  • The same array may not be loopable if it is null. If the source can be empty or missing, guard with a Condition or use coalesce(value, createArray()).

Loops you can avoid

Before you build a loop, check whether one of the Data Operation actions does the work in a single step, which is faster and cheaper:

Goal Loop-free option
Keep only items that match a test Filter array
Change the shape of every item (rename, compute a field) Select
Turn a list into one text Join (or join() in an expression)
Make an HTML or CSV table Create HTML table, Create CSV table
Count matches length(body('Filter_array'))
Get the first match first(body('Filter_array'))

A typical pattern is Filter array to keep only the overdue invoices, then Apply to each over the small result, instead of looping over all invoices and testing each with a Condition. The two stand-ins below show the same work done both ways.

JS Stand-in: Filter array and Select instead of a loop

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Practical advice

  • Loop over as few items as possible: filter first.
  • Do slow work (HTTP calls, approvals) in the loop only when it must be per item; large loops with approvals create many pending requests.
  • To process in chunks, use the take() and skip() functions or the pagination settings, and consider a child flow for each batch.
  • For a loop that must stop early or when a condition changes, use Do until instead.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Inside nested loops, which expression reliably reads the current item of the OUTER loop named Apply_to_each?

    item() means the innermost loop. items('name') selects a specific loop.
  2. 2. What is the risk of turning on concurrency for a loop that increments a shared variable?

    Parallel iterations can race on a shared variable. Collect results with Select after the loop.
  3. 3. Where may Initialize variable be placed?

    Initialize variable is top-level only; use Set or Increment inside loops.
  4. 4. You only need rows where Status is Open, then a transformation of each. What is a faster design than a loop with a Condition?

    The data operations handle whole arrays in one action.

Summary

  • Apply to each runs actions once per item; items('Name') reads the current item.
  • Items run sequentially by default; concurrency (up to 50) speeds up independent work but loses order and breaks shared variables.
  • Initialize variables before the loop, never inside it.
  • Prefer Filter array, Select and Join when they can replace a loop.

Next: Do Until, Scope and Terminate