Hello Friends,
Welcome back with another post on Power Automate. Today, we will discuss about another amazing function provided by Microsoft. This function is "SELECT". Let's start.
- The scenario is, you have to get the items from a SharePoint list based upon the IDs provided. These IDs are sent to the flow as comma separated. Usually, what we do is we convert these comma separate IDs into an array, create a filter query using "Apply to each" loop and then use Get Items action. Now, the same we will do using Select action.
- We had created a list named TestListSJ.
- This list has 25700 items starting from ID 401 upto 26100. We will use IDs from 401 upto 500 means 100 items.
- Let's create a flow in Power Automate.
- We are creating a "Manually trigger a flow" with name as "POC-SelectFunction".
- Click on Create.
- Declare a string variable "strItemIDs" to hold the comma separated Item IDs. Assign the comma separated IDs from 401 to 500.
- Now, we will initialize another variable "arrItemIDs" of type array which will hold these IDs in array form.
split(variables('strItemIDs'),',')
- We will define another variable "intTotalItemIDsCount" of type Integer to capture the total count of Item IDs received.
length(variables('arrItemIDs'))
- From here, the actual logic starts. Now, we will use "Select action and convert each item ID into individual filter query like from 401 to (ID eq 401).
- Click on the arrow marked icon. It will switch the Mapping window to basic mode.
- Now add the "Concat" function as below.
concat('(ID eq ',item(),')')
- This concat function will convert each item id into individual filter query.
- Keep saving the flow frequently.
- Now we need to combine all these filter queries into one single string, separated with " or ". For this we will use "Compose" action and in compose action, we will use the "join" function.
join(body('Select_-_Individual_Filter_Query'),' or ')
- This is what we were looking for.
- Now, add "Get Items" action.
- Select the Site Address, List Name, click on "Show advanced options". In "Filter Query", select the output of Final Query Compose action.
- That's all we done.
- Now save the flow and Test it.
- If you see the execution time of each action. It all 0. It means they all individually too less than a second to execute.
- This is the power of "Select" action. It reduce the execution time. Now, to prove that, we will use tradition method in parallel as well. We will also capture Start/End time for each method and then we will compare the time consumption.
- For this, we will add a parallel branch just after the "Initialize variable - intTotalItemIDsCount" action. This variable will be named as "strCombinedFilterQuery".
- Add "Apply to each" action on the arrItemIDs. Inside the Apply to each action add another action called "Append to string variable". Here we will append the individual filter query to the variable "strCombinedFilterQuery"
concat('(ID eq ',item(),') or ')
- This step will give us a string like "(ID eq 401) or (ID eq 402) or ... (ID eq 500) or ". As we see, there is an extra " or " at the end, we need to scrap it out.
- We will add a Compose action and use "substring" function to scrap the last " or " piece.
substring(variables('strCombinedFilterQuery'),0,add(lastIndexOf(variables('strCombinedFilterQuery'),')'),1))
- Now, use the Get Items action to get the filtered records.
- Save the flow and test it again.
- If you see, the traditional approach took 26 seconds to prepare the filter query itself.
- Now, if we see the Run history.
- Here, we will find that the "Select" approach took only 31 milliseconds to complete the process, while the tradition approach took 27 seconds. It means the Select approach is 81 times faster than the traditional approach.
- This select approach is surprisingly useful, when you have to process data. The traditional approach will again add a "Apply to each" loop, while the same work will be done by Select action in fraction of time.
- For example, here after getting the data, if I have to add a column where, I need to fill in-
- "Apple" if GUID starts with "a"
- "Banana" if GUID starts with "b"
- "Citrus" if GUID starts with "c"
- "Others" for all other GUIDs
- Then I need to create HTML of this output. So, what I will do in traditional approach is, I will initialize a string variable to hold the HTML data.
- Then, I will use "Apply to each". In "Apply to each" either I will use the Condition (If-Else) or Switch-Case action. In that action, I will check the first character of GUID and based upon that character, I will append HTML code in the declared string.
- At the end, I will add another Append to String variable to complete the HTML.
- So, the final sequence in Traditional way is
- For new approach, I will use the Select action and add column "Flavour". In mapping, I will write the logic.
if( equals(toUpper(first(item()?['GUID0'])),'A'), 'Apple', if( equals(toUpper(first(item()?['GUID0'])),'B'), 'Banana', if( equals(toUpper(first(item()?['GUID0'])),'C'), 'Citrus', 'Others' ) ) )
- Lastly, I will add the Create HTML Table action and provide the output of previously added Select action as the input here.
- Now, Save the flow and execute it.
- Again, if you see, the new approach completed it process in less than a minute. While the traditional approach took around a minute to get the same completed.
- If we verify the same, this comes out to be "0" seconds (means less than a second) for New approach and 55 seconds for Traditional approach.
- Thus, you can see, how powerful is the "Select" action. So, friends, wherever possible, please use Select action and try to avoid "Apply to each" or "Switch" actions.
With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.
Stay Safe !
Stay Healthy !
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.