Table.LastN

Table.LastN is a Power Query M function that retrieves the last row(s) from a table based on a count or condition. The function returns a list of rows that meet the specified criteria.

Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365

Syntax

Table.LastN(
   table as table,
   countOrCondition as any,
) as table

Description

Returns the last row(s) from the table, table, depending on the value of countOrCondition:

  • If countOrCondition is a number, that many rows will be returned starting from position (end – countOrCondition).
  • If countOrCondition is a condition, the rows that meet the condition will be returned in ascending position until a row does not meet the condition.

Examples

Find the last two rows of the table.

Table.LastN( 
    Table.FromRecords( {
        [CustomerID = 1, Name = "Bob", Phone = "123-4567"],
        [CustomerID = 2, Name = "Jim", Phone = "987-6543"],
        [CustomerID = 3, Name = "Paul", Phone = "543-7890"]
    } ),
    2
 )

 /* Output: 
Table.FromRecords( {
    [CustomerID = 2, Name = "Jim", Phone = "987-6543"],
    [CustomerID = 3, Name = "Paul", Phone = "543-7890"]
} )
 */ 

Find the last rows where [a] > 0 in the table.

Table.LastN( 
    Table.FromRecords( {
        [a = -1, b = -2],
        [a = 3, b = 4],
        [a = 5, b = 6]
    } ),
    each _ [a] > 0
 )

 /* Output: 
Table.FromRecords( {
    [a = 3, b = 4],
    [a = 5, b = 6]
} )
 */ 

Other functions related to Table.LastN are:

BI Gorilla Blog

Last update: August 28, 2023 | Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/table-lastn
© 2023 BI Gorilla. All rights reserved. Content derived from Microsoft documentation is property of Microsoft Corp.