Power Automate has a fantastic feature to reference information from other actions. It’s usually accessible in the “Dynamic Content” tab, but there are times where we want to keep additional information. For example, let’s say that we get a list of items from SharePoint with a list of people, and we want to keep an impressive list of countries where they are from. The country information is available in SharePoint, but we need to do some work to fetch the unique items. That’s where variables come into play, and the first thing we need to do is use the “Initialize variable” action.
So let’s look at how it looks like, some things to keep in mind, and overall how to use it properly.
Where to find it?
To find it, you can search for the “Initialize variable” action or by going to “Built-in.”
Expand to see all actions.
You can find the section called “Variable.”
Pick the “Initialize variable” action.
Here’s what it looks like.
Now that we know where it is, let’s use it.
The “Initialize variable” action has three parameters:
- The name of the variable.
- Type, like “Float,” for example.
- Value, which is the initial value that is assigned to the variable. This parameter is optional.
We want to have a counter with people on a list over 50. Here’s what it looks like.
Since it’s a counter, we will initialize it at 0.
Regarding the type of data, the “Initialize variable” can accept multiple types, like “Integer,” “Float,” and even “Object.”
There’s two main “types” or “groups” that we can consider:
- Single value, like “String,” for example.
- Multiple values, like “Array.”
Although they are in the same list of options, they behave slightly differently and change other actions depending on the type. So let’s explore them individually.
The single value variables, like the name indicates, contain only one value. Above we looked at the “Integer” value, so let’s pick “Float” as another example.
Notice that we didn’t provide a value, so let’s add 1000 (using the “add function”) to it and see what happens.
add(variables('Total expenditures with people'),1000)
Here’s the Flow.
The result is:
It works. Although we didn’t add any value to start, Power Automate is smart enough to understand that a “Flow” or an “Integer” has an implicit default of zero.
What happens if we add an incorrect type. For example, a string into a “Float” value?
Power Automate checks that before you run the Flow. Here’s another example with a “Boolean” type.
Notice that even “known correct” values will fail. For example, “1” is widely known as the value of “true” and “‘0” of “false. Nevertheless, power Automate will still consider it invalid.
Finally, let’s look at the “Object” value. The “Object” value can contain a complex group of information that you can refer to. For example, let’s say that you’re fetching the information of a person in your Microsoft 365 directory using the “Get User Profile” action, and you want to refer to it after. It would be a pain to have multiple variables for “Name” “Email,” to name a few. So we can add the whole “object” in a variable and refer to it after. Here’s an example with the “Get my profile” action.
Notice that this is not the same as an array. An array will keep a group of items of the same type in a list. Think of the “object” as storing a “bag” of stuff where an array is a “box of chocolates.”
The last type is the “Array” type. As mentioned before, an “array” variable will contain a set of items of the same type (roughly). We can use the “Append to array variable” action to add values to an array.
For example, we can add to an array an “Integer” and a “String”:
And Power Automate will add it without issues:
There’s no validation of the types in the “array,” so you can have a mixed bag of things.
You can also provide default values, but you need to be careful because Flow behaves differently from “single value” elements. For example, if you initialize it with an Integer value, it won’t complain:
But it will return an exception when running:
Power Automate will return the following exception:
The variable ‘Group of items’ of type ‘Array’ cannot be initialized or updated with value of type ‘Integer’. The variable ‘Group of items’ only supports values of types ‘Array’.
So how can we provide values?
Variables can only be initialized at the top level of the Flow. Here’s an example: we try to do it inside a “Condition” action.
Power Automate returns the following exception:
The operation ‘Initialize variable’ can only be used at top level.
I recommend adding the variables at the beginning to know what variables you have on your Flow from the start. If you want to add them in the middle of your Flow, please be careful when moving actions around because it can break your Flow.
Here are some things to keep in mind.
The variable name is super important.
In the above examples, we named the variable with underscores, but we can name it with spaces, like:
It’s not essential if you add it with or without spaces, but I recommend that you have a strategy to name things. It’s much easier to have a standard syntax to look at something and know it’s a variable, for example. I also recommend that you define in the name the type of variable. For example, having “Number of people above 50” in the name will tell you all to want to know instantly, but if you have” People above 50,” you won’t know if it’s the number of people, the list of people or something else.
Keep arrays consistent
Although Power Automate enables you to mix multiple types, I strongly recommend checking and ensuring that only one type is inserted per array.
For example, if you have an array of integers with a string in the middle, the Flow will fail when you try to sum all the numbers. There’s not much you can do with an array with mixed value types, but if you have a use case that makes sense, please DM me on Twitter, and I’ll add it here.
Initialize it every time you can
Having a start value for the “Initialize variable” is always a good idea because you provide extra information for the person reading the Flow. For example, think of a variable that keeps track of the number of times something happens. The variable can have 0, but it may also make sense to start at 1. You can have the logic well defined in your Flow, but by having the value in the start value, you’re making things a lot more clear on “how” you will count.
If it’s an array, make it clear on the name.
Since arrays behave differently than the other variables, it’s always a good practice to have them named differently. In addition, it will avoid many mistakes and failing Flows since Power Automate won’t always check the variable type before it runs.
Name the action correctly.
You don’t need to indicate in the variable its type, but it’s always good to indicate what the variable contains. It will make you identify the variable that you need a lot easier. Some people add the “var” prefix to the variables to indicate that it is a variable, but, in my opinion, this is not an improvement. We already know by the UI that it is a variable, and we’ll be making the names a bit more obtuse without adding a lot of value.
Use “Object” sporadically.
There’s the temptation to have “object” variables when you don’t want to overthink about the type, but I wound strongly advise against using it. The “object” type is rarely helpful. But if you define the variables with the correct datatypes, Power Automate will filter the ones that “don’t make sense” in the actions. For example, if an effort expects an integer value, the “Display Dynamic” values will only display the “Integer” variables and not all of them.
Always add a comment.
Adding a comment will also help avoid mistakes. For example, indicate why you’re using the “Initialize variable” and what the variable will be used. Variables with the same name can be used for entirely different reasons. It’s also good to indicate where it will be used in the Flow. For example, you may have two variables that keep track of the number of elements—adding a quick comment indicating that a variable is for section “A” and the other for section “B” would go a long way in debugging if something is off.
Always deal with errors.
Have your Flow fail graciously and notify someone that something failed. It’s horrible to have failing Flows in Power Automate since they may go unlooked-for a while or generate even worse errors. I have a template, and a template that you can use that will help you make your Flow resistant to issues. You can check all details here.
Back to the Power Automate Action Reference.