List.FirstN Function in Power Query M

Sumit Bansal
Written by

Sumit Bansal is the founder of TrumpExcel.com and a 13-time Microsoft Excel MVP. He started this site in 2013 to share his passion for Excel through easy tutorials, tips, and training videos, helping you master Excel, boost productivity, and maybe even enjoy spreadsheets!

The List.FirstN function in Power Query gives you the first few items of a list. You can tell it how many items you want, or give it a condition and it keeps items from the top until one fails.

The examples below cover both, including how to get the top 3 values from a column and how to stop at the first blank row.

Follow along with the example file

List.FirstN Function Examples.xlsx

Download

List.FirstN Function Syntax

List.FirstN(list as list, countOrCondition as any) as any
  • list – the list you want to take items from. It can be a list you type in curly brackets, like {1,2,3}, or a column from a table that your query has loaded (Example 3 shows how).
  • countOrCondition – either a number or a condition. A number, like 3, returns up to that many items from the start of the list. A condition, like each _>0 (is the item greater than 0?), returns items from the start while they meet it and stops at the first one that doesn’t. Example 5 explains the each _ part.

What it returns: A new list with the items it picked, in their original order. You get an empty list if the count is 0, or if the very first item fails the condition.

The List.FirstN function is part of the core Power Query M library, so it works wherever Power Query does, including Excel for Microsoft 365, 2024, 2021, 2019, and 2016 (on Windows for 2019 and 2016), and Power BI.

When to Use the List.FirstN Function

Use this function when you need to:

  • Get the first few items of a list, like the top 3 names in a results column
  • Get the highest (or lowest) few values, by sorting the list first
  • Keep items from the top of a list until something changes, like a value dropping below a target or a blank row
  • Keep only the first few columns of a table

If you only need the very first item, the List.First function is simpler, since it returns that item on its own instead of inside a list. And to get the first few rows of a whole table, use the Table.FirstN function.

Example 1: Get the First 5 Items of a List

Let’s start with a simple one. Suppose you have a list of the seven days of the week, and you only want the five working days, Monday to Friday.

In Excel, go to Data > Get Data > From Other Sources > Blank Query. In the Power Query Editor that opens, type this formula in the formula bar:

= List.FirstN({"Mon","Tue","Wed","Thu","Fri","Sat","Sun"},5)

Result: Mon, Tue, Wed, Thu, Fri

List.FirstN returns the first 5 days: Mon, Tue, Wed, Thu, Fri

Here, the second argument is the number 5, so List.FirstN returns the first five items of the list and drops the rest.

Examples 1 and 2 type their list right into the query, so they don’t need a table on a worksheet. In the example file, you can open them from Data > Queries & Connections.

Example 2: When the Count Is Bigger Than the List

Now let’s see what happens when you ask for more items than the list has. Suppose your list has only three quarters in it, Q1 to Q3, but you ask for the first 5 items.

Create a new blank query and use this formula:

= List.FirstN({"Q1","Q2","Q3"},5)

Result: Q1, Q2, Q3

Asking for 5 items from a 3-item list returns all three: Q1, Q2, Q3

Here, the list has fewer items than you asked for, so List.FirstN returns all of them. You don’t get an error, which is handy when your data doesn’t always have enough rows.

A count of 0 gives you an empty list. A negative count, like -1, gives the error “The ‘count’ argument is out of range.”

Example 3: Get the Top 3 Names from a Column

Now let’s use a column from a table. Below is a race results table that’s already in finishing order, and I want a list of the top 3 runners.

RaceResults table with runners listed in finishing order

The results are in an Excel Table named RaceResults. The code below reads it by that name, so for your own data, change “RaceResults” to the name of your table.

Create a new blank query, open the Advanced Editor (Home > Advanced Editor), and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="RaceResults"]}[Content],
TopThree = List.FirstN(Source[Runner],3)
in
TopThree

Result: Veronica Fisher, Cynthia Henderson, Mitchell Peterson

The top 3 runners: Veronica Fisher, Cynthia Henderson, Mitchell Peterson

Here, Source[Runner] gives the Runner column as a list, from the fastest runner to the slowest. List.FirstN then keeps the first 3 names.

This works because the rows are already in finishing order (if yours aren’t, sort them first with the Table.Sort function). If you want the first 3 complete rows (with the Bib and Age Group too), use the Table.FirstN function instead.

Example 4: Get the Top 3 Values (Sort First)

This next one comes up a lot. Below is a sales table with one row per sales rep, in no particular order, and I want the three highest sales amounts.

RepSales table with sales amounts in no particular order

The sales data is an Excel Table named RepSales (use your own table’s name in the code). Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="RepSales"]}[Content],
TopThree = List.FirstN(List.Sort(Source[Sales],Order.Descending),3)
in
TopThree

Result: 31200, 26750, 22900

The three highest sales amounts: 31200, 26750, 22900

Here, the List.Sort function sorts the Sales column from largest to smallest, because of Order.Descending. List.FirstN then keeps the first 3 values of that sorted list, which are the three highest amounts.

List.FirstN only looks at position, not value. Without the sort, you’d get the first 3 amounts in the table (18400, 26750, 12300), which aren’t the top 3.

If you only need the single highest amount, the List.Max function gets it in one step.

Follow along with the example file

List.FirstN Function Examples.xlsx

Download

Example 5: Keep Items Until a Condition Fails

Now let’s use a condition instead of a number. Below is a weekly sales table with a target of 4,500 a week, and I want the sales from the start of the period up to the first week that missed the target.

WeeklySales table where Week 4 is the first week below the 4,500 target

The data is an Excel Table named WeeklySales (use your own table’s name in the code). Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="WeeklySales"]}[Content],
OnTargetRun = List.FirstN(Source[Sales],each _>=4500)
in
OnTargetRun

Result: 5200, 4800, 6100

Sales up to the first missed week: 5200, 4800, 6100

Here, each _>=4500 is the condition, and the underscore (_) stands for each sales value. List.FirstN keeps going from the top while the sales are 4,500 or more.

Week 4 has 3900, so List.FirstN stops there. It ignores Weeks 5 and 6 even though they’re above the target, which is how you get the weeks on target before the first miss.

Important: If you want every value that meets the condition, wherever it is in the list, use the List.Select function instead. List.Select(Source[Sales],each _>=4500) returns five values, including Weeks 5 and 6.

Example 6: Stop at the First Blank

This one helps with sheets where people add notes below the data. Below is a sign-up table with a blank row, followed by a “Checked by” note, and I want only the attendee names above the blank row.

SignUps table with a blank row followed by a Checked by note

The sign-ups are in an Excel Table named SignUps (use your own table’s name in the code). Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="SignUps"]}[Content],
Attendees = List.FirstN(Source[Attendee],each _<>null)
in
Attendees

Result: Valerie Taylor, Cassandra Hayes, Christopher Johnson, Stephanie Shaw

Only the four attendee names above the blank row

Here, a blank cell comes into Power Query as null. The condition each _<>null keeps names while the cell isn’t blank, so List.FirstN stops at the blank row and leaves out the note below it.

This only works for truly empty cells. A cell with just a space in it isn’t null, so List.FirstN would keep going past it.

Example 7: Keep the First 3 Columns of a Table

List.FirstN also works on lists of column names. Below is a staff table with six columns, and I want to keep only the first three, Employee ID, Name, and Department.

Staff table with six columns from Employee ID to Location

The staff list is an Excel Table named Staff (use your own table’s name in the code). Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="Staff"]}[Content],
FirstThreeColumns = Table.SelectColumns(Source,List.FirstN(Table.ColumnNames(Source),3))
in
FirstThreeColumns

Result: The Staff table with only the Employee ID, Name, and Department columns

The Staff table with only Employee ID, Name, and Department

Here, the Table.ColumnNames function returns the column names as a list, from Employee ID to Location. List.FirstN keeps the first 3 names, and the Table.SelectColumns function keeps only those columns.

Since the code picks columns by position, it still works if a column is renamed later. But if the column order changes, it keeps whichever three columns come first.

Example 8: Get the Top 25% of Values

In this last example, the count comes from a calculation instead of a fixed number. Below is a table of quiz scores for 10 students, and I want the top 25% of the scores.

QuizScores table with scores for 10 students

The scores are in an Excel Table named QuizScores (use your own table’s name in the code). Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="QuizScores"]}[Content],
TopQuarter = List.FirstN(List.Sort(Source[Score],Order.Descending),Number.RoundUp(Table.RowCount(Source)*0.25))
in
TopQuarter

Result: 97, 93, 91

The top 25% of scores: 97, 93, 91

Here, the Table.RowCount function counts the 10 students, and 25% of 10 is 2.5. The Number.RoundUp function rounds that up to 3 (Number.RoundDown would give 2), so List.FirstN returns the three highest scores from the sorted list.

Important: The count must be a whole number, so round it first with Number.RoundUp or Number.RoundDown. If you pass 2.5 directly, you get the error “The number is out of range of a 32 bit integer value.”

Other Excel Articles You May Also Like:

All Power Query Functions

Sumit Bansal

Sumit Bansal

13x Microsoft Excel MVP

Hey! I'm Sumit Bansal, founder of trumpexcel.com and a Microsoft Excel MVP. I started this site in 2013 because I genuinely love Microsoft Excel (yes, really!) and wanted to share that passion through easy Excel tutorials, tips, and Excel training videos. My goal is straightforward: help you master Excel skills so you can work smarter, boost productivity, and maybe even enjoy spreadsheets along the way!

Get the FREE 51 Excel Tips Ebook

Enter your details and the free PDF is on its way to your inbox.

Hmm, that didn't go through. Please check your email and try again.

No spam. You'll also get my weekly Excel newsletter. Unsubscribe anytime.

Check your inbox!

The ebook is on its way to your email. It usually lands within a couple of minutes.