Power Automate Apply To Each Loops 20X Faster
Power Automate Apply To Each Loops 20X Faster Posted by - Matthew Devaney on - July 23, 2023 11 Comments The Power Automate apply to each loop often runs slo...
- Published
- Reading time
- 5 min read
Before you start
Is this guide for you?
- Best entry point
- Power Automate
- Time investment
- 5 min read
Imported reference from Matthew Devaney's blog for learning purposes. Original: https://www.matthewdevaney.com/power-automate-apply-to-each-loops-20x-faster/. Author: Matthew Devaney.
Power Automate Apply To Each Loops 20X Faster Posted by - Matthew Devaney on - July 23, 2023 11 Comments
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: InvoiceNumber VendorName InvoiceAmount IsPastDue 1001 Wordify 105 No 1002 Twitterwire 350 No 1003 Bubblebox 240 No 1004 Twitterlist 335 No 1005 Skipstorm 250 No 1006 Flashset 165 No 1007 Chatterpoint 110 No 1008 Kazio 355 No 1009 Abata 175 No 1010 Gabspot 130 No 1011 Dabtype 210 No 1012 Linkbridge 335 No 1013 Gigazoom 175 No 1014 Skidoo 415 No 1015 Wikizz 470 No 1016 Edgepulse 200 No 1017 Viva 465 No 1018 Vimbo 500 No 1019 Mycat 225 No 1020 Fliptune 265 No
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.
add(items( 'Apply_to_each:_Company_Invoice' )?[ 'InvoiceAmount' ], 10 ) Code language: JavaScript ( javascript )
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.
float(outputs( 'Compose:_Updated_Invoice_Amount' )) Code language: JavaScript ( javascript )
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 Copilot Studio articles sent to your inbox each week for FREE Enter your email address Sign Me Up Questions?
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. Matthew Devaney Power Automate
Save Docusign Document To SharePoint With Power Automate
Tagged
Power Automate
Have a Microsoft 365 topic idea?
Share article suggestions, community session ideas, corrections, or real-world scenarios for future nextM365 learning notes.
Keep learning Microsoft 365
Explore more practical guides for SharePoint, Power Platform, Copilot Studio, migration, automation, governance, and security.
Continue learning
Related tutorials
Related questions
Related comparisons
Next action