Latest Posts

PowerApps - retrieve last N records


The LastN function in Power Apps is used to retrieve the last N records from a table or collection. This function is useful when you need to display or process the most recent entries in a dataset, such as the latest transactions, messages, or records.

PowerApps –LastN() function syntax

LastN(Table, NumberOfRecords)

  • Table: The table or collection from which you want to retrieve records.
  • NumberOfRecords: The number of records you want to retrieve from the end of the table.

Notes:

  1. Delegation: Be mindful of delegation limits when working with large datasets and ensure the data source supports delegation if necessary.
  2. Combining with Other Functions: Combine LastN with functions like Filter, Sort, and Search to retrieve and display the most relevant records.
  3. User Input Validation: Ensure that user inputs for the number of records to retrieve are valid numbers to prevent errors.
  4. Displaying Recent Entries: Use LastN to display the most recent entries in a collection, such as the latest messages, transactions, or updates.

PowerApps – Retrieve Last Two Records from collection

Suppose you have a collection named Products and you want to get the last two products in this collection.

  1. Create the Collection:

ClearCollect(Products,

    Table(

        {ProductID: 1, ProductName: "Laptop", Price: 1000},

        {ProductID: 2, ProductName: "Tablet", Price: 500},

        {ProductID: 3, ProductName: "Monitor", Price: 300}

    )

);

  1. Retrieve the Last Two Records:
    • Add a Gallery control.
    • Set the Items property of the Gallery to:

LastN(Products, 2)

This will display the last two products, "Tablet" and "Monitor".

PowerApps – Get LastN records from a Filtered Collection

Suppose you want to get the last three products that cost less than $600 from the Products collection.

ClearCollect(Products,

    Table(

        {ProductID: 1, ProductName: "Laptop", Price: 1000},

        {ProductID: 2, ProductName: "Tablet", Price: 500},

        {ProductID: 3, ProductName: "Monitor", Price: 300},

        {ProductID: 4, ProductName: "Keyboard", Price: 100},

        {ProductID: 5, ProductName: "Mouse", Price: 50}

    )

);

  1. Retrieve the Last Three Records from Filtered Collection:
    • Add a Gallery control.
    • Set the Items property of the Gallery to:

LastN(Filter(Products, Price < 600), 3)

This will display the last three products with a price less than $600, which are "Monitor", "Keyboard", and "Mouse".

PowerApps – Get LastN records with User Input

Suppose you want to allow users to specify how many records to retrieve from the Products collection.

  1. Add a Text Input Control:
    • Insert a Text Input control named RecordCountInput.
  2. Retrieve the Specified Number of Records:
    • Add a Gallery control.
    • Set the Items property of the Gallery to:

LastN(Products, Value(RecordCountInput.Text))

 

 

This will display the number of products specified by the user in the RecordCountInput control.

 


We value your Feedback:

Page URL:

Name:

 

Email:

 
 

Suggestion:

 

© 2024 Code SharePoint