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

Day 3 — Variables, Compose and the data types

Two ways to hold a value, and knowing which one to reach for stops a lot of pain.

Initialize variable

Every variable must be initialised before it can be used, and it can only be initialised once, at the top level of the flow.

{
  "trigger": { "type": "manual", "body": {} },
  "actions": [
    { "name": "Init count",  "type": "InitializeVariable", "variable": "count",  "varType": "Integer", "value": 0 },
    { "name": "Init name",   "type": "InitializeVariable", "variable": "name",   "varType": "String",  "value": "DAI Academy" },
    { "name": "Init flag",   "type": "InitializeVariable", "variable": "found",  "varType": "Boolean", "value": false },
    { "name": "Init list",   "type": "InitializeVariable", "variable": "courses","varType": "Array",   "value": ["Excel", "SQL"] },
    { "name": "Show them", "type": "Compose",
      "input": "@{variables('name')} has @{length(variables('courses'))} courses listed" }
  ]
}

The Variables at the end table under the run history shows every variable and its final value. That table is worth watching whenever a loop is not doing what you expect.

The types

TypeHoldsExample
StringText"Anita"
IntegerWhole numbers42
FloatDecimals3.5
Booleantrue or falsetrue
ArrayA list["a", "b"]
ObjectNamed fields{"Name": "Anita"}

Changing a variable

{
  "trigger": { "type": "manual", "body": { "Fee": 12000 } },
  "actions": [
    { "name": "Init total", "type": "InitializeVariable", "variable": "total", "value": 0 },
    { "name": "Init log",   "type": "InitializeVariable", "variable": "log",   "value": [] },

    { "name": "Add the fee",   "type": "IncrementVariable", "variable": "total",
      "value": "@triggerBody()?['Fee']" },
    { "name": "Add GST",       "type": "IncrementVariable", "variable": "total",
      "value": "@mul(triggerBody()?['Fee'], 0.18)" },

    { "name": "Note it",   "type": "AppendToArrayVariable", "variable": "log", "value": "Fee added" },
    { "name": "Note GST",  "type": "AppendToArrayVariable", "variable": "log", "value": "GST added" },

    { "name": "Round it", "type": "SetVariable", "variable": "total",
      "value": "@int(variables('total'))" },

    { "name": "Result", "type": "Compose",
      "input": "Total including GST: @{variables('total')}" }
  ]
}

12,000 plus 18 per cent is 14,160. Check the run history and the variables table agree with that.

Initialize variable must be at the top Not inside a Condition, not inside an Apply to each. Power Automate refuses to save it, and the error message it gives is not obvious. Initialise everything first, then change it wherever you like.

Variable or Compose?

VariableCompose
Can change laterYesNo - it is fixed once it runs
Needs initialisingYesNo
Read it withvariables('x')outputs('Name of the action')
Safe inside a parallel loopNoYes
Use it forRunning totals, counters, flagsWorking out a value once
Reach for Compose first If a value is worked out once and never changes, a Compose says so and cannot be accidentally altered forty steps later. Variables are for things that genuinely change - a counter in a loop, a flag, a list you build up.

Reading a Compose later

outputs() takes the name of the action, exactly as written, spaces and all.

{
  "trigger": { "type": "manual", "body": { "Name": "Priya", "Course": "Tableau" } },
  "actions": [
    { "name": "Full greeting", "type": "Compose",
      "input": "Dear @{triggerBody()?['Name']}" },
    { "name": "Course line", "type": "Compose",
      "input": "You have enrolled in @{triggerBody()?['Course']}." },
    { "name": "Whole message", "type": "Compose",
      "input": "@concat(outputs('Full greeting'), ' ', outputs('Course line'))" }
  ]
}
Rename an action and every expression pointing at it breaks outputs('Full greeting') is matched on the name. Rename the action to "Greeting" and this flow fails with "No action called Full greeting". Name your actions properly the first time - and if you must rename one, search the whole flow for the old name.
Try it: change the first action’s name in the box above and run it.

The parallel loop trap

Do not use a variable as a counter inside a concurrent Apply to each Apply to each can run up to fifty iterations at once. Two of them incrementing the same variable will read the same starting value and one increment is lost. Your total comes out wrong, differently wrong on each run, which is a horrible bug to chase.
The fixes: turn concurrency off, or collect the values and total them at the end with a Select and a sum. Day 7.

Try these yourself

  1. Initialise one variable of each type and compose them into a sentence.
  2. Build a running total of three fees and print it.
  3. Append four items to an array variable and print its length.
  4. Rewrite one of the flows above so it uses Compose instead of variables.
  5. Explain why Initialize variable cannot go inside a Condition.