Avoid Unnecessary Looping (Apply to each) in Power Automate

Published Mar 06 2021 08:26 AM 13K Views
Senior Member

In this blog post I will show you how to avoid creating unnecessary apply to each loops in Power Automate which can slow down your flow and make it harder to read.

 

Use Case

Often you want to retrieve just one item in a SharePoint list and then display the value of a property from it. You will use the SharePoint Get Items action with a query (ODATA filter query) i.e. Title eq 'City' and you know that it should only return one record. Unfortunately the SharePoint Get Items action by default returns an array (collection of items) even if there is just one item and even if you specify the Top Count (items to be returned) to be 1. So if you want to use a property from the Get Items action then Power Automate will automatically add the action to an Apply to each loop as it comes from an Array (see image below).

LeonArmston_1-1615045186646.png

 

If we test the Flow and we can see there is only one object in the array so we can see the Apply to each loop is unnecessary as it does not need to loop through any other items.

 

LeonArmston_2-1615045186651.png

 

I always like to reduce the amount of actions and loops in Power Automate for speed and readability reasons.

 

Remove unnecessary Apply to each loops with the first() function

I will now show you how to avoid having unnecessary Apply to each loops being added to your Flow and instead just access the first element of the array using an Expression.

 

Enter to the room the Power Automate first function which used for retrieving the first item from an array or string. Now we will delete the Apply to each loop from our Flow and then create a new Compose action where we will enter the following expression to access just the Title property of the list item without a loop.

 

 

 

 

 

first(body('Get_Items')['value'])['Title']

 

 

 

 

 

LeonArmston_3-1615045186656.png

 

In the image below I will describe the expression: in blue the first() expression is being used and within it is brackets () in red is a body reference to the SharePoint Get Items action. If you have renamed the SharePoint Get Items action then you will need to replace 'Get_Items' with the name of your action with spaces replaced by underscores. Next in Grey is the SharePoint value column which holds all of the SharePoint list item values in an array – this does not need to be changed. The first function brackets are then closed in blue. Then finally in green enter the internal name of the SP column you wish display in my case the Title column.

 

LeonArmston_4-1615045186663.png

 

The above expression could also be done with the following expression using [0] to access the first element of the array. However using the first() function is much better for readability 

 

 

body('Get_Items')['value'][0]['Title']

 

 

 

I will now test my Flow by running it and we can now see from the image below that it ran successfully and without a adding the Compose action to a Apply to each loop.

LeonArmston_5-1615045186665.png

 

Summary

So there we have it we have learnt how to remove unnecessary Apply to each loops when using the SharePoint Get Items action by using the First() function. You can use the knowledge you have learned in this article to reduce the amount of actions and loops in your flows in Power Automate today. As an extra bonus the same first() function can also be used in Azure Logic Apps which names it’s loop For each.

%3CLINGO-SUB%20id%3D%22lingo-sub-2190265%22%20slang%3D%22en-US%22%3EAvoid%20Unnecessary%20Looping%20(Apply%20to%20each)%20in%20Power%20Automate%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2190265%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20this%20blog%20post%20I%20will%20show%20you%20how%20to%20avoid%20creating%20unnecessary%20apply%20to%20each%20loops%20in%20Power%20Automate%20which%20can%20slow%20down%20your%20flow%20and%20make%20it%20harder%20to%20read.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%225%22%3E%3CSTRONG%3EUse%20Case%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3EOften%20you%20want%20to%20retrieve%20just%20one%20item%20in%20a%20SharePoint%20list%20and%20then%20display%20the%20value%20of%20a%20property%20from%20it.%20You%20will%20use%20the%20SharePoint%26nbsp%3BGet%20Items%20action%20with%20a%20query%20(ODATA%20filter%20query)%20i.e.%20%3CSTRONG%3ETitle%20eq%20'City'%3C%2FSTRONG%3E%20and%20you%20know%20that%20it%20should%20only%20return%20one%20record.%20Unfortunately%20the%26nbsp%3BSharePoint%20Get%20Items%20action%20by%20default%20returns%20an%20array%20(%3CEM%3Ecollection%20of%20items%3C%2FEM%3E)%20even%20if%20there%20is%20just%20one%20item%20and%20even%20if%20you%20specify%20the%20Top%20Count%20(%3CEM%3Eitems%20to%20be%20returned%3C%2FEM%3E)%20to%20be%201.%20So%20if%20you%20want%20to%20use%20a%20property%20from%20the%20Get%20Items%20action%20then%20Power%20Automate%20will%20automatically%20add%20the%20action%20to%20an%20Apply%20to%20each%20loop%20as%20it%20comes%20from%20an%20Array%20(%3CEM%3Esee%20image%20below%3C%2FEM%3E).%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22LeonArmston_1-1615045186646.png%22%20style%3D%22width%3A%20741px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F261392i55FA420DAF7B3D75%2Fimage-dimensions%2F741x912%3Fv%3D1.0%22%20width%3D%22741%22%20height%3D%22912%22%20role%3D%22button%22%20title%3D%22LeonArmston_1-1615045186646.png%22%20alt%3D%22LeonArmston_1-1615045186646.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20we%20test%20the%20Flow%20and%20we%20can%20see%20there%20is%20only%20one%20object%20in%20the%20array%20so%20we%20can%20see%20the%20Apply%20to%20each%20loop%20is%20unnecessary%20as%20it%20does%20not%20need%20to%20loop%20through%20any%20other%20items.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22LeonArmston_2-1615045186651.png%22%20style%3D%22width%3A%20774px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F261393i9D9FC94ABE2C0764%2Fimage-dimensions%2F774x735%3Fv%3D1.0%22%20width%3D%22774%22%20height%3D%22735%22%20role%3D%22button%22%20title%3D%22LeonArmston_2-1615045186651.png%22%20alt%3D%22LeonArmston_2-1615045186651.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20always%20like%20to%20reduce%20the%20amount%20of%20actions%20and%20loops%20in%20Power%20Automate%20for%20speed%20and%20readability%20reasons.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%225%22%3E%3CSTRONG%3ERemove%20unnecessary%20Apply%20to%20each%20loops%20with%20the%20first()%20function%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3EI%20will%20now%20show%20you%20how%20to%20avoid%20having%20unnecessary%20Apply%20to%20each%20loops%20being%20added%20to%20your%20Flow%20and%20instead%20just%20access%20the%20first%20element%20of%20the%20array%20using%20an%20Expression.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EEnter%20to%20the%20room%3C%2FSTRONG%3E%20the%20Power%20Automate%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Flogic-apps%2Fworkflow-definition-language-functions-reference%23first%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Efirst%20function%3C%2FA%3E%26nbsp%3Bwhich%20used%20for%20retrieving%20the%20first%20item%20from%20an%20array%20or%20string.%20Now%20we%20will%20delete%20the%20Apply%20to%20each%20loop%20from%20our%20Flow%20and%20then%20create%20a%20new%20Compose%20action%20where%20we%20will%20enter%20the%20following%20expression%20to%20access%20just%20the%20Title%20property%20of%20the%20list%20item%20without%20a%20loop.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3Efirst(body('Get_Items')%5B'value'%5D)%5B'Title'%5D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22LeonArmston_3-1615045186656.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F261395i846C94EEFEC7F0F2%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22LeonArmston_3-1615045186656.png%22%20alt%3D%22LeonArmston_3-1615045186656.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20the%20image%20below%20I%20will%20describe%20the%20expression%3A%20in%20%3CSTRONG%3E%3CFONT%20color%3D%22%230000FF%22%3Eblue%3C%2FFONT%3E%3C%2FSTRONG%3E%20the%20first()%20expression%20is%20being%20used%20and%20within%20it%20is%20brackets%20()%20in%20%3CSTRONG%3E%3CFONT%20color%3D%22%23FF0000%22%3Ered%3C%2FFONT%3E%3C%2FSTRONG%3E%20is%20a%20body%20reference%20to%20the%20SharePoint%20Get%20Items%20action.%20If%20you%20have%20renamed%20the%20SharePoint%20Get%20Items%20action%20then%20you%20will%20need%20to%20replace%20'Get_Items'%20with%20the%20name%20of%20your%20action%20with%20spaces%20replaced%20by%20underscores.%20Next%20in%3CSTRONG%3E%3CFONT%20color%3D%22%23808080%22%3E%20Grey%3C%2FFONT%3E%3C%2FSTRONG%3E%20is%20the%20SharePoint%20value%20column%20which%20holds%20all%20of%20the%20SharePoint%20list%20item%20values%20in%20an%20array%20%E2%80%93%20this%20does%20not%20need%20to%20be%20changed.%20The%20first%20function%20brackets%20are%20then%20closed%20in%20%3CSTRONG%3E%3CFONT%20color%3D%22%230000FF%22%3Eblue%3C%2FFONT%3E%3C%2FSTRONG%3E.%20Then%20finally%20in%20%3CSTRONG%3E%3CFONT%20color%3D%22%23008000%22%3Egreen%3C%2FFONT%3E%3C%2FSTRONG%3E%20enter%20the%20internal%20name%20of%20the%20SP%20column%20you%20wish%20display%20in%20my%20case%20the%20Title%20column.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22LeonArmston_4-1615045186663.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F261396i47A7FBE6A970B24E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22LeonArmston_4-1615045186663.png%22%20alt%3D%22LeonArmston_4-1615045186663.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20above%20expression%20could%20also%20be%20done%20with%20the%20following%20expression%20using%20%3CSTRONG%3E%5B0%5D%3C%2FSTRONG%3E%20to%20access%20the%20first%20element%20of%20the%20array.%20However%20using%20the%20%3CSTRONG%3Efirst()%3C%2FSTRONG%3E%20function%20is%20much%20better%20for%20readability%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3Ebody('Get_Items')%5B'value'%5D%5B0%5D%5B'Title'%5D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20will%20now%20test%20my%20Flow%20by%20running%20it%20and%20we%20can%20now%20see%20from%20the%20image%20below%20that%20it%20ran%20successfully%20and%20without%20a%20adding%20the%20Compose%20action%20to%20a%20Apply%20to%20each%20loop.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22LeonArmston_5-1615045186665.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F261394i2AE18D02BFBE7403%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22LeonArmston_5-1615045186665.png%22%20alt%3D%22LeonArmston_5-1615045186665.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%225%22%3E%3CSTRONG%3ESummary%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3ESo%20there%20we%20have%20it%20we%20have%20learnt%20how%20to%20remove%20unnecessary%20Apply%20to%20each%20loops%20when%20using%20the%20SharePoint%20Get%20Items%20action%20by%20using%20the%20First()%20function.%20You%20can%20use%20the%20knowledge%20you%20have%20learned%20in%20this%20article%20to%20reduce%20the%20amount%20of%20actions%20and%20loops%20in%20your%20flows%20in%20Power%20Automate%20today.%20As%20an%20extra%20bonus%20the%20same%20first()%20function%20can%20also%20be%20used%20in%20Azure%20Logic%20Apps%20which%20names%20it%E2%80%99s%20loop%20For%20each.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2190265%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20this%20blog%20post%20I%20will%20show%20you%20how%20to%20avoid%20creating%20unnecessary%20apply%20to%20each%20loops%20in%20Power%20Automate%20which%20can%20slow%20down%20your%20flow%20and%20make%20it%20harder%20to%20read.%20This%20often%20happens%20when%20you%20want%20to%20retrieve%20just%20one%20record%20in%20a%20SharePoint%20list%20so%20you%20would%20use%20the%20Get%20Items%20action%20with%20a%20query%20(ODATA%20filter%20query)%20and%20you%20know%20it%20should%20only%20return%20one%20item.%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Co-Authors
Version history
Last update:
‎Mar 06 2021 08:13 AM
Updated by: