Blog
Data - How to return the last record from a table
A common question from app builders is, how do we return the last record from a table? The reason for this question is because the Last function may not return the expected record. This post examines this behaviour and describes the most efficient way to return the last record from a table.
Demonstration of problem
To highlight this problem, let's take the example of a table called issues which is shown beneath. There are currently 2,502 records in this table, and the 'IssueID' of the last record is 2502.
There is a natural inclination to retrieve the last record by calling the Last function like so:
Last('[dbo].[Issue]')
Why does the Last function not work?
Power Apps derrives the last record by fetching records from the source table and returning the last record. For performance reasons, it doesn't fetch all the records. Instead, it fetches a batch of records and the number of records that it retrieves corresponds to the data limit setting, which by defaults is 500.
This is the reason why the Last function returns record 500 in this example.
How to correctly return the last record
The correct way to return the last record is to sort the records in descending order (by ID or 'create date' value) and to return the first record from this result set. The syntax we use would look like this:
First(
Sort('[dbo].[Issue]',IssueID, Descending)
)
How to return the last record in the most efficient way
If we only want to return the single last record, the First/Sort technique from above is not efficient. This is because Power Apps fetches a batch of 500 records (ie, the data row limit value) just so that we can retrieve one record from this batch.
First(
FirstN(Sort('[dbo].[Issue]',IssueID, Descending),
1
)
)
As we can see from the the screenshot, the formula returns the correct record (record ID 2502) and the delegation warning also disappears.
Conclusion
- Categories:
- data
- Data - How to find the common rows from 3 or more collections
- Data - How to show the distinct rows from 2 data sources or collections
- Data - How to implement circular rotational date sorting
- Bug - What to do when the data section of the Power Apps Maker portal doesn't work
- Data - Combine columns from separate tables into a single table
- Formula - Transposing/converting rows to columns- an almost impossible task?
- Data - How to rename field names in a record
- Data - How to hide duplicate rows in a gallery / show distinct multiple columns in a gallery
- Data - Retrieving news/forum/blog articles with RSS
- Data - How to sort by partial numbers in a text field
- Data - How to create bulk test/dummy records with random values
- Data - 3 things you should know before using the MySQL or PostgreSQL connectors
- Data - A walkthrough of how to migrate the data source of an app from Excel to Sharepoint
- Data - How to enforce unique values (or prevent duplicate values) in one or more columns
- Data - How much mobile data does Power Apps consume? What ways can we minimise this?
- Data - How to save and retrieve Google calendar entries
- Data - How to save and retrieve Google contacts
- SQL - Caution! This is how users can hack shared SQL connections
- SharePoint – 2 Mistakes to avoid when importing Excel data
- SQL - Don't let this DateTime bug catch you out!
- Settings - What's the purpose of the "Explicit Column Selection" Setting?
- SQL Server for Beginners Part 3 - Installing On-Premises Gateway
- SQL Server for Beginners Part 2 - Installing Management Studio
- SQL Server for Beginners Part 1 - Installing SQL Server
- Searching data–What you need to know about case sensitivity
- Images - How to create images that can change depending on data
- Excel - Reasons NOT to use Excel as a data source
- SharePoint - What you need to know about Filtering Data
- Formulas - Generating Row Numbers