List.Last
Returns the last item in a list.
List.Last(list, optional default)Returns: The last item, or the default when the list is empty.
What it does
List.Last returns the item at the end of a list. Use it for the closing balance of a month, the most recent payment date after a sort, or the final value of a running total. It is the counterpart of List.First.
Syntax and arguments
list: the list to read.default(optional): the value returned when the list has no items.
Example
A cash ledger lists the balance after each day of the month, in date order. The treasurer needs the closing balance for the month-end pack.
let
DailyBalances = {15200, 14850, 16100, 15940},
ClosingBalance = List.Last(DailyBalances, 0)
in
ClosingBalance
ClosingBalance is 15940, the last value in the list. The default protects the result if a month has no days loaded.
Example: a second, different use
Finding the most recent payment: sort the payment dates in ascending order, then take List.Last to get the latest date.
Things to watch
- The "last" item depends on the order. Sort the list first if the source order is not reliable.
- A list of running totals gives the ending balance directly, because the last step holds the total.
- For the last row of a table, use Table.Last, which returns a record.
- To read the last few items, use List.LastN.
In business terms
Closing balances, latest rates, most recent statement dates and final period amounts are common uses. Check that the list really ends at the period you want, because a missing day at the end gives a wrong closing figure without an error.