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.
Step 3: The following formula returns the first record from the data source.
Power Apps Formula
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 – It is the required parameter. 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.
Step 3: The following formula returns a table containing the first two records from the data source.
Power Apps Formula
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 - It is the required parameter. 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.
Step 3: The following formula returns the last record from the data source.
Power Apps Formula
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 - It is the required parameter. 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.
Step 3: The following formula returns a table containing the first two records from the data source.
Power Apps Formula
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 - It is the required parameter. Table to operate on.
- RecordIndex - It is the required parameter. 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.
Step 3: The following formula returns the second record from the data source.
Power Apps Formula
When the Record index is out of range it returns an error.
Power Apps Formula
We can also extract the particular column value once we extract the record. Add a Text label control to the canvas.
Note: First, Index, and Last return a single record. FirstN and LastN return a table, even if we specify only a single record.