Data - How to get a row by ordinal number

Following an update to Power FX, we can now retrieve a record by row number/ordinal number by calling the Index function. The official documentation for this new feature is here:


The introduction of this feature is to support upcoming JSON parsing functionality. In the meantime, the introduction of the new Index function will simplify scenarios where we want to retrieve a row by ordinal number.

How to retrieve an item by row number

The basic use of the Index function looks like this.

Index(datasource, index)
The first argument defines the input data, which could be a collection, table data, or an online/live data source.

The second argument defines the index or row number. This is value one based - (eg, 1 will return the first record).

What's the use case for retrieving records by ordinal number?

There are many practical use cases for needing to return a record based on a specific row number.

As an example, there could be a requirement to build a reporting/dashboard type screen that shows data values based on row number.

The ability to retrieve records by the number is also useful in transformation scenarios such as those that are needed to transpose rows to columns, particularly when saving to non-normalised data structures.

The built-in budget tracker sample app includes formulas that retrieve data from collections based on the ordinal number. The screenshot beneath shows how prior to the Index function, we needed to write convoluted formula that utilised the Last/FirstN functions to carry out this task.

When we call Index - What happens if the record doesn't exist?

According to the documentation, an error occurs when we attempt to call Index to return a row that doesn't exist.

For practical purposes, if we want to detect that Index fails to return a row, we can do so by calling the IsBlankOrError function. The screenshot below highlights how this returns true when we attempt to return a row from a collection that doesn't exist (row 16).


What happens when we call Index against an online data source?

When we attempt to call Index against a live online data source, an important point to note is that the function is not delegable. Let's take the example of a large SharePoint list called Clients.

If we attempt to return row 3,000 (which falls outside of the bounds of the maximum 'data row limit' setting), Power Apps will fail to return the required record.

Index(Clients, 3000)

We can verify this behaviour through the Monitor utility, as shown below. Therefore, Index will only rows up to the value in the 'data row limit' setting for online data sources.


Conclusion

In cases we want to return a row based on ordinal number, we can now accomplish this by calling the Index function. This is a big improvement compared to the Last/FirstN method that was necessary prior to this.

Related posts

FormuIas - Is it possible to call a user-defined function recursively in Power Apps?
January 31, 2024
Formulas - A beginners guide on how to create and call user-defined functions (UDFs)
January 28, 2024
Formula - How to add a button that converts degrees Centigrade to Fahrenheit and vice versa
May 08, 2023
Formula - How to convert a single delimited string to rows and columns
April 05, 2023
Data - How to group data in a gallery and calculate sums
January 20, 2023
Formula - How to calculate compound interest
November 24, 2022
Utilities - The best way to peform OCR on images of Power Apps Formulas
October 11, 2022
Example - How to use a drop down control to convert currencies
September 27, 2022
Formula - How to parse JSON in Power Apps- 4 examples
September 15, 2022
Formula - What to do when the If statement doesn't work?
December 17, 2021
Formula - Boolean And / Or operators - What is the order of precedence?
December 16, 2021
Controls - How to set the data source of a Combo Box to a comma separated string
November 16, 2021
Numbers - 10 examples of how to round numbers
August 18, 2021
Formula - Difference between round, square, and curly brackets
July 20, 2021
Top 3 highlights of upcoming enhancements to the Power Apps language (Power FX)
May 26, 2021
Email - Sending email attachments with the Office 365 Outlook connector
March 30, 2021
Formula - What to try when numbers don't format correctly
March 24, 2021
Controls - How to convert HTML to Text
March 23, 2021
Formulas - how to return all days between two dates
March 15, 2021
Formula - How to create comma separated (CSV) list of items
February 25, 2021
Formula - How to use the IF and Switch functions - 3 common examples
February 12, 2021
Location - Finding the closest location and and sorting records by distance, based on the current location of the user
January 24, 2021
Formulas - How to cope with weekends and public holidays in date calculations
January 21, 2021