Variables vs. Compose in an apply to each in Power Automate

Speeding Up Power Automate: Replacing ‘Append to Variable’ with ‘Compose’ for Efficient Workflows

Experiencing slow performance with the ‘append to’ variable in Power Automate’s ‘apply to each’ loops? This insightful guide reveals how substituting ‘append to’ variables with ‘compose’ actions can significantly accelerate your loops. We’ll walk you through an example, showcasing the transformative impact of this method on your Power Automate flows. Enhance your efficiency and streamline your processes with our step-by-step approach.

Part one: Using Variables in an Apply to Each in Power Automate

In this part, we look at using ‘Variables’ in an ‘Apply to Each’ in Power Automate. Let’s look at an example loop that’s using the ‘append to’ variable. In the example below, we’re examining a manually triggered flow that lists an account’s related contact records. Let’s go through it quickly, step by step. More details continue under the screenshot.

Step 1: The flow starts with the trigger ‘Manually trigger a flow,’ which allows you to run the flow manually or through the action ‘Run a Child Flow’ from another flow. After the trigger, we initialize an array variable intended to store our custom-defined JSON contact objects for demonstration purposes.

Step 2: Add a Dataverse ‘List rows’ action with a Fetch XML Query to retrieve an account’s related records by listing contacts and filtering on the ‘parentcustomerid’ attribute in the contact table. This will list all contacts related to the account, filtered by a ‘hard-coded’ GUID. For those who wish to copy or view it, check it out below.

<fetch>
  <entity name="contact">
    <attribute name="firstname" />
    <attribute name="jobtitle" />
    <filter>
      <condition attribute="parentcustomerid" operator="eq" value="40cdc810-6a66-df11-aed4-000c293b421c" />
    </filter>
  </entity>
</fetch>

A tip for creating Fetch XML queries for your ‘List rows’ actions: use the FetchXML Builder tool available in XrmToolBox.

Step 3: Next, we use an ‘apply to each’ action that loops through all the contacts related to the account. Then, we use the ‘append to array variable’ action and define our own JSON object with the fields from each contact in the loop.

Step 4: Continuing, we output the array within a ‘Compose’ action. This allows us to see what the array looks like after it has finished looping through the contact records and adding the JSON objects to the array variable.

variables('contacts')

Step 5: If we run this flow, it takes 44 seconds (in the time of writing, this might change from each run) to loop through 139 contacts and append them to an array variable. To speed things up, we can improve performance by switching from using variables to compose actions. See the next part for details.

Part Two: Using Compose in an Apply to Each in Power Automate

In the next part, we examine using ‘Compose’ in an ‘Apply to Each’ in Power Automate. The same example as in Part One, but with some modifications. Let’s go through it step by step. The complete flow looks like below. More details continue under the screenshot.

Step 1: Similar to Part One, it starts with adding the ‘Manually trigger a flow’ trigger and then includes the Dataverse ‘List rows’ action with a Fetch XML Query. In this flow, we’re not using variables at all, but rather compose actions to create a list of objects.

Step 2: Instead of adding an ‘Append to array variable,’ we create a ‘Compose’ action and add the same JSON structure as we did with the variable.

Another step we need to take is to turn on ‘Concurrency Control.’ This enables us to run multiple parallel branches at once, which will speed up the process.

To turn on Concurrency Control, click the three dots on the ‘Apply to each’ action, then click on ‘Settings.’ Next, turn on Concurrency Control, drag the slider to the right to the maximum (50), and finally, click ‘Done.

💡NOTE
When using ‘Compose’ with concurrency in Power Automate, we avoid the complications seen with ‘Append to variable’ in parallel loops. Normally, variables in a loop are processed one by one, which is straightforward but slow. Enabling concurrency without using ‘Compose’ can lead to confusion because all loop items try to access and modify the same variable simultaneously, causing unpredictable outcomes. This is because variables are shared across all iterations, leading to conflicts. However, ‘Compose’ actions are self-contained, meaning they don’t rely on shared variables in the same way, making them ideal for concurrent execution where each loop can operate independently without interference. This method ensures efficiency and reliability in processing multiple items at once.

Step 3: Last step, we’re outputting the ‘Compose – Append to Contact List’ compose from within the ‘Apply to each’ action to see the result. To find the reference to the compose output, we need to add it using an expression like below. The compose won’t appear in the dynamic values tab.

outputs('Compose_-_Append_to_Contact_List')

When using this approach, the compose action automatically creates an array by default. Therefore, there’s no need to declare an array before appending to it with this method.

Step 4: Running this flow takes just 7 seconds, a significant improvement from the 44 seconds required when using variables. It’s a clear win, resulting in a much more efficient flow.

Conclusion

To sum it up, using ‘Compose’ actions in Power Automate makes things faster and smoother. It’s a great way to make your workflows simpler and avoid problems that come with using ‘Append to variable’ in many tasks at once. ‘Compose’ lets each task run on its own without mixing up, making sure everything works well and quickly.

Resources

Making the ‘Append to’ loops in your Power Automate flow faster (tomriha.com)

Using variable in a parallel loop in Power Automate flow (tomriha.com)


For More Content See the Latest Posts