First trigger to write following post was my observation that Power Automate designer adds Apply for Each (loop) even if it is not required:
- in Approval action even if we assign single Approver/User
- when we use Get Items or Get files (properties only) but filtering results for single result
Second was query from Power Automate forum:
Third reason and the most important one is of course performance I’m always try to make sure that process is not only working but it is performant. In Power Automate Loop or to many loops can easily drastically affect run/execution time.
In Power Automate as Makers we have a lot of flexibility thanks to the functions and ability to work with array’s / objects content. Thanks to tis two factors we can remove loop. In this post I will show two methods:
- First function
- “Location” of item in array
As this post is quite long let me give you brief introduction to the content:
- Firstly will show how to work with First function and Approval action
- Secondly I will show how to apply First function for Get Items
- Next Bonus how to use Empty function in your Get Items to drive behavior of your Flow
- Lastly you will see more advance technic of using the index of the item in array
The main assumption is that action will return one item you can use:
1.) First function
Hero of this section is the First function below you can find the link to its reference. We will use it by getting the item from collection and then “telling” workflow engine which property we want to extract.
How to get the values of the above function? Let’s do that in baby steps.
When you will look again in to auto- generated loop you will see information:
If you will select the Responses field and copy it to expressions you should get:
Change the name of the Action which you want to use in the expression to make your life easier.
Instead of Start and wait for approval you can use simply Approval.
That would be the first part of what we need. The Responses object/collection. Second part is that you need to know what field we want to extract:
Let’s pick up Responses Comments:
You should be interested only in the last part of the above dynamic filed, remember only value of the last square brackets [‘comments’]. You are taking only this part as items(‘Apply_to_each’)? refers to the loop.
Remaining part is to go to action where we will store our result. I will use Compose.
I’m using here Compose action but in your workflows consider the proper naming of your actions and don’t forget about Variables.
Change to Expression tab and add all elements of our puzzle:
- type function:
- remove @ character if you copied value:
- close function:
- add the field to extract:
Result should looks like this:
Add your expressions as comments to your actions it is much easier to work with your process later on.
Get Items (SharePoint)
Method will works for Get Items and Get files (properties only) actions without problem.
The main difference in Get Items is that you need to create proper Filter Query as Power Automate give us hint that it is ODATA type:
Working with this can be a little bit challenging but practice makes perfect.
Great article about using Filter Query can be found in blog SharePains by Pieter Veenstra:
While using the Get Items action you are referring to value object and not responses – it is specific for Approval action.
I came across the cases when it is worth to use another function when you want to avoid loop.
Imagine scenario where you are filtering your data and you need to either create new item or update metadata of existing one but you cannot use GET ITEM…
After adding all necessary filter to your query you can do following:
- Add Condition after Get Items
- In the Condition add following function
Now for you YES path you know that there is not such item yet so you can go and Create it.
For NO path you can update item and to get the ID of it use the First function approach.
2.) “Location” of item in array
This approach will be for sure familiar for people which have background in programming. Basically what we want to do here is use the array index (starting with 0).
Example for Approvals:
Both example will take the first array item. If you will change  to  you will get access to second item and soon on.
For the Power Automate beginners maker this approach might looks weird but it open doors to more powerful scenarios where we are working with big objects and still don’t want to Use the loop.
Example of such situation might be getting User Display Name from Pepole picker which allows multiple selections:
With presented approached you can remove the unnecessary Power Automate loops from your process and make it easier to maintain, runs finish quicker.
It is also good for performance which should be always in Makers mind.
Final disclaimer I’m not loop hater just prefer to use them when it is needed only.
Initially I felt that this will be short post. If you survive so far thank you for that and hope this information will help you during your Power Automate journey.
About the Author:
I’m working in IT almost 11 years and over half of this time it was for me SharePoint related work, were I was helping organization to transform and move their paper based process to digital world. Had the chance to work with InfoPath, Classic SP workflows, K2, Nintex and of course Power Platform. Is see bright future in Power Platform development and personally love it for the freedom and opportunities which it gives me. To increase and speed up the adoption Im trying my best to help other grow quickly 😀 #sharingIsCaring
Ziolkowski, D. (2020). Power Automate without Loop. Available at: https://365corner.pl/2020/02/07/power-automate-without-loop/ [Accessed: 9th April 2020].
Find more great Power Platform content here.