Home / Power Automate Tutorial / Day 3
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
| Type | Holds | Example |
|---|---|---|
| String | Text | "Anita" |
| Integer | Whole numbers | 42 |
| Float | Decimals | 3.5 |
| Boolean | true or false | true |
| Array | A list | ["a", "b"] |
| Object | Named 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?
| Variable | Compose | |
|---|---|---|
| Can change later | Yes | No - it is fixed once it runs |
| Needs initialising | Yes | No |
| Read it with | variables('x') | outputs('Name of the action') |
| Safe inside a parallel loop | No | Yes |
| Use it for | Running totals, counters, flags | Working 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
Try it: change the first action’s name in the box above and run it.
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
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
- Initialise one variable of each type and compose them into a sentence.
- Build a running total of three fees and print it.
- Append four items to an array variable and print its length.
- Rewrite one of the flows above so it uses Compose instead of variables.
- Explain why Initialize variable cannot go inside a Condition.
