How to Get First Item in Power Apps Collection?

Are you eagerly waiting to learn how to get the first item in the Power Apps collection? No worries, You are in the right place!

Follow this Microsoft Power Apps tutorial to learn all about how to get the first item in the Power Apps collection, including:

  • Get First ‘N’ Items in Power Apps Collection
  • How to Get Last Item in Power Apps Collection
  • Working with Getting Last ‘N’ Items in Power Apps Collection

Get First Item in Power Apps Collection

Here, we will discuss how to get the first item in the Power Apps collection with a simple scenario:

Scenario:

I have a Power Apps collection named “colProducts” containing the fields below.

Column NameData Type
ProductNameText
PurchaseDateDate and time
PriceNumber
Get First Item in Power Apps Collection

Now, I want to get the first item in the Power Apps collection and display it on the gallery control, as in the screenshot below.

How to Get the First Item in Power Apps Collection

To work around this, follow the below-mentioned steps. Such as:

1. To create a Power Apps collection, select the App object [from left navigation] and set its OnStart property to the code below.

OnStart = ClearCollect(
    colProducts,
    {
        ProductName: "Computer",
        PurchaseDate: "5/12/2023",
        Price: 15000
    },
    {
        ProductName: "Mobile",
        PurchaseDate: "6/12/2023",
        Price: 12000
    },
    {
        ProductName: "Refrigerator",
        PurchaseDate: "6/12/2023",
        Price: 18000
    },
    {
        ProductName: "Watch",
        PurchaseDate: "7/12/2023",
        Price: 1000
    },
    {
        ProductName: "Tablet",
        PurchaseDate: "8/12/2023",
        Price: 11000
    },
    {
        ProductName: "Air conditioning",
        PurchaseDate: "8/12/2023",
        Price: 14000
    }
)

Where,

  • colProducts = Power Apps Collection Name
  • ProductName, PurchaseDate, Price = Collection Headers/Columns
  • “Computer”, “5/12/2023”, 15000 = Collection Items/Records
Get the First Item in Power Apps Collection

2. To get a created collection, click on the App’s Run OnStart property as shown below.

Get the First Item in the Power Apps Collection

3. Then, insert a Button control and set its OnSelect property to the code below.

OnSelect = ClearCollect(
    colFirstItem,
    First(colProducts)
)

Where,

  • colFirstItem = Power Apps Collection Name
  • First() = This function returns the first record of a table
  • colProducts = Power Apps Source Collection
Get First Item in the Power Apps Collection

4. Now, insert a Gallery control and set its Items property code as:

Items = colFirstItem

Where,

  • colFirstItem = Power Apps Collection Name
How to Get First Item in Power Apps Collection

5. Save, Publish, and Preview the app. When the user clicks the button control, the gallery control will display the first collection item, as in the screenshot below.

How to Get First Item in the Power Apps Collection

This is how to get the first item in the Power Apps collection.

See also  Power Apps Toggle Control + 10 Examples

Get First ‘N’ Items in Power Apps Collection

1. Next, we will see how to get the first three items in the Power Apps collection. For that, select the Button control and set its OnSelect property to the code below.

OnSelect = ClearCollect(
    colFirstNItems,
    FirstN(colProducts,3)    //You can change the number of first items
)

Where,

  • colFirstNItems = Power Apps Collection Name
  • FirstN() = This Power Apps FirstN() function returns first set of records of a table
  • 3 = It is the number of the first items
Get First N Items in Power Apps Collection

2. Now, select Gallery control and set its Items property as:

Items = colFirstNItems
Get First N Items in the Power Apps Collection

3. Save, Publish, and Preview the app. When the user clicks the button control, the gallery control displays the first three collection items like the one below.

How to Get First N Items in the Power Apps Collection

This is how to get the first N items in the Power Apps collection.

Get Last Item in Power Apps Collection

1. Let’s see how to get the last item in the Power Apps collection. For that, select the Button control and set its OnSelct property to the code below.

OnSelect = ClearCollect(
    colLastItem,
    Last(colProducts)
)

Where,

  • colLastItem = Power Apps Collection
  • Last() = This function returns the last record of a table
Get Last Item in Power Apps Collection

2. Then, select a Gallery control and set its Items property as:

Items = colLastItem
Get Last Item in the Power Apps Collection

3. Save, Publish, and Preview the app. When the user clicks the button control, the gallery will display the last collection item, as in the screenshot below.

How to Get Last Item in Power Apps Collection

This is how to get the last item in the Power Apps collection.

Get Last ‘N’ Items in the Power Apps Collection

1. In the last, I will show you how to get the last four items in the Power Apps collection. For that, select a Button control and set its OnSelect property to the code below.

OnSelect = ClearCollect(
    colLastNItems,
    LastN(colProducts,4)    //You can change the number of first items
)

Where,

  • colLastNItems = Power Apps Collection Name
  • LastN() = The LastN() function returns the last set of records of a table
  • 4 = It is the number of the last number of items
Get Last N Items in Power Apps Collection

2. Next, select a Gallery control and set its Items property as:

Items = colLastNItems
Get Last N Items in the Power Apps Collection

3. Once your app is ready, Save, Publish, and Preview the app. The gallery control will display the last four collection items whenever the user clicks on the button control, as in the screenshot below.

How to Get Last N Items in Power Apps Collection

This is how to get the last N items in the Power Apps collection.

See also  8 Various Ways to Use The Patch Function in Power Apps

Conclusion

I trust this Power Apps tutorial taught in detail information about how to get the first item in the Power Apps Collection, including:

  • Get First ‘N’ Items in the Power Apps Collection
  • How to Get the Last Item in the Power Apps Collection
  • Working with Getting Last ‘N’ Items in Power Apps Collection

You may also like:

>