Power Automate Apply To Each Loops 20X Faster
The Power Automate ‘Apply to Each‘ loop tends to operate at a reduced pace because it processes actions in a sequential manner. By leveraging parallelism, we can enhance the efficiency of the ‘Apply to Each‘ feature, allowing several threads to operate concurrently. Such parallel processing can boost a flow’s speed, potentially cutting down its completion time by over 20 times. In this piece, I’ll guide you on how to accelerate the ‘Apply to Each‘ action in Power Automate.
Introduction: The Past Due Invoices Flow
A plumbing firm utilizes Power Automate to determine if an invoice is overdue and subsequently modifies its status. To enhance processing speed, the flow updates invoices simultaneously rather than one after the other.
Setup The SharePoint List
“Set up a new SharePoint List named ‘Company Bills‘ with these columns:
- BillID (number)
- SupplierName (single-line text)
- BillTotal (number)
- OverdueStatus (yes/no)
Enter the following entries into the ‘Company Bills’ list:
Note: I’ve changed certain terminologies (e.g., “Invoices” to “Bills“, and “VendorName” to “SupplierName“) to give it a different feel while retaining the context. Adjust as needed.
The SharePoint list should appear as follows once configured.
Run Apply To Each Loops Actions One-At-A-Time
We aim to develop a process that retrieves all entries from the Company Invoices SharePoint list and sets the IsPastDue column to “Yes.” Initially, our flow will process each item sequentially. We will then optimize it to process multiple items concurrently.
Proceed to design the specified process in Power Automate. During the Update Item stage, adjust the IsPastDue column to “Yes” in the ‘Change Past Due To Yes’ action.
Examine the flow to determine its speed. During this examination, the flow finished in 9 seconds.
Improve Apply To Each Speed 20X With Parallelism
The ‘Apply To Each’ action is currently lagging in our workflow. We can enhance its speed significantly by utilizing parallel processing. This technique lets us run multiple threads simultaneously. To activate parallel processing, go to the ‘Apply To Each’ action settings.
Next, enable Concurrency control. By default, it allows 20 simultaneous threads. Before increasing this number, it’s advisable to review the throttling limits of the Power Automate SharePoint connector. Overloading with too many concurrent API calls might actually reduce the flow’s speed.
Once you’ve set the desired parallelism level, click ‘Done‘.
Check the flow’s speed to gauge its efficiency. It now finishes in just 2 seconds, down from the previous 9 seconds. What a remarkable enhancement!
Problems With Variables While Using Parallelism In Loops
Let’s examine a different situation to grasp the constraints of parallel processing and strategies to address them. In this process, we aim to increase the ‘InvoiceAmount‘ column for every company invoice by 10, as a penalty for missed payments.
We’ve introduced an ‘Initialize Variable’ action at the beginning of the flow and assigned a value of 0 to varUpdatedAmount. Within the ‘apply to each’ loop, we incorporated a ‘Set Variable’ action using the provided code.
When initiating a ‘Set variable’ step within an ‘apply to each’ loop in Power Automate, we receive a cautionary prompt. Using variables in a loop with parallelism active is not recommended. This poses a risk, as simultaneous threads could alter our variable, leading to inaccurate outcomes in the ‘Update Item’ action.
Replace Set Variable In Apply To Each Action With Compose
How can we address this issue? It’s necessary to utilize variables inside the ‘Apply to Each’ function. Luckily, there’s an alternative method.
Eliminate the ‘Initialize Variable’ at the beginning of the flow. Subsequently, within the ‘Apply to Each’ loop, substitute the ‘Set Variable’ action with a ‘Compose’ action.
The “Compose” action delivers its outcome in a text format. Thus, in the “Update Item” action’s InvoiceAmount field, we need to draft a flow expression to convert the type to a float, which represents a decimal number.
Reference: Decimal, Float, and Value functions
The flow now operates without displaying any warnings and achieves the expected outcome. Please test the flow to ensure its continued functionality.
Some Relevant Insights:
- What It Is: Power Automate Parallel Processing involves running multiple threads simultaneously to enhance the speed of ‘Apply to Each‘ loops in workflows.
- Benefit: This method can accelerate flow completion times by over 20 times compared to sequential processing.
- Use Case: Particularly useful in scenarios like updating multiple SharePoint list items concurrently.
- Limitation: Caution is advised when using variables in parallel processing due to the risk of inconsistent outcomes.
- Solution: Using the ‘Compose‘ action instead of ‘Set Variable‘ within the loop for safer and more efficient operations.