PowerApps First, FirstN, Last, and LastN function with examples

In this PowerApps Tutorial, We will see what is PowerApps First, FirstN function, Last and LastN function. What are the syntaxes of each function? Also, by taking some simple scenarios, We will see how to use all these functions in PowerApps app.

PowerApps First and FirstN Function

First, let us discuss on PowerApps First and FirstN function with an example.

  • PowerApps First is a function that will return the first item of a data source or a table.
  • PowerApps FirstN is a function that helps to return the first set of records of a table whereas the second argument defines the number of records that to be returned.
  • The First function returns a single record or an item whereas the FirstN function returns a table.
  • Although if you specify only a single record, then the FirstN function returns a table.

Syntax:

Below represents the syntaxes of PowerApps First and FirstN function.

Syntax-1:

First( Table )

Where,

Table = This is required. You need to specify the table name to operate on.

Syntax-2:

FirstN( Table [, NumberOfRecords ] )

Where,

  1. Table = This is required to specify the table name to operate on.
  2. NumberOfRecords = This is an Optional that will help you to return the Number of records. If you will not specify this argument, then the function will return only one record or item.

Also, check out, PowerApps CountRows function.

PowerApps First and FirstN Function Example

Here we will discuss how we can use PowerApps First and FirstN function by taking a simple scenario.

  • The below screenshot represents a SharePoint List named Travel Details. This list has a number of columns with different data types. Also, it has these many below records as shown below.
  • Out of these many records, I want to retrieve one or some of the records using the PowerApps First and FirstN function.
PowerApps First and FirstN Function Example
powerapps first function
  • On the PowerApps screen, Insert a Vertical Gallery Control and set its Layout property as Title and subtitle.
  • Select the gallery control and apply this below formula on its Items property as:
Items = First('Travel Details')

Where,

‘Travel Details’ = SharePoint List Name

  • Once you will preview the app, you can view the first record in the gallery control as shown in the below screenshot.
PowerApps First and FirstN Function Examples
powerapps first function
  • Similarly, if you want to view set of records i.e. suppose you want first four records from that specific SharePoint list, then apply this below formula on Gallery controls Items property as:
Items = FirstN('Travel Details', 4)

Where,

4 = Number of records that you want to display in the gallery control

  • Once you will preview the app, you can view the first four records in the gallery control as shown in the below screenshot.
PowerApps First and FirstN Functions Examples
powerapps FirstN function

This is how we can use the First and FirstN Function in PowerApps.

PowerApps Last and LastN Function

Let us see how to use the Last and LastN Function in PowerApps.

  • PowerApps Last is a function that will return the last item or record of a data source or a table.
  • PowerApps LastN is a function that helps to return the last set of records of a table whereas the second argument defines the number of records that to be returned.
  • The Last function returns a single record or an item whereas the LastN function returns a table.
  • Although if you specify only a single record, then the LastN function returns a table.

Syntax:

Below represents the PowerApps Last and LastN function syntaxes:

Syntax-1:

Last( Table )

Where,

Table = This is required to specify the table name to operate on.

Syntax-2:

LastN( Table [, NumberOfRecords ] )

Where,

  1. Table = This is required to specify the table name to operate on.
  2. NumberOfRecords = This is an Optional that will help you to return the Number of records. If you will not specify this argument, then the function will return only one record or item.

NOTE:

When you used with the data source, then all these functions can’t be delegated. You can get only the first portion of the data source and then the function applied.
When the data source item crosses the limits, then a warning may appear to remind you of the PowerApps Delegation.

PowerApps Last and LastN Function Example

  • Suppose, you want to view the Last record from the above SharePoint list named Travel Details.
  • In the same way, Insert a Vertical Gallery control and set its Items property as:
Items = Last('Travel Details')
  • When you will save and preview the app, you can see the last item details in the gallery control as shown below.
PowerApps Last and LastN Function Example
PowerApps Last Function
  • Similarly, To retrieve some of the last records, you can apply this below formula on Gallery control’s Items property as:
Items = LastN('Travel Details', 4)

Where,

4 = Number of records that you want to display in the gallery control

  • Once you will preview the app, you can view the last four records in the gallery control as shown in the below screenshot.
PowerApps Last and LastN Function Examples
PowerApps LastN Function

This is how we can use the Last and LastN Function in PoweApps.

First vs FirstN expression in PowerApps

In this example, We will see what is the difference between PowerApps First and FirstN function.

  • Below represents a SharePoint List named Travel Details. This list has a column named “Driver“.
  • Here, I want to get the 3rd record (of Driver column only) from the specified list i.e. Nagaraj.
PowerApps First vs FirstN expression
First vs FirstN expression in PowerApps
  • Here the problem is When I am using the First function to get the first value, the output returns as the expected value. Not only the first function but also the Last function returns the last value.
  • But when I am using the below syntax as FirstN, then it generates an error as: “Expected Text Value“. That means the property on this control expects text values.
  • I have applied the formula on Label control’s Text property as:

Text = FirstN('Travel Details', 3).Driver

Where,

  1. ‘Travel Details’ = SharePoint List Name
  2. Driver = SharePoint List Column name that will retrieve the 3rd name

You can see the error in this below screenshot. Follow the below things to resolve the issue.

First vs FirstN expression in PowerApps
First vs FirstN expression in PowerApps
  • First of all, to clarify this type of issue, you need to understand properly the difference between First, First N, Last and LastN functions in PowerApps.
  • The PowerApps First Function helps to retrieve the first record whereas the Last Function retrieves the last record from your data source. The return result is a single record.
  • But the FirstN function is used to return the first set of records of a table and the LastN function returns the last set of records. The returned result is always a table only.
  • To resolve the above issue, you can use this below formula on Label’s Text property as:
Text = Last(FirstN('Travel Details', 3)).Driver
  • Now Save and Preview (F5) the app. You can see the value of the 3rd record (Driver value i.e. Nagaraj) as shown in the below screenshot.
First vs FirstN expression in Power Apps
First vs FirstN expression in PowerApps

Also, you may like these below PowerApps Tutorials as:

In this PowerApps Tutorial, We saw what is PowerApps First, FirstN function, Last and LastN function. What are the syntaxes of each function? Also, by taking some simple scenarios, We discussed how to use PowerApps First, FirstN, Last and LastN functions.

  • >