Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query List.SingleOrDefault: Step-by-Step Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

List.SingleOrDefault returns the only item in a list, or a default value (null unless you specify one) if the list is empty. It throws an error if the list contains more than one item. Use it when you expect exactly one match and want a clean fallback instead of an error when nothing is found. You need Power Query in Excel or Power BI Desktop; no extra installation is required.

What You'll Need

  • Power Query Editor open in Excel (Data > Get & Transform Data) or Power BI Desktop (Transform Data).
  • A query with a list, or the ability to create one (for example, by referencing a column or using a list literal).
  • Basic familiarity with the Power Query formula bar and the Advanced Editor.

Step-by-Step

  1. Open the Power Query Editor. In Excel, select any cell in your data and go to Data > Get & Transform Data > From Table/Range. In Power BI Desktop, click Transform Data on the Home ribbon. The exact menu labels can differ slightly by version or region.
  2. Create or locate a list. A list is a Power Query value enclosed in curly braces, such as {1, 2, 3} , or a column reference like MyTable[MyColumn] . You can also use a step that outputs a list, for example .
  3. Decide on a default value. If the list is empty, List.SingleOrDefault returns null by default. To return something else, pass a second argument, for example List.SingleOrDefault(MyList, "Not found").
  4. Write the formula. In the formula bar, or by adding a step in the Advanced Editor, type List.SingleOrDefault(MyList) or with the optional default: List.SingleOrDefault(MyList, "Default"). Replace MyList with your actual list expression.
  5. Test with different list sizes. Create a small query to see behavior: a one-item list returns that item; an empty list returns null or your default; a two-item list raises Expression.Error: The list contains more than one item. This confirms the function is working as intended.
  6. Handle the error case. If your list might contain multiple items, wrap the call in try ... otherwise ... to catch the error and return a fallback, for example try List.SingleOrDefault(MyList) otherwise "Multiple items".
  7. Apply the result. Use the output in a subsequent step, such as a column value, a parameter, or a filter condition. For instance, you can store the single product name in a variable and reference it later.

Common Mistakes

  • Assuming it returns the first item. List.SingleOrDefault does not pick the first; it requires exactly one item or none. If you need the first, use List.First.
  • Forgetting the default argument. Without a second argument, an empty list returns null, which may cause downstream errors if you expect text or a number.
  • Not handling multiple items. The function throws an error when the list has more than one item. Use try ... otherwise or check List.Count first.
  • Passing a table instead of a list. List.SingleOrDefault expects a list. Convert a table column to a list with Table.Column(MyTable, "ColumnName") or use the column reference syntax.
  • Confusing null with an empty string. A default of "" is different from null; choose deliberately based on how you will use the result.

Tips

  • Use List.SingleOrDefault when you are certain a lookup yields at most one row, such as filtering by a unique ID.
  • Combine with List.Select to narrow a list before calling: List.SingleOrDefault(List.Select(MyList, each _ = "Target")).
  • For clarity, assign the list to a step name (for example, FilteredList) and reference that name in the function call.
  • If you need a default only for empty lists but still want an error for multiple items, the two-argument form does exactly that.
  • Document your default value in a comment or step name so others understand the fallback behavior.

Troubleshooting

  • Error: "The list contains more than one item." Your list has duplicates or multiple matches. Check the source data, apply List.Distinct, or use try ... otherwise to handle it gracefully.
  • Result is null unexpectedly. The list is empty, or your filter removed all items. Verify the list count with List.Count and adjust the filter or default.
  • Function not recognized. Ensure you are in Power Query (not DAX) and that the function name is spelled correctly. List.SingleOrDefault is available in Power Query M; it is not a DAX function.
  • Type mismatch. The default value should match the expected type of the list items. If the list contains numbers, use a numeric default like 0; for text, use "".
  • Unexpected error in downstream step. The function may have returned null, which then breaks a later operation. Use try ... otherwise around the downstream step or provide a non-null default.

Frequently Asked Questions

What is the difference between List.Single and List.SingleOrDefault?

List.Single throws an error if the list is empty or has more than one item. List.SingleOrDefault returns null (or a specified default) for an empty list, but still throws an error if there is more than one item.

Can I use List.SingleOrDefault on a table column?

Yes, but you must convert the column to a list first, for example with Table.Column(MyTable, "ColumnName"). The function expects a list, not a table.

Does List.SingleOrDefault work in Power BI Desktop?

Yes, it is part of the Power Query M language and works in both Excel Power Query and Power BI Desktop. It is not available in DAX.

How do I return a custom value when the list is empty?

Pass a second argument: List.SingleOrDefault(MyList, "Default"). This returns "Default" if the list is empty, and still errors if there are multiple items.

What happens if the list has multiple items?

The function raises an error: "The list contains more than one item." You can catch this with try ... otherwise to return a fallback value instead.

Is List.SingleOrDefault case-sensitive?

The function itself does not compare values; it simply returns the single item. Case sensitivity depends on how you built the list (for example, using List.Select with a comparison).