Table.Last

Table

Returns the last row of the table as a record, or a default value if the table is empty.

Examples on this page use shared sample tables. View them to understand the input data before reading the examples below.

Syntax

Table.Last(table as table, optional default as nullable any) as any

Parameters

NameTypeRequiredDescription
tabletableYesThe table to retrieve the last row from.
defaultanyNoOptional value to return if the table is empty. Defaults to null if omitted.

Return Value

anyThe last row as a record, or the default value if the table is empty.

Remarks

Table.Last returns the last row of a table as a record, enabling field access via [FieldName] syntax. If the table is empty and no default value is provided, it returns null. Providing a default prevents errors in pipelines where the table may occasionally be empty after filtering.

Table.Last is the counterpart of Table.First. It is most useful when retrieving the most recent record in a time-ordered dataset — for example, the latest transaction after sorting by date, or the final log entry. Combine with Table.Sort to ensure deterministic results: Table.Last(Table.Sort(table, {{"Date", Order.Ascending}})) reliably returns the row with the maximum date.

For cases where you need to enforce that exactly one row exists, use Table.SingleRow. To extract just the bottom-left scalar value without unpacking a record, chain field access: Table.Last(table)[ColumnName]. Note that without a prior sort, "last" refers to whatever row physically appears last in the table, which may not be deterministic for database sources.

Examples

Example 1: Get the most recent order after sorting by date

let
    Sales = #table(
        {"OrderID", "CustomerName", "Product", "UnitPrice", "OrderDate"},
        {{1,"Alice","Widget A",25.00,#date(2024,1,15)},
         {2,"Bob","Gadget B",50.00,#date(2024,1,18)},
         {3,"Charlie","Widget C",15.00,#date(2024,2,1)},
         {8,"Diana","Widget C",15.00,#date(2024,4,15)}}
    ),
    Sorted = Table.Sort(Sales, {{"OrderDate", Order.Ascending}})
in
    Table.Last(Sorted)
Result
Result
1[OrderID = 8, CustomerName = "Diana", Product = "Widget C", UnitPrice = 15, OrderDate = #date(2024,4,15)]

Example 2: Extract the latest order date as a scalar

let
    Sales = #table(
        {"OrderID", "CustomerName", "OrderDate"},
        {{1,"Alice",#date(2024,1,15)},{2,"Bob",#date(2024,1,18)},{8,"Diana",#date(2024,4,15)}}
    ),
    Sorted = Table.Sort(Sales, {{"OrderDate", Order.Ascending}})
in
    Table.Last(Sorted)[OrderDate]
Result
Result
14/15/2024

Example 3: Return a safe default for an empty table

let
    Sales = #table(
        {"OrderID", "CustomerName", "Region"},
        {{1,"Alice","East"},{2,"Bob","West"}}
    ),
    Filtered = Table.SelectRows(Sales, each [Region] = "South")
in
    Table.Last(Filtered, [OrderID = 0, CustomerName = "None", Region = "N/A"])
Result
Result
1[OrderID = 0, CustomerName = "None", Region = "N/A"]

Compatibility

Power BI Desktop Power BI Service Excel Desktop Excel Online Dataflows Fabric Notebooks