Power Automate - Compose vs Variable
Charlie Phipps-Bennett ????
2x Microsoft BizApps MVP | Delivery Director at Synapx | Founder - SPPUG | Power Apps Super User -??MVP-Phipps | ??PowerApps | ??PowerBi | ??PowerAutomate | ??Copilot
Variables vs Compose…
Using variables inside your flows can be very handy. With a variable, you can store data to reuse within your flow. You can update the data in the variable, and you can append it to either a string or an array.
So, what is a Compose? A compose is an action within Power Automate that enable you to have an input and perform an action against it. Or you can use a compose to return an output. For example, if I was to filter on a SharePoint list and I want to display the list values, I could put the values into a compose.
Why use a Compose rather than a Variable?
With a variable you can’t/shouldn't use concurrency or parallelism. When you turn concurrency on it allows you to run multiple loops at the same time.
My use case - get a list of users from an Azure AD group which was about 2500 users. I then wanted to take the user's Display Name and Email to return to Power Apps. This meant for each user collected from Azure AD it would append to a string variable within an apply to each.
In an ‘Apply to each’ you can navigate to the settings and turn on the ‘Concurrency Control’ and then set the ‘Degree of Parallelism’. You can set the value of concurrency from 1 to 50. When you set the concurrency to 50, you tell the flow it can process 50 loops at the same time.
领英推荐
It is not recommended by Power Automate to turn on concurrency when using variables because ‘Apply to each’ loops do not have their own context of the variable so if you set the variable and try to use it later you will face problems.
I tried anyway because this was not going to cause issues because I was not using the variable afterward. The flow took 10mins 21 seconds to execute which was just far too long.
So instead of using a variable and an append to string variable within an apply to each. I use a compose within an apply to each. I then use a join outside of the apply to each to append the values to an array. This only took 1min 18 seconds to execute.
So how do you make a compose an array? Compose action has one special feature. When you use it inside of an apply to each. It turns the compose into an array when you refer to it outside of the apply to each action.
One thing I noticed is, the compose outputs do not appear in dynamic contents like a variable output would. However, this does not stop you from writing out the expression and referring to the compose output.
Conclusion:
You will find some scenarios whereby a variable is more suitable than a compose action. However, if you can use a compose then I would always recommend it for two main reasons. When you initialise variables in Power Automate you cannot put these within a scope action which can make flows quite messy at first look. However, you can put composes within a scope, this allows you to have all ‘variable’ (composes) declared in a tidy scope which makes the flow more maintainable. The other obvious advantage is the speed, variables can slow your flows and it is not only when working with concurrency.?
Streamlining systems and processes using the Microsoft Power Platform. Microsoft Business Applications MVP.
2 年You make a good point. But in this particular case you need neither a compose nor a variable. You don't even need the apply to each. My guess would be that you could get that flow down to well under 10 seconds. If you have 5 minutes today give me a call and I will show you how to do it.