FirstN

How to Use the Power Fx FirstNĀ  Function in Power Apps

What is the FirstN Function?

The FirstN function is a Power Fx function that retrieves the first N records from a data source. The function takes two arguments: the data source and the number of records to retrieve. The syntax for the FirstN function is as follows:


FirstN(DataSource, NumberOfRecords)


For example, if you want to retrieve the first 5 records from a SharePoint list called “Contacts”, you would use the following formula:


FirstN(Contacts, 5)


The FirstN function is particularly useful when you need to retrieve a specific number of records from a data source, rather than retrieving all records and then filtering the results.

How to Use the FirstN Function in Power Apps

To use the FirstN function in your Power Apps application, you’ll need to follow these steps:

1. Open your Power Apps application and go to the screen where you want to use the FirstN function.

2. Add a gallery control to the screen.

3. Set the Items property of the gallery control to the FirstN function, using the syntax we discussed earlier. For example, if you want to retrieve the first 10 records from a SharePoint list called “Orders”, you would set the Items property of the gallery control to the following formula:


FirstN(Orders, 10)


4. Customize the gallery control as needed to display the data from the data source.

That’s it! You’ve now successfully used the FirstN function to retrieve the first N records from your data source.

Examples of Using the FirstN Function

Let’s take a look at a few examples of using the FirstN function in Power Apps.

Example 1: Retrieving the First 5 Records from a SharePoint List

Suppose you have a SharePoint list called “Employees” that contains information about all of the employees in your company. You want to display the first 5 employees in a gallery control on your Power Apps screen.

To do this, you would follow the steps we discussed earlier:

1. Add a gallery control to your Power Apps screen.

2. Set the Items property of the gallery control to the following formula:


FirstN(Employees, 5)


3. Customize the gallery control as needed to display the employee data.

Example 2: Retrieving the First 3 Records from a SQL Server Table

Suppose you have a SQL Server table called “Products” that contains information about all of the products in your inventory. You want to display the first 3 products in a gallery control on your Power Apps screen.

To do this, you would follow the same steps we discussed earlier:

1. Add a gallery control to your Power Apps screen.

2. Set the Items property of the gallery control to the following formula:


FirstN('SQL Server'.Products, 3)


Note that in this example, we’re using single quotes around the data source name because it contains a space. If your data source name doesn’t contain a space, you can omit the single quotes.

3. Customize the gallery control as needed to display the product data.

The FirstN function is a powerful tool that can save you time and effort when working with large data sets in your Power Apps application. By following the steps and examples we’ve discussed in this article, you should now be able to use the FirstN function to retrieve the first N records from your data source with ease.

Power Apps Training Courses by G Com Solutions (0800 998 9248)