“I’ve got so many variables in my Power Automate flow, so many ‘Initialize variable’ actions, can I somehow reduce their number?”
When you need a variable in a Power Automate flow, you must initialise it with an action – one variable, one action. An action that must be at the top level, meaning you can’t initialise it only when you need it. So you initialise it right away, at the beginning of the flow. A smaller or bigger series of ‘Initialize variable’ actions to prepare all of them.
While it’s not that bad, I don’t like having many variables in a flow. It’s annoying to initialise them, it makes the flow longer than necessary, they make it harder to copy/paste parts of the flow, and they create a mess among dynamic content as they stay always at the top.
This article will be about reducing their amount in a flow.
Using existing dynamic contents?
Since every Power Automate action gives you an output, there’s often no need to even have a variable. If all your variable does is storing some output value, you don’t need the variable.
You can reference the output directly when you need it. Even if you apply some expressions on the output, you can do the same later. No need for a variable.
Storing result from a condition?
Sometimes your flow needs a ‘Condition’, something should happen in one branch, something else in the other. Here you might be again tempted to use a variable, but that’s not necessary.
If it’s just a decision, you can replace the variable with the if(…) expression.
if(equals(outputs('Get_item_-_by_ID')?['body/ApprovalStatus/Value'], 'New'), 'New', 'Other')
Again, usable when you need it, no variable necessary.
Not sure which action did run?
Similarly, if you’re running different actions in the branches, and you’re not sure which branch the flow used.
You don’t need a variable to get the output from the active action, you can use the coalesce(…) expression instead. Coalesce(…) will give you the first non-empty parameter.
coalesce(<param1>, <param2>, <param3>...)
If you use it with outputs from both the actions as parameters, it’ll return the result of the one that did run as the other one will be empty. No need for a variable.
Appending data in a loop?
Another use for variables I’ve seen very often is appending data in a loop. In this case it’s not only useless variable, it also makes your flow slow as explained in a previous article.
Storing flow configuration in a variable?
If you’re using something repeatedly in a flow, it’s a good practice to have these items configured at the beginning of the flow. It can be the url to a SharePoint site, list names, time intervals, etc. Here I can understand the use of a variable, but not one variable per configuration item!
The whole configuration can be done using a single object variable. As used e.g. in the approval process template, instead of 7 variables you can have just one object with 7 properties.
The values can be then accessed using an expression.
variables('<variableName>')?['propertyName']
e.g.
variables('var_flowConfig')?['SPSite']
Summary
As you can see, there’re many situations when you don’t really need a variable. Since every Power Automate action gives you its output, you can reference the output directly, often reducing the number of required variables to 0. And even if you need a variable, it doesn’t have to be 1 variable = 1 value. A single object variable can replace many variables with fixed values, making your flow much cleaner.
I was just building a flow with So. Many. Variables and thinking there has to be a better way than this. I was able to reduce them down with a single object variable and tested it, and it worked perfectly! I added one extra piece– a note on the variable object for myself, with the expression to use so I can quickly copy and paste when I need to access the information later. Thank you for all of these!