Downloads
Power Query MList: pick and buildBeginner

List.FirstN

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

Syntax
List.FirstN(list, countOrCondition)

Returns: A list with the selected items.

What it does

List.FirstN takes the start of a list. You can ask for a fixed count, such as the first ten items, or pass a condition and it keeps items from the start for as long as the condition holds. Use it for a top-ten list after sorting, or for the opening rows of a period before a change in pattern.

Syntax and arguments

Example

A controller wants the five largest vendor balances for the review pack. The list has already been sorted from largest to smallest.

let
    VendorBalances = {98000, 74500, 61200, 40100, 36800, 22000, 9100},
    Top5 = List.FirstN(VendorBalances, 5)
in
    Top5

Top5 is the first five items: {98000, 74500, 61200, 40100, 36800}. The list must already be in descending order, because FirstN takes what comes first.

Example: a second, different use

Taking the opening run of values that are still above a threshold: List.FirstN(Balances, each _ >= 10000) keeps items from the start until the first one below 10,000.

Things to watch

In business terms

Top-ten customers, largest expenses, earliest open items and the first few lines of a report are typical uses. Sorting first and then taking the top N is the usual pattern in a management report.

Related functions

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