How to get an item from an array, collection or table by index in Power Apps canvas app

Welcome to my blog and here I am back with another blog on Power Apps. Today I am going to write about a fairly new feature, a feature awaited from a pretty long time.

Did you ever had the following requirements in Canvas apps?

  • Get an array item at a particular index
  • Get an item at a particular index from a collection
  • Get a row from a table at a specified index

If you have any of the above requirement, you are on the right blog. The first question is – Was this not possible to do all this while?

Yes it was possible. Let’s see the formula we used all this while to retrieve the second item from a collection. Below is a sample collection.

Collect(
    sampleCollection,
    {
        Name: "Debajit",
        Role: "Architect"
    },
    {
        Name: "Chandana",
        Role: "Developer"
    }
)

Let’s say we have a requirement to get the second item in the collection. To do that, traditionally we will use the below formula.

Get item at a specified index from an array, collection or table

Not the best way right? If we need to get the second row of accounts table in DataVerse, we will use the below formula. Also notice the formula may not yield expected result. For example, I have only one account record and although my expectation is for the second row of the table, the below formula will be return me the first record.

Get item at a specified index from an array, collection or table

Microsoft have finally introduced the Index function to retrieve a record based on index for an array, collection or table.

So now if I want to retrieve the second item from my previous collection, we shall now use the below formula.

$"Second Item: {Index(sampleCollection,2).Name}"

And to retrieve a row by number, we use the below formula. Observe I am not getting any data when I try to retrieve the second row from the accounts table, which is as expected.

Get item at a specified index from an array, collection or table

Wonderful isn’t it? So no more using Last and FirstN functions. Make use of the Index formula.

Hope this helped. You will also like the below posts.

Debajit Dutta
Business Solutions MVP