🚀 New batches open: Advanced Excel • Power BI • SQL • AI for Analytics — Book a free demo

Day 6 — Apply to each - doing it to every row

The moment your trigger hands you a list instead of a single thing, you need this.

The loop

Apply to each takes an array and runs its actions once per item. Inside it, item() is the current one.

{
  "trigger": { "type": "manual", "body": {} },
  "actions": [
    { "name": "Orders", "type": "Compose", "input": [
        { "Product": "Laptop",   "Units": 3,  "Price": 52000 },
        { "Product": "Monitor",  "Units": 7,  "Price": 9500  },
        { "Product": "Keyboard", "Units": 12, "Price": 1200  }
      ] },
    {
      "name": "Each order",
      "type": "ApplyToEach",
      "from": "@outputs('Orders')",
      "actions": [
        { "name": "Line", "type": "Compose",
          "input": "@{item()?['Units']} x @{item()?['Product']} = @{mul(item()?['Units'], item()?['Price'])}" }
      ]
    }
  ]
}

The run history shows Iteration 1 of 3, 2 of 3 and 3 of 3, each with its own Compose underneath. That layout is exactly how the real run history looks when you expand a loop.

Building a total

Initialise before the loop, increment inside it, read it after.

{
  "trigger": { "type": "manual", "body": {} },
  "actions": [
    { "name": "Init total", "type": "InitializeVariable", "variable": "total", "value": 0 },
    { "name": "Init count", "type": "InitializeVariable", "variable": "count", "value": 0 },
    { "name": "Orders", "type": "Compose", "input": [
        { "Product": "Laptop",   "Units": 3,  "Price": 52000 },
        { "Product": "Monitor",  "Units": 7,  "Price": 9500  },
        { "Product": "Keyboard", "Units": 12, "Price": 1200  },
        { "Product": "Mouse",    "Units": 20, "Price": 650   }
      ] },
    {
      "name": "Each order",
      "type": "ApplyToEach",
      "from": "@outputs('Orders')",
      "actions": [
        { "name": "Add value", "type": "IncrementVariable", "variable": "total",
          "value": "@mul(item()?['Units'], item()?['Price'])" },
        { "name": "Count it", "type": "IncrementVariable", "variable": "count", "value": 1 }
      ]
    },
    { "name": "Summary", "type": "Compose",
      "input": "@{variables('count')} orders worth @{variables('total')}" }
  ]
}

156000 + 66500 + 14400 + 13000 = 249,900 across 4 orders. Check the variables table agrees.

A decision inside the loop

{
  "trigger": { "type": "manual", "body": {} },
  "actions": [
    { "name": "Init big", "type": "InitializeVariable", "variable": "bigOnes", "value": [] },
    { "name": "Orders", "type": "Compose", "input": [
        { "Product": "Laptop",   "Units": 3,  "Price": 52000 },
        { "Product": "Monitor",  "Units": 7,  "Price": 9500  },
        { "Product": "Keyboard", "Units": 12, "Price": 1200  },
        { "Product": "Mouse",    "Units": 20, "Price": 650   }
      ] },
    {
      "name": "Each order",
      "type": "ApplyToEach",
      "from": "@outputs('Orders')",
      "actions": [
        {
          "name": "Over 50k?",
          "type": "Condition",
          "expression": "@greater(mul(item()?['Units'], item()?['Price']), 50000)",
          "yes": [
            { "name": "Keep it", "type": "AppendToArrayVariable", "variable": "bigOnes",
              "value": "@item()?['Product']" }
          ],
          "no": []
        }
      ]
    },
    { "name": "Result", "type": "Compose",
      "input": "Big orders: @{join(variables('bigOnes'), ', ')}" }
  ]
}

Only Laptop (156,000) and Monitor (66,500) clear 50,000. Keyboard is 14,400 and Mouse is 13,000.

Nested loops and items()

Inside a loop within a loop, item() means the inner one. To reach the outer one you use items('Name of the outer loop').

{
  "trigger": { "type": "manual", "body": {} },
  "actions": [
    { "name": "Batches", "type": "Compose", "input": [
        { "Batch": "Excel-A", "Students": ["Anita", "Rahul"] },
        { "Batch": "SQL-B",   "Students": ["Priya", "Meera", "Vikram"] }
      ] },
    {
      "name": "Each batch",
      "type": "ApplyToEach",
      "from": "@outputs('Batches')",
      "actions": [
        {
          "name": "Each student",
          "type": "ApplyToEach",
          "from": "@item()?['Students']",
          "actions": [
            { "name": "Line", "type": "Compose",
              "input": "@{item()} is in @{items('Each batch')?['Batch']}" }
          ]
        }
      ]
    }
  ]
}
item() always means the innermost loop This is the number one confusion with nested loops. Inside "Each student", item() is the student name. The batch is reached with items('Each batch') - naming the loop you want. Name your loops something meaningful and this gets far easier to read.

The concurrency trap

Apply to each runs in parallel by default Up to twenty iterations at once, and up to fifty if you turn it up. That is a big speed win and a correctness disaster for counters, because two iterations read the same starting value and one increment is lost.
Symptoms: the total is wrong, and differently wrong each time you run it.
Fixes: turn concurrency off in the action settings when you are using a variable as a counter - or better, do not use a counter at all. Day 7 shows how Select and Filter array get the same answers with no loop and no variable.
This playground runs iterations one at a time, so your totals here are always right. Real Power Automate is where this bites.

Do you need the loop at all?

Power Automate adds an Apply to each automatically the moment you use a field from a list, and beginners end up with loops they never wanted.

You wantBetter than a loop
Some of the itemsFilter array
One field from every itemSelect
How manylength()
The first or last onefirst(), last()
Is X in the listcontains()

All of those run as a single action instead of one per row. On 500 rows that is the difference between two seconds and four minutes. Tomorrow is entirely about them.

Try these yourself

  1. Loop an array of five numbers and total them.
  2. Loop the orders and collect the product names of everything under 20,000.
  3. Build a nested loop over batches and students, printing "student - batch".
  4. Count how many orders are from a given region using a loop and a condition.
  5. Explain why a counter variable inside a parallel Apply to each gives the wrong answer.