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-
- 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
- Open the Power Automate Portal. Click on "+ Create", select "Instant cloud flow".
- Give you flow a suitable name. and select the appropriate trigger condition. For demo purpose, I am selecting "Manually trigger a flow".
- Initialize a variable "strItemIDs" of type String. It will hold the comma separate IDs.
- Now, I will initialize 4 more variables-
- arrItemIDs - It will hold the ItemIDs in array format using split function over strItemIDs.
- intTotalIDsCount - It will hold the total number of IDs present in arrItemIDs using length function.
- intBatchSize - It is a number which represents the number of items we will extract from the arrItemIDs
- arrItemsBatch - it will hold the IDs extracted from arrItemIDs in one batch. The logic for this will be discussed later in this post.
- We will add one more variable named "intSkipCount" and set it to 0.
- Now, we will add an action called Do-until. It will continue to execute until the intSkipCount becomes equal to or greater than intTotalIDsCount.
- During each iteration, it will fetch the desired records set and assign it to arrItemsBatch. Then increment the intSkipCount with intBatchSize.
- Below are the series of screenshots-
- Here the expression used is-
take(skip(variables('arrItemIDs'),variables('intSkipCount')),variables('intBatchSize'))
- 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.
- Initially, intSkipCount was set to 0, so we received first 50 items. Now see the next screenshot.
- After getting the first set of items, we are incrementing the intSkipCount with BatchSize (which is 50 in our case).
- 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.
- The loop will continue to trigger until inSkipCount becomes equal to or greater than intTotalIDsCount.
- 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.
- This is the Take & Skip function works. Belos are the screenshots of execution.
- Below is the outcome of all batches.
- 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.