How to Skip the First or Last Item in an Array in Power Automate

Do you want to skip the first or last item while looping through an array in your flow?

When working with arrays in Power Automate, it can be required to skip the first or last item in the loop.

One potential example could be when working with a list of employees and their salaries. The first or last item in the array might be a summary total for all salaries, and it may not be necessary to include this in the loop.

By skipping over the first or last item, the loop can focus solely on the relevant data, resulting in an efficient process that doesn’t fail due to an unexpected total row.

This functionality can be useful in any situation where the first or last item in the array needs to be processed differently than the rest.

This post will show you how to exclude the first or last item in an array from being processed in a loop.

Skip the First Item in an Array

[ 162900, 50400, 45000, 67500 ]

Here is an array of salary numbers with the first item representing a total of the following items.

Suppose you want to exclude this first item in your loop since it’s a total and not a salary.

Here is how to skip the first item in an array.

  1. Add the Apply to each step to your flow.
  2. Select the input field in the Apply to each step.
  3. Click on the Expression tab in the pop-up that appears when the input field in selected.
skip(outputs('Compose'),1)
  1. Add the above expression to the formula editor, where outputs('Compose') is the reference to the array.
  2. Press the Update button.

This uses the skip function to remove the first item in the array. The skip function has a second argument which is the number of items to remove from the array.

In this example, the second argument is 1 because you want to skip only the 1st item from the array. But you can set this to any number if you need to skip more than the first item in your array.

Now that you have your loop that will skip the first item from the array, you will likely need to reference the current item within each loop.

This can be achieved with the items function. It will get the current item in the loop.

items('Apply_to_each')

The above expression will get the current array value from the Apply to each loop.

You should also find this as the Current item dynamic content when you select any input field inside the Apply to each loop.

Skip the Last Item in an Array

[ 50400, 45000, 67500, 162900 ]

Here is an array of salary numbers with the last item representing the total of the previous items. In this example, you want to exclude the last item in your loop.

Here is how to skip the last item in the array.

  1. Add the Apply to each step to your flow.
take(outputs('Compose'), sub(length(outputs('Compose')),1))
  1. Add the above expression to the Apply to each input. Here outputs('Compose') is the reference to the array to remove the last item from.

Unfortunately, there is no function similar to the skip function that will allow you to remove a given number of items from the end of an array.

The above expression uses the take function to return only the first number of items from the array.

The number of items to return is calculated by finding the total number of items in the array with the length function and subtracting 1 from the result with the sub function.

skip(reverse(outputs('Compose')),1)

Another option is the above expression. It will reverse the order of the array items so the last item is now the first item. This way you can use the skip function to remove the first item.

reverse(skip(reverse(outputs('Compose')),1))

If the order of the loop and array items is important. You can use the reverse function again to restore the order without the last item.

Again, you can access each of the items from the array without the last element by using the Current item dynamic content.

Conclusions

You might need to skip the first or last item from an array in your loop.

This is possible by using expressions to remove the first or last item from your array when referencing the array in a Apply to each step of your flow.

Using these techniques, you can ensure that your flow only processes the relevant data from your array. This can make your process more efficient and less prone to errors.

Have you needed different logic for the first or last items in your loop? How did you get this done? Let me know in the comments!

About the Author

John MacDougall

John MacDougall

John is a Microsoft MVP and freelance consultant and trainer specializing in Excel, Power BI, Power Automate, Power Apps and SharePoint. You can find other interesting articles from John on his blog or YouTube channel.

Related Articles

Comments

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Get the Latest Tech Tips

Write For Us

Are you a tech enthusiast with a talent for writing great content? Come write for us!

Follow Us

Follow us on social media to stay up to date with the latest in tech!