Power Automate Apply To Each Loops 20X Faster
The Power Automate apply to each loop often runs slowly because it executes actions sequentially one-by-one. We can improve the performance of Apply To Each by using parallelism to have multiple threads running at the same time. Parallelism increases the speed of a flow and reduces completion time by 20x or more. In this article I will show you how to run the Power Automate apply to each action faster.
Table of Contents • Setup The SharePoint List • Run Apply To Each Loop Actions One-At-A-Time • Improve Apply To Each Speed 20X With Parallelism • Problems With Variables While Using Parallelism In Loops • Replace Set Variable In Apply To Each Action With Compose
Introduction: The Past Due Invoices Flow
A plumbing company uses a Power Automate flow to check if an invoice is past due and then update its status. The flow applies invoice updates concurrently instead of sequentially to speed up processing times.
Setup The SharePoint List
Create a new SharePoint List called Company Invoices with the following columns:
- InvoiceNumber (number)
- VendorName (single-line text)
- InvoiceAmount (number)
- IsPastDue (yes/no)
Populate the Company Invoices list with these items:
Once setup the SharePoint list should look like this.
Run Apply To Each Loops Actions One-At-A-Time
We want to build a flow that gets all items in the Company Invoices SharePoint list and updates the yes/no column IsPastDue to equal “Yes.” The first flow we build will run the apply to each action sequentially one-by-one. Then we will improve the flow using parallelism to execute many apply to each actions at the same time.
Create the following flow in Power Automate. Set the IsPastDue column to Yes in the Update Item: Change Past Due To Yes action.
Test the flow to check how fast it runs. In this test the flow took 9s to complete.
Improve Apply To Each Speed 20X With Parallelism
The Apply To Each action runs very slowly in our current flow. We can improve it to run lighting-fast by using parallelism. Parallelism allows us to execute multiple threads at once. Enable parallelism for the Apply To Each action by opening its Settings.
Then turn on Concurrency control. The default degree of parallelism is 20 concurrent threads. If we wanted to increase it further, it would be a good idea to check the throttling limits for the Power Automate SharePoint connector. Too many concurrent API can cause the flow to run slower.
After selecting the degree of parallelism press Done.
Now test the flow to see how fast it runs. The flow takes only 2s to complete compared to the original time of 9s. An amazing improvement!
Problems With Variables While Using Parallelism In Loops
Let’s review another scenario to understand the limitations of parallelism and how to deal with them. In this flow we want to increment the InvoiceAmount column for each Company Invoice by 10 as a penalty for non-payment.
We have added an Initialize Variable step at the start of the flow and set varUpdatedAmount to 0. Then we included a Set Variable step inside the apply to each loop with the following code.
When we create a Set variable step inside an apply to each loop Power Automate gives us a warning. Variables should not be used within a loop while parallelism is enabled. The danger is our variable would be set by multiple concurrent threads running at once and provide an incorrect result to the Update Item action.
Replace Set Variable In Apply To Each Action With Compose
How can we solve this problem? We do need to use variables within an apply to each action. Fortunately there is a workaround.
Remove the Initialize Variable from the start of the flow. Then replace the Set Variable action with a Compose action inside the Apply to Each loop.
The Compose action outputs its result as a text data type. Therefore, we must write a flow expression in the Update Item action InvoiceAmount field to change the type to a float. A float is a decimal number.
Now flow no longer shows a warning and has the desired result. Test the flow to make sure it still works.
Did You Enjoy This Article? 😺
Subscribe to get new Power Apps & Power Automate articles sent to your inbox each week for FREE
If you have any questions or feedback about Power Automate Apply To Each Loops 20X Faster 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.