Wednesday, August 31, 2022

Power Automate: Use Of SELECT Function

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.

  1. 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.
  2. We had created a list named TestListSJ.
  3. This list has 25700 items starting from ID 401 upto 26100. We will use IDs from 401 upto 500 means 100 items.
  4. Let's create a flow in Power Automate.
  5. We are creating a "Manually trigger a flow" with name as "POC-SelectFunction".
  6. Click on Create.
  7. Declare a string variable "strItemIDs" to hold the comma separated Item IDs. Assign the comma separated IDs from 401 to 500.
  8. Now, we will initialize another variable "arrItemIDs" of type array which will hold these IDs in array form.
    1. split(variables('strItemIDs'),',')
      
  9. We will define another variable "intTotalItemIDsCount" of type Integer to capture the total count of Item IDs received.
    1. length(variables('arrItemIDs'))
      
  10. 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).
  11. Click on the arrow marked icon. It will switch the Mapping window to basic mode.
  12. Now add the "Concat" function as below.
    1. concat('(ID eq ',item(),')')
      
  13. This concat function will convert each item id into individual filter query.
  14. Keep saving the flow frequently.
  15. 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. 
    1. join(body('Select_-_Individual_Filter_Query'),' or ')
      
  16. This is what we were looking for.
  17. Now, add "Get Items" action.
  18. Select the Site Address, List Name, click on "Show advanced options". In "Filter Query", select the output of Final Query Compose action.
  19. That's all we done.
  20. Now save the flow and Test it.
  21. If you see the execution time of each action. It all 0. It means they all individually too less than a second to execute.
  22. 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.
  23. For this, we will add a parallel branch just after the "Initialize variable - intTotalItemIDsCount" action. This variable will be named as "strCombinedFilterQuery".
  24. 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" 
    1. concat('(ID eq ',item(),') or ')
      
  25. 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.
  26. We will add a Compose action and use "substring" function to scrap the last " or " piece.
    1. substring(variables('strCombinedFilterQuery'),0,add(lastIndexOf(variables('strCombinedFilterQuery'),')'),1))
      
  27. Now, use the Get Items action to get the filtered records.
  28. Save the flow and test it again. 
  29. If you see, the traditional approach took 26 seconds to prepare the filter query itself.
  30. Now, if we see the Run history.
  31. 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.
  32. 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.
  33. For example, here after getting the data, if I have to add a column where, I need to fill in-
    1. "Apple" if GUID starts with "a"
    2. "Banana" if GUID starts with "b"
    3. "Citrus" if GUID starts with "c"
    4. "Others" for all other GUIDs
  34. 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.
  35. 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.
  36. At the end, I will add another Append to String variable to complete the HTML.
  37. So, the final sequence in Traditional way is
  38. For new approach, I will use the Select action and add column "Flavour". In mapping, I will write the logic.
    1. 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'
      		)
      	)
      )
      
  39. Lastly, I will add the Create HTML Table action and provide the output of previously added Select action as the input here.
  40. Now, Save the flow and execute it.
  41. 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.
  42. 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.
  43. 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.