Tuesday, August 30, 2022

Power Automate: Use of TAKE & SKIP Function

Hello Friends,

Welcome back with another post on Power Automate. Today, we will discuss about 2 amazing functions provided by Power Automate. These are "TAKE" and "SKIP".

The purpose of "TAKE" function is to pick the first n values from the provided array / string.

The purpose of "SKIP" function is to skip the first n values from the provided array / string.

These 2 are amazingly useful when working with arrays. Let's see-

  1. I am having a bulk array of Item IDs and I have to process this array in chunks. Here, these 2 functions show their expertise. These works helps when sending bulk IDs from PowerApps to Power Automate for processing
  2. Open the Power Automate Portal. Click on "+ Create", select "Instant cloud flow".
  3. Give you flow a suitable name. and select the appropriate trigger condition. For demo purpose, I am selecting "Manually trigger a flow".
  4. Initialize a variable "strItemIDs" of type String. It will hold the comma separate IDs.
  5. Now, I will initialize 4 more variables-
    1. arrItemIDs - It will hold the ItemIDs in array format using split function over strItemIDs.
    2. intTotalIDsCount - It will hold the total number of IDs present in arrItemIDs using length function.
    3. intBatchSize - It is a number which represents the number of items we will extract from the arrItemIDs
    4. arrItemsBatch - it will hold the IDs extracted from arrItemIDs in one batch. The logic for this will be discussed later in this post.
  6. We will add one more variable named "intSkipCount" and set it to 0.
  7. Now, we will add an action called Do-until. It will continue to execute until the intSkipCount becomes equal to or greater than intTotalIDsCount.
  8. During each iteration, it will fetch the desired records set and assign it to arrItemsBatch. Then increment the intSkipCount with intBatchSize.
  9. Below are the series of screenshots-
  10. Here the expression used is-
    1. take(skip(variables('arrItemIDs'),variables('intSkipCount')),variables('intBatchSize'))
      
  11. This is what the magic of Take & Skip functions is. the Skip function is skipping the first "n" number of records / characters (depends upon, what you are passing to it- array / string) and returning the remaining array/string. Then Take function is using that result and extracting the "m" number of records / characters and giving us as an output.
  12. Initially, intSkipCount was set to 0, so we received first 50 items. Now see the next screenshot.
  13. After getting the first set of items, we are incrementing the intSkipCount with BatchSize (which is 50 in our case).
  14. Now the loop will trigger again. This time, the Skip function will skip first 50 items so the result of skip function will be items starting for 51 to 290. This will be used as input by Take function and it will give first 50 items, means 51 -100.
  15. The loop will continue to trigger until inSkipCount becomes equal to or greater than intTotalIDsCount.
  16. The most important point here is that the last batch will be having only 40 items (251 - 290). However, the Take function was written to extract the 50 items. Ideally it should throw error (like in Substring function). But NO, it is smart enough to handle this situation and returning us the items whatever left there.
  17. This is the Take & Skip function works. Belos are the screenshots of execution.
  18. Below is the outcome of all batches.
  19. This is how, we can leverage the features of Take & Skip functions in Power Automate.
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.