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

ClearCollect(MyNewCollection,
{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

Step 3: The following formula returns the first record from the data source.

Power Apps Formula

First (MyNewCollection)
First, Last and Index powerapps functions

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:

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

ClearCollect(MyNewCollection,
{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

Step 3: The following formula returns a table containing the first two records from the data source.

Power Apps Formula

FirstN(MyNewCollection,2)
First, Last and Index powerapps functions

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:

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

ClearCollect(MyNewCollection,
{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

Step 3: The following formula returns the last record from the data source.

Power Apps Formula

Last(MyNewCollection)
First, Last and Index powerapps functions

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:

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

ClearCollect(MyNewCollection,
{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

Step 3: The following formula returns a table containing the first two records from the data source.

Power Apps Formula

LastN(MyNewCollection,2)
First, Last and Index powerapps functions

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:

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

ClearCollect(MyNewCollection,
{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

Step 3: The following formula returns the second record from the data source.

Power Apps Formula

Index(MyNewCollection,2)
Index Function in Power Apps

When the Record index is out of range it returns an error.

Power Apps Formula

Index(MyNewCollection,7)
Index Function in Power Apps

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

Note: First, Index, and Last return a single record. FirstN and LastN return a table, even if we specify only a single record.