How to Filter an Array by Index in Power Automate?

One of my team members asked about filtering an array by Index in Power Automate. Unfortunately, Power Automate does not have a built-in option to get the index of an item within an array. But there are ways to do this.

In this tutorial, I will show you how to filter an array by index using Power Automate with a few more examples:

  • Filter Array by Specific Indexes using Power Automate
  • Filter the First item from an Array by Index value in Power Automate
  • Get the Last Item in an Array using a Filter Array Index in Power Automate

Filter Array Based on Specific Index in Power Automate

Suppose you have an array with multiple objects as shown in the code below. You want to filter the multiple array objects based on a specific index.

Let’s assume you want to filter out objects 1 and 3 from the array.

[
  {
    "TaskID": "T001",
    "TaskName": "Create Power Platform PDF",
    "Status": "Pending"
  },
  {
    "TaskID": "T002",
    "TaskName": "Develop Site Page",
    "Status": "Pending"
  },
  {
    "TaskID": "T003",
    "TaskName": "Create a Web Part",
    "Status": "In Progress"
  },
  {
    "TaskID": "T003",
    "TaskName": "Create a Site Page",
    "Status": "Completed"
  }
]

You can achieve this by using the steps below in Power Automate.

  1. Create a Power Automate Instant cloud flow that can be triggered manually by clicking a button.
  2. After that, add an “Initialize variable” action to store the array value. Enter a name for a variable, take the type as an array, and a value.
Filter an Array by Index using Power Automate

Power Automate does not have a built-in way to get the index of an item from any array. So, we need to create an index manually to filter the array items based on specific indices.

  1. For that, add one more “Initialize variable” action that will assign the index to each item in the array. Set the parameters as below.
    • Name: Provide a name for a variable. I have named it “index”.
    • Type: Select type as ‘Integer’.
    • Value: Set the value to ‘0’.
Filter an Array by Index Power Automate
  1. Then, take another “Initialize variable” action with an empty array. This will store the array items with the appended indexes. Provide the parameters, such as Name, Type (as Array), and Value, as shown below.
Power Automate Filter an Array by Index
  1. Then add the “Apply to each loop” action to loop through each item in an array variable. Select an output from the previous steps to: Initialize variable [Tasks].
Power Automate Filter Array by Index
  1. Inside the loop, add an “Append to array variable” action to append the index value to each item when the loop runs each time. Set the properties:
    • Name: Select the Empty Array variable from the previous steps [VarIndexedArray].
    • Value: Insert the below expression.
{
  "Task": "@{item()?['TaskName']}",
  "Task Status": "@{item()?['Status']}",
  "index": "@{variables('index')}"
}
Microsoft Power Automate Filter Array Index
  1. Next, add an “Increment variable” action, which will increment the variable count of the index. Select [index] variable from options and Value to ‘1‘.
Power Automate filter from an array index

At this point, the Indexed array will look like the following format:

[
    {
        "Task": "Create Power Platform PDF",
        "Task Status": "Pending",
        "index": "0"
    },
    {
        "Task": "Develop Site Page",
        "Task Status": "Pending",
        "index": "1"
    },
    {
        "Task": "Create a Web Part",
        "TaskStatus": "In Progress",
        "index": "2"
    },
    {
        "Task": "Create a Site Page",
        "Task Status": "Completed",
        "index": "3"
    }
]
  1. Now, let’s add a “Filter Array” action outside of the loop, which will filter the items with indices 1 and 3. Make sure to set the parameters.
    • From: Choose to add the Initialize variable with an empty array [VarIndexedArray].
    • Filter Query: Click on Edit in advanced mode -> Provide the below expression.
or(equals(int(item()?['index']), 1), equals(int(item()?['index']), 3))
Filter Array Based on Specific Index in Power Automate

The flow is ready now. Let’s save and run it manually.

  1. When the flow runs successfully without any errors, open and check the Filter Array Outputs. It will show the Array objects whose index values are 1 and 3.
Power Automate Filter an Array Specific Index

This is how to filter items in an array based on specific indexes using Power Automate.

Check out Filter an Array by Multiple Conditions in Power Automate

Get the First Item from an Array by Index in Power Automate

To illustrate this example, I will use the array mentioned above, where I want to filter the first item from the given array.

From the image below, you can see the Array Input. The highlight mark represents the first item of an array.

Power Automate filter array by index value

You can follow the same steps mentioned in the above example, but change the Filter Query expression in the Filter Array action.

The index of an array item starts with Zero. To get the first item from an array by index value, use the expression present in the table below.

Choose ValueOperatorChoose Value
int(item()?[‘index’])is equal to0
Filter First Item in Array Power Automate

This will filter and get the first item from any array of objects using the Filter Array action in Power Automate.

Save and run the flow.

Output:

Filter Array action in Power Automate

Check out How to Filter an Array by Another Array in Power Automate?

Get the Last Item from an Array by Index using Filter Array in Power Automate

You can also get the last item from any array with the help of an index position using the filter array action in Power Automate.

The image below shows the Array with multiple objects. The highlighted item represents the last item of an array.

Power Automate filter last item from array by index

Follow the same steps mentioned in Example 1, and then update the filter query expression as shown below.

  1. Add a “Filter Array” action and set the required parameters.
    • From: Take the VarIndexedArray variable from the previous action.
    • Filter Query: Insert the below expression.
Choose ValueOperatorChoose Value
int(item()?[‘index’])is equal tosub(length(variables(‘VarIndexedArray’)),1)

Since the array is zero-indexed, subtract one from the length of the array.

Power Automate select item from array by index

Output:

Take a look at the reference image, which displays the last item from an Array.

Power Automate filter item from array by index

I recommend using the first() and last() functions if you only want to retrieve the first and last items from an array, rather than using the Filter array action.

In this tutorial, you have learnt how to filter an array by indexes in Power Automate using the filter array action.

I hope you found this article helpful. Do mention your suggestions or comments in the comments section.

You may like the following Power Automate tutorial:

>

Build a High-Performance Project Management Site in SharePoint Online

User registration Power Apps canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

Power Platform Tutorial FREE PDF Download

FREE Power Platform Tutorial PDF

Download 135 Pages FREE PDF on Microsoft Power Platform Tutorial. Learn Now…