Power Automate provides the "take" function to grab items from the front of a collection. It works with both arrays and strings, making it more versatile than its counterpart, the "skip" function. Where "skip" removes items from the beginning, "take" keeps them and throws away the rest.
Usage
It follows a simple pattern.
- Collection (string or array)
- Count (positive integer - the number of items to take from the front)
With arrays
Here's a basic example:
take(createArray(0, 1, 2, 3, 4), 3)
will return
[0, 1, 2]
It grabbed the first 3 items and discarded the rest.
With strings
Unlike most collection functions, "take" also works on strings. It returns the first N characters:
take('hello', 3)
will return
'hel'
This makes it useful for truncating text or extracting prefixes.
Getting the last N items
The "take" function works from the front, but you can combine it with the "reverse" function to grab items from the end:
reverse(take(reverse(variables('MyArray')), 3))
This reverses the array, takes the first 3 items, then reverses again to restore the original order. If you don't care about maintaining the original order, you can skip the outer reverse.
Pair with skip for slicing
Together with the "skip" function, you can extract any portion of an array. To get items at positions 3 through 5:
take(skip(variables('MyArray'), 2), 3)
This skips the first 2 items, then takes the next 3. It's the Power Automate equivalent of array slicing.
Edge Cases
Count exceeds collection length
If you try to take more items than the collection contains, you get the entire collection - no error:
take(createArray(1, 2, 3), 10)
will return
[1, 2, 3]
Same with strings:
take('hi', 10)
will return
'hi'
This is good to know because it means you don't need to check the length before using "take."
Count of zero
Taking zero items returns an empty array or an empty string:
take(createArray(1, 2, 3), 0)
will return
[]
Empty collections
Taking items from an empty array returns an empty array. Taking from an empty string returns an empty string.
Negative count
The count parameter must be a positive integer. Passing a negative number will cause an error.
Limitations
No "take from end" parameter
The function always takes from the front. If you need the last N items, you'll need to combine it with "reverse" or calculate the skip count using the "length" function and the "sub" function:
skip(variables('MyArray'), sub(length(variables('MyArray')), 3))
This skips everything except the last 3 items.
Return type matches input type
When you pass a string, you get a string back. When you pass an array, you get an array back. This is generally what you'd expect, but it's worth noting since the "skip" function only works with arrays.
Recommendations
Here are some things to keep in mind.
Use it to remove the last item
To get all items except the last one, combine "take" with "length" and "sub":
take(variables('MyArray'), sub(length(variables('MyArray')), 1))
This is the cleanest pattern for trimming the end of an array.
Use the "substring" function for truncation
When you need to limit a string to a certain number of characters (for example, fitting text into a field with a character limit), the "substring" function is the better approach:
substring(variables('LongText'), 0, 100)
Combine with skip for pagination
For pagination, use "skip" and "take" together:
take(skip(variables('AllItems'), mul(variables('PageNumber'), variables('PageSize'))), variables('PageSize'))
This skips to the right page offset and takes one page's worth of items.
Always add a comment
Adding a comment will help others understand your formula. Indicate what the function is doing and why, especially if the expression is complex.
Final Thoughts
The "take" function is a straightforward way to grab items from the front of arrays and strings in Power Automate. It pairs naturally with "skip" for slicing and pagination, and its ability to work with strings makes it uniquely versatile among the collection functions. Just remember it always works from the front, and you'll find plenty of uses for it.
Sources
Microsoft's "take" Function Reference
Back to the Power Automate Function Reference
Photo by the blowup on Unsplash
No comments yet
Be the first to share your thoughts on this article!