Power Query List.First: How to Get the First Item
In this article
List.First returns the first item in a Power Query list, or an optional default value if the list is empty. You use it in the Power Query Editor (or in a Power Query M formula) when you need to grab the top element of a list without manually counting positions. The syntax is List.First(list as list, optional defaultValue as any) as any. If the list is empty and you provide a default, that default is returned; if you omit it, the function returns an error. This guide walks through using List.First in the Power Query Editor, covers common mistakes, and answers practical questions.
What You'll Need
- Power BI Desktop, Excel (Power Query is built into Excel 2016 and later), or another tool that hosts Power Query.
- A dataset or a manually created list to work with. You can create a list from scratch using the M formula bar.
- Basic familiarity with the Power Query Editor interface: the ribbon, the Queries pane, and the formula bar.
Step-by-Step
- Open the Power Query Editor. In Excel, go to the Data tab and select Get Data > Launch Power Query Editor. In Power BI Desktop, select Transform Data from the Home ribbon. Menu labels can differ slightly by version and platform, but the core path is similar.
- Create or load a list. You can load a table and then convert a column to a list, or create a list directly in a blank query. For a quick test, go to Home > New Source > Blank Query, then in the formula bar type
= {1, 2, 3}and press Enter. This returns a list of three numbers. - Add a step to call List.First. With your list query selected, go to Home > Advanced Editor (or open the Advanced Editor from the ribbon). In the M code, add a new step that uses List.First. For example, if your list step is named
Source, add a line likeFirstItem = List.First(Source)and then outputFirstItem. Close the Advanced Editor. The query now returns a single value: the first item of the list. - Handle empty lists with a default value. If your list might be empty, supply a second argument:
List.First(Source, "No items"). This returns "No items" when the list is empty instead of raising an error. The default can be any value: text, number, null, or even another list. - Test with different list types. Try a list of text values, a list of records, or a list of dates. List.First always returns the item at position 0 (the first element). For a list of records, the result is a record; you can then expand it or access fields with the field access operator.
- Use List.First inside other functions. You can nest List.First in expressions, for example to get the first item of a grouped table column:
List.First(Table.Column(MyTable, "Sales")). This is useful when you need a single value from a column after sorting or filtering. - Apply and load. Once the step works, close and load the query to a worksheet or data model. The result is a single value, not a table, so it will load as a scalar value or be used in a calculated table or measure depending on your host tool.
Common Mistakes
- Forgetting the default value on empty lists. Calling List.First on an empty list without a default throws an error: "The list is empty." Always provide a default if emptiness is possible.
- Assuming List.First sorts the list. List.First does not sort; it returns the item at the current first position. If you need the smallest or largest value, sort the list first with List.Sort, then call List.First.
- Passing a table instead of a list. List.First expects a list. If you pass a table, you will get an error. Convert a column to a list using Table.Column or Table.ToList first.
- Misunderstanding the default argument. The default is returned only when the list is empty. It is not a fallback for null values inside the list; if the first item is null, List.First returns null.
- Using List.First on a list of lists. The function returns the first inner list, not the first item of the first inner list. To get the first item of the first inner list, use
List.First(List.First(MyList)).
Tips
- Use List.First in combination with List.Sort to get the minimum or maximum:
List.First(List.Sort(MyList))gives the smallest value for numbers or text. - For lists that may be empty, always supply a default. A common pattern is
List.First(MyList, null)to return null instead of an error. - If you need the first N items, use List.FirstN instead of calling List.First multiple times.
- Remember that Power Query lists are zero-indexed; List.First is equivalent to
MyList{0}but with the added default-value handling. - When working with grouped data, List.First on a grouped column returns the first record in each group after the grouping operation, which may not be the original first row unless you sorted beforehand.
Troubleshooting
If List.First returns an error, check the following in order:
- Is the input a list? Use the formula bar to inspect the step. If the step returns a table or a scalar, convert it to a list first.
- Is the list empty? If so, add a default value as the second argument.
- Is the list null? List.First on null returns an error. Guard against null by using
if MyList = null then null else List.First(MyList). - Check for typos in the function name. Power Query M is case-sensitive: use
List.First, notlist.first. - Verify the step name. In the Advanced Editor, ensure the variable you pass to List.First is the correct step name from the previous line.
If the function works but the result is not what you expect, confirm the list order. List.First does not sort; it returns the item in the first position of the list as it currently exists.
Frequently Asked Questions
What does List.First return if the list is empty?
If you provide a default value as the second argument, it returns that default. If you omit the default, it returns an error.
Can List.First return multiple items?
No, it returns a single item. To get multiple items, use List.FirstN with a count.
Does List.First work on a list of records?
Yes, it returns the first record in the list. You can then access fields using the field access operator or expand the record.
Is List.First the same as using {0} on a list?
They both return the first item, but List.First allows an optional default value for empty lists, while {0} on an empty list returns an error.
Can I use List.First in a custom column formula?
Yes, you can use it in the formula bar when adding a custom column, as long as the column you reference is a list. For example, List.First([MyListColumn]).
What happens if the list contains null as the first item?
List.First returns null. The default value is only used when the list is empty, not when the first item is null.