Table of Contents
• How Should I Store Values In A Flow?
• Variables
• Compose Action
• Environment Variables
• Configuration Data Table
• Get A Single Key-Value Pair From Configuration Data
• Retrieve All Key-Value Pairs For A Configuration Data Group
How Should I Store Values In A Flow?
Variables, the Compose action, and Environment Variables, and Configuration Data all have a useful role to play in Power Automate flows. This section discusses where and when to use each of them.
Variables
Variables store values that can be referenced in other flow actions. Use a variable when the value will need to be updated at multiple points during the flow run.
Avoid creating variables to store constants. A constant is a value supplied when the variable is initialized that does not change during the flow’s execution.
Do use a variable to store the result of a calculation. Do not use variables when a direct reference can be made in the flow action.
Compose Action
Variables cannot be used inside of Apply To Each loops when in Concurrency enabled. Substitute the Compose action for variables inside to loops to maintain the performance benefit of parallelism.
When a value is stored inside of a Compose action it cannot be updated. Only use it to store values that will not change.
The Compose action is often cited as an alternative to Power Automate variables. Don’t overuse this technique. Compose actions can make flows less readable at a glance because they always show the word Output when referencing a prior action.
Environment Variables
A developer following ALM best practices will create flows in a development environment, then move the flow to a testing environment for testing and into a production environment for go-live. The flow will often need to behave differently in each environment. Use environment variables to store configuration values and avoid hardcoding them into the flow. Editing a flow inside a managed solution will cause an unwanted unmanaged layer to appear.
For example, a flow sending an email targets an email distribution list while in production but only sends messages to the developer while in development and test environments.
Leverage a single environment variable to replace several of the same hardcoded values across multiple flows.
Configuration Data Table
An environment variable should not be created when it will only be used in a single flow. Instead, create a table called Configuration Data and use it to hold key-value pairs containing the flow settings.
The Configuration Data table includes the following single-line text columns:
- Key – describes the key-value pair, must be unique to the group.
- Value – the value to be used inside the flow
- Group – identifies a set of key-value pairs, making it possible to get all needed key-value pairs at once
ID | Key | Value | Group |
1 | DistributionList | [email protected] | DailyNotificationsFlow |
2 | EnableSendingEmail | true | DailyNotificationsFlow |
3 | DelaySeconds | 60 | DailyNotificationsFlow |
4 | OutputFolder | C:/Documents/Sales Reports | GenerateReportFlow |
5 | DelaySeconds | 15 | GenerateReportFlow |
Replace as many hardcoded values in a flow as possible. This will eliminate the need to redeploy a flow to make changes to its behavior.
Get A Single Key-Value Pair From Configuration Data
A single value key-value pair can be obtained using the Get Item (SharePoint) or Get Row (Dataverse) action.
To use the configuration data value select it from the dynamic content menu.
Retrieve All Key-Value Pairs For A Configuration Data Group
All key-value pairs for a group (e.g. DailyNotificationsFlow) can be retrieved using this flow pattern.
The following expression is used in the Compose: Key-Value Pairs Object flow action.
first(json(replace(replace(string(body('Select:_Key-Value_Pairs')), '},{', ','),'null,','"",')))
Configuration data values for the group can be accessed in the dynamic content menu.
Did You Enjoy This Article? 😺
Subscribe to get new Power Apps & Power Automate articles sent to your inbox each week for FREE
Questions?
If you have any questions or feedback about Power Automate Standards: Variables please leave a message in the comments section below. You can post using your email address and are not required to create an account to join the discussion.
“Variables cannot be used inside of Apply To Each loops when in Concurrency enabled. Substitute the Compose action for variables inside to loops to maintain the performance benefit of parallelism.”
Awesome tip. I have always used variables by default, but started wondering what the point of Compose really was. Removing all the variables inside my loop reduced time to run flow from 52 seconds to 8! Many thanks 🙂
Jimmy,
I had this same evolution in my thought process. Coding in Power Automate is so much better without variables. Thanks for the feedback.
Many thanks for the great summary on this! But:
“Do not use variables when a direct reference can be made in the flow action.”
For me this is not a yes or no. While direct references might be faster, sometimes I still use variables to make the flow easier to understand. I would use Compose instead, if there would be an option to name the output, but that’s currently not possible.
Gianluca,
This is a good debate to have. I’m all in favor of readability. Having outputs everywhere really doesn’t help 🙁
Interesting, but these days I am trying to avoid using variables AND composes as much as possible! How? Use one compose/variable to create a json object with as many ‘variables’ or ‘settings’ as I need, followed by a parse json. Each ‘setting’ is then usable in the flow and is discoverable to use!
Benefits include, shorter flows, more readable when troubleshooting as all values can be seen in one compose. You can use conditions for values of settings using coalesce or if statements….
I also tend to do all my settings/ initialize variables at the top of my flows… so structure is important…. Go wide, not deep! Easier to read, especially when using scopes to group related code(note there is a depth limit of 9).
If I have like 10 or more variables use branches…1 branch for the settings, x number for variables.
Hope this helps!