<!-- https://learnsql.cpetoday.com/functions/m/list-lastn -->
# List.LastN

Language: Power Query M
Category: List: pick and build
Syntax: List.LastN(list, countOrCondition)
Summary: Returns the last few items of a list, either a set number or all the items at the end that pass a test.
Returns: A list with the selected items, in their original order.
Level: Beginner
Related: List.FirstN, List.Last, List.Reverse, List.Skip, List.Select

## 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
- `list`: the list to read from.
- `countOrCondition`: either a whole number, or a function (`each ...`) tested from the end that stops at the first item that fails.

## 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.

```powerquery
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
- The list must be in date order before you take the last items. Sort first if the source is not reliable.
- The condition form stops at the first item from the end that fails.
- Asking for more items than the list holds returns the whole list.
- For a table, use Table.LastN, which works on rows.

## 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.
