Power Query List.FirstN: Step-by-Step Guide
In this article
List.FirstN returns the first N items from a list in Power Query. You give it a list and a number, and it hands back a new list containing only the leading items. If you ask for more items than exist, it returns the whole list without error. If you ask for zero or a negative number, it returns an empty list. That behaviour makes it safe for trimming columns, sampling rows, or grabbing a fixed number of elements before further processing.
What You'll Need
- Power Query Editor open in Excel, Power BI Desktop, or another host that supports Power Query (the steps are the same across these).
- A query that already produces a list, or a column you can convert to a list. If you are starting from a table, you can use the table's column as a list with
Table.Columnor by referencing the column directly in a step. - Basic familiarity with the Power Query formula bar and the Advanced Editor, since List.FirstN is a function you call in a custom step rather than a button in the ribbon.
Step-by-Step
- Open the Power Query Editor. In Excel, go to Data > Get Data > Launch Power Query Editor. In Power BI Desktop, click Transform data. Menu labels can differ slightly by version and host, but the editor itself works the same way.
- Load or create the query that contains the list you want to trim. If your data is a table, you can still use List.FirstN on a column by converting that column to a list first: select the column, then use Transform > To List (or right-click the column header and choose To List). This gives you a list value you can pass to the function.
- Add a custom step. In the Home tab, click New Source > Blank Query, or use Add Column > Custom Column if you are working inside a table. For a standalone list, a blank query is cleaner. Open the Advanced Editor (Home > Advanced Editor) so you can write the full expression.
- Write the List.FirstN call. The syntax is
List.FirstN(list as list, countOrCondition as any) as list. The first argument is your list; the second is either a number or a condition. For a simple count, use a number:List.FirstN(MyList, 3). ReplaceMyListwith the name of the step that holds your list, or with an inline list such as{"a","b","c","d"}. - If you want to take items while a condition is true, pass a condition function instead of a number. For example,
List.FirstN(MyList, each _ < 10)returns items from the start of the list until the first item that does not satisfy the condition. The condition is evaluated on each item in order, and the function stops at the first failure. - Close the Advanced Editor and check the result. The step should show a list with the expected number of items. If you are working inside a table, use the expand icon next to the new column to bring the list items into rows, or use
Table.FromListto turn the list back into a table. - Rename the step so the intent is clear, for example "First 3 Items" or "Leading Values Under 10". This keeps the Applied Steps pane readable when the query grows.
Common Mistakes
- Passing a table instead of a list. List.FirstN expects a list. If you pass a table, you get an error. Convert the column to a list first, or use
Table.FirstNif you actually want the first rows of a table. - Confusing List.FirstN with List.First. List.First returns a single item (the first one). List.FirstN returns a list of items. If you need one value, use List.First; if you need several, use List.FirstN.
- Assuming a negative count errors. A negative or zero count returns an empty list, not an error. That can silently produce blank results downstream if you expected a failure.
- Using a condition that never fails. If the condition is true for every item, List.FirstN returns the entire list. That is valid but may not be what you intended if you expected a shorter result.
- Forgetting that the condition is order-dependent. The function stops at the first item that fails the condition. If your list is not sorted the way you expect, the result may look wrong even though the function behaved correctly.
Tips
- Use List.FirstN when you need a fixed sample size from a list, such as the top three values after sorting. Sort the list first with List.Sort, then take the first N.
- For tables, Table.FirstN is usually the right tool. List.FirstN is for list values, which often come from a column, a parameter, or a manually defined list.
- Combine List.FirstN with List.Count to check how many items you actually got:
List.Count(List.FirstN(MyList, 5))returns the smaller of 5 and the list length. - If you need the last N items, use List.LastN. The behaviour mirrors List.FirstN, including the safe handling of counts larger than the list.
- Keep the count in a parameter or a named step if you reuse it in several places. That makes it easier to adjust later without hunting through the query.
Troubleshooting
- Error: "We cannot convert a value of type Table to type List." You passed a table where a list is required. Convert the column to a list (Transform > To List) or switch to Table.FirstN if you meant to take rows.
- The result is empty when you expected items. Check the count argument. A zero or negative number returns an empty list. Also confirm the list itself is not empty at that step.
- The result contains more items than expected. If you used a condition, it may be true for every item, so the whole list is returned. If you used a number, confirm the number is what you think it is, especially if it comes from a parameter.
- The query is slow. List.FirstN itself is cheap, but the steps that build the list may be expensive. Check the Applied Steps and consider filtering or sorting earlier to reduce the list size before calling the function.
- The step works in the editor but fails on refresh. This often means an upstream step returns a different shape on refresh, such as a table instead of a list, or an empty result. Inspect the step before List.FirstN and confirm it still returns a list.
Frequently Asked Questions
What is the difference between List.FirstN and List.First?
List.First returns a single item, the first element of the list. List.FirstN returns a list of the first N items. Use List.First when you need one value, and List.FirstN when you need several.
Can I use a condition instead of a number?
Yes. The second argument can be a condition function, such as each _ > 0. The function returns items from the start of the list until the first item that fails the condition. The order of the list matters.
What happens if I ask for more items than the list contains?
You get the entire list back. List.FirstN does not raise an error when the count exceeds the list length.
Does List.FirstN work on a table column?
Not directly. A table column is a list only after you convert it. Use Transform > To List on the column, or reference the column with Table.Column, then pass that list to List.FirstN.
Can I use List.FirstN to take the last N items?
No, use List.LastN for the last N items. List.FirstN always takes from the start of the list.
Is List.FirstN available in all Power Query hosts?
It is part of the standard Power Query M library and is available in Excel, Power BI Desktop, and other hosts that use Power Query. Menu labels may differ, but the function syntax is the same.