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 Name | Data Type |
ProductName | Text |
PurchaseDate | Date and time |
Price | Number |
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.
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
2. To get a created collection, click on the App’s Run OnStart property as shown below.
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
4. Now, insert a Gallery control and set its Items property code as:
Items = colFirstItem
Where,
- colFirstItem = Power Apps Collection Name
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.
This is how to get the first item in the Power Apps collection.
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
2. Now, select Gallery control and set its Items property as:
Items = colFirstNItems
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.
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
2. Then, select a Gallery control and set its Items property as:
Items = colLastItem
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.
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
2. Next, select a Gallery control and set its Items property as:
Items = colLastNItems
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.
This is how to get the last N items in the Power Apps collection.
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:
- Power Apps Save Collection to SharePoint List
- Remove Items From Power Apps Collection
- Count Rows in Power Apps Collection
- Sort Power Apps Collection Alphabetically
- How to Add Copilot to Power Apps
- How to Use Power Apps Collection GroupBy?
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com