Introduction
In this blog post, we'll cover the "FirstN" and "LastN" functions in PowerApps which are used to retrieve a specified number of records from a data source or a collection, but they operate differently in terms of the records they return.
Both functions take two parameters
- The data source or collection from which records are to be retrieved.
- The number of records to retrieve.
FirstN Function
The "FirstN" function is used to retrieve a specified number of records from a data source or a collection. starting from the first record in the data source or collection.
Syntax
FirstN(DatasourceName,numberofitems)
Here we have a collection named 'CarOwners', which has 3 userdata saved in the collection so here FirstN function can be used here to get the first 2 records or 1 record in ascending order.
Collection
ClearCollect(CarsOwners,
{ ID: 1, Name: "John", Carname: "Ford" },
{ ID: 2, Name: "Emma", Carname: "Maruti Suzuki" },
{ ID: 3, Name: "Michael", Carname: "BMW" }
)
Here I have added a blank vertical gallery and connected my collection as a data source.
Example
FirstN(CarOwners, 2)
Output
LastN Function
The "LastN" function in PowerApps is used to retrieve a specified number of records from a data source or a collection, starting from the last record in the data source or collection
Syntax
LastN(DatasourceName,numberofitems)
Example
LastN(CarOwners, 2)
Output
Conclusion
This function is useful when you need to display the most recent records in a gallery or any other control in your PowerApp.