First, FirstN, Index, Last, and LastN functions in Power Apps
a) First function
The First power apps function returns the first record of a table. Power Apps Syntax First(Table)
Any tabular datasource is given as an argument to this function.
Example:Step 1: Select the Insert tab in the Canvas app and add a Button control, and set its OnSelect property to the following powerapps formula:
Power Apps Formula
{Name: "Ashish" , Country: "India", Profession: "Engineer"},
{Name: "Deepak", Country: "America", Profession: "Engineer"},
{Name: "Anjali", Country: "India", Profession: "Doctor"},
{Name: "Ayushman", Country: "America", Profession: "Lawyer"}
)
Step 2: Add a Vertical gallery control, select MyNewCollection as the data source.
![First, Last and Index powerapps functions First, Last and Index powerapps functions](media/first-function-01.webp)
Step 3: The following formula returns the first record from the data source.
Power Apps Formula
![First, Last and Index powerapps functions First, Last and Index powerapps functions](media/first-function-02.webp)
b) FirstN function
The FirstN function returns the first set of records of a table; the second argument specifies the number of records to return.
Power Apps Syntax FirstN( Table [, NumberOfRecords ] )
The function takes the following arguments:
- Table – Required. It specifies the table.
- NumberOfRecords - Optional. It specifies the number of records to return.
Note: If we don't specify the NumberOfRecords argument, the function returns only one record. And if we specify the number more than the actual records in the table, then it returns all the records.
Example:Step 1: Select the + Insert tab in the Canvas app and add a Button control, and set its OnSelect property to the following powerapps formula:
Power Apps Formula
{Name: "Ashish" , Country: "India", Profession: "Engineer"},
{Name: "Deepak", Country: "America", Profession: "Engineer"},
{Name: "Anjali", Country: "India", Profession: "Doctor"},
{Name: "Ayushman", Country: "America", Profession: "Lawyer"}
)
This creates a collection named MyNewCollection.
Step 2: Add a Vertical gallery control, select MyNewCollection as the data source.
![First, Last and Index powerapps functions First, Last and Index powerapps functions](media/firstn-function-03.webp)
Step 3: The following formula returns a table containing the first two records from the data source.
Power Apps Formula
![First, Last and Index powerapps functions First, Last and Index powerapps functions](media/firstn-function-04.webp)
Note: The FirstN function is not delegable when use with Sharepoint list as a datasource.
c) Last function
The Last function returns the last record of a table.
Power Apps Syntax Last(Table)
The function takes the following argument:
- Table - Required. Table to operate on.
For Example:Step 1: Select the Insert tab in the Canvas app and add a Button control, and set its OnSelect property to the following formula:
Power Apps Formula
{Name: "Ashish" , Country: "India", Profession: "Engineer"},
{Name: "Deepak", Country: "America", Profession: "Engineer"},
{Name: "Anjali", Country: "India", Profession: "Doctor"},
{Name: "Ayushman", Country: "America", Profession: "Lawyer"}
)
This creates a collection named MyNewCollection.
Step 2: Add a Vertical gallery control, select MyNewCollection as the data source.
![First, Last and Index powerapps functions First, Last and Index powerapps functions](media/last-function-05.webp)
Step 3: The following formula returns the last record from the data source.
Power Apps Formula
![First, Last and Index powerapps functions First, Last and Index powerapps functions](media/last-function-06.webp)
d) LastN function
The LastN function returns the last set of records of a table; the second argument specifies the number of records to return.
Power Apps Syntax LastN( Table [, NumberOfRecords ] )
The function takes the following arguments:
- Table - Required. It specifies any tabular datasource (including collections).
- NumberOfRecords - Optional. It specifies the number of records to return. If we don't specify this argument, the function returns only one record.
For Example:Step 1: Select the Insert tab in the Canvas app and add a Button control, and set its OnSelect property to the following formula:
Power Apps Formula
{Name: "Ashish" , Country: "India", Profession: "Engineer"},
{Name: "Deepak", Country: "America", Profession: "Engineer"},
{Name: "Anjali", Country: "India", Profession: "Doctor"},
{Name: "Ayushman", Country: "America", Profession: "Lawyer"}
)
This creates a collection named MyNewCollection.
Step 2: Add a Vertical gallery control, select MyNewCollection as the data source.
![First, Last and Index powerapps functions First, Last and Index powerapps functions](media/lastn-function-07.webp)
Step 3: The following formula returns a table containing the first two records from the data source.
Power Apps Formula
![First, Last and Index powerapps functions First, Last and Index powerapps functions](media/lastn-function-08.webp)
e) Index function
The Index powerapps function returns a record of a table based on its ordered position in the table.
Power Apps Syntax Index(Table, RecordIndex)
The function takes the following parameters:
- Table - Required. Table to operate on.
- RecordIndex - Required. The index of the record to return. Record numbering begins with 1.
Note: • The Index function returns an error if the requested record index is less than 1, greater than the number of records in the table, or the table is empty.
• The First function, First(table) returning the same record as Index(table, 1).
Example:Step 1: Select the + Insert tab in the canvas app and add a Button control, and set its OnSelect property to the following powerapps formula:
Power Apps Formula
{Name: "Ashish" , Country: "India", Profession: "Engineer"},
{Name: "Deepak", Country: "America", Profession: "Engineer"},
{Name: "Anjali", Country: "India", Profession: "Doctor"},
{Name: "Ayushman", Country: "America", Profession: "Lawyer"}
)
This creates a collection named MyNewCollection .
Step 2: Add a Vertical gallery control, select MyNewCollection as the data source.
![First, Last and Index powerapps functions First, Last and Index powerapps functions](media/index-function-09.webp)
Step 3: The following formula returns the second record from the data source.
Power Apps Formula
![First, Last and Index powerapps functions Index Function in Power Apps](media/index-function-10.webp)
When the Record index is out of range it returns an error.
Power Apps Formula
![First, Last and Index powerapps functions Index Function in Power Apps](media/index-function-11.webp)
We can also extract the particular column value once we extract the record. Add a Text label control to the canvas.
![Index Function in Power Apps Index Function in Power Apps](media/index-function-12.webp)
Note: First, Index, and Last return a single record. FirstN and LastN return a table, even if we specify only a single record.