Downloads
Power Query MList: pick and buildBeginner

List.LastN

Returns the last few items of a list, either a set number or all the items at the end that pass a test.

Syntax
List.LastN(list, countOrCondition)

Returns: A list with the selected items, in their original order.

What it does

List.LastN takes the end of a list. Ask for a count, such as the last three months, or pass a condition and it keeps items from the end for as long as the condition holds. Use it for trailing averages, the most recent periods in a trend, or the final few entries of a log.

Syntax and arguments

Example

A monthly report shows the balance of a reserve account for the year. The reviewer wants the last three months for a short trend note.

let
    ReserveBalances = {50000, 51200, 49800, 52400, 53100, 54900},
    LatestThree = List.LastN(ReserveBalances, 3)
in
    LatestThree

LatestThree is {52400, 53100, 54900}, in the same order as the original list, so the trend reads from left to right.

Example: a second, different use

Trailing values that are all above a threshold: List.LastN(Balances, each _ >= 50000) keeps the most recent run of balances that are at or above 50,000.

Things to watch

In business terms

Trailing three-month averages, the latest quarter of results, the most recent entries in an audit log and the final periods of a trend are typical uses. Keep the order of the source clear, so the "last" months are the ones you expect.

Related functions

All Power Query M functions · View as Markdown · M basics