Blog
Data - How to get a row by ordinal number
April 28. 2022
It's now possible to retrieve records from a table, collection, or data source by ordinal number. The function that provides this functionality is called Index, and this post summarises how it works.
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).
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.
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.
- Categories:
- formula
Related posts
- FormuIas - Is it possible to call a user-defined function recursively in Power Apps?
- Formulas - A beginners guide on how to create and call user-defined functions (UDFs)
- Formula - How to add a button that converts degrees Centigrade to Fahrenheit and vice versa
- Formula - How to convert a single delimited string to rows and columns
- Data - How to group data in a gallery and calculate sums
- Formula - How to calculate compound interest
- Utilities - The best way to peform OCR on images of Power Apps Formulas
- Example - How to use a drop down control to convert currencies
- Formula - How to parse JSON in Power Apps- 4 examples
- Formula - What to do when the If statement doesn't work?
- Formula - Boolean And / Or operators - What is the order of precedence?
- Controls - How to set the data source of a Combo Box to a comma separated string
- Numbers - 10 examples of how to round numbers
- Formula - Difference between round, square, and curly brackets
- Top 3 highlights of upcoming enhancements to the Power Apps language (Power FX)
- Email - Sending email attachments with the Office 365 Outlook connector
- Formula - What to try when numbers don't format correctly
- Controls - How to convert HTML to Text
- Formulas - how to return all days between two dates
- Formula - How to create comma separated (CSV) list of items
- Formula - How to use the IF and Switch functions - 3 common examples
- Location - Finding the closest location and and sorting records by distance, based on the current location of the user
- Formulas - How to cope with weekends and public holidays in date calculations