List.SingleOrDefault Function in Power Query
The List.SingleOrDefault function in Power Query returns a single item from a list, when there is only one item in the list. If the list is empty, the function returns null unless an optional default is specified. If there is more than one item in the list, the function returns an error.
Syntax
List.SingleOrDefault( list as list, optional defaultValue as any ) as any
The function has the following parameters:
- list: The list to search.
- defaultValue: It is an optional parameter. Value to return if no item is found. If not provided, it returns null by default.
Example: Single value in the list.
Power Query M
let Source = List.SingleOrDefault({3}) in Source
The output of the above code is 3.
Example: Empty list with default null.
Power Query M
let Source = List.SingleOrDefault({}) in Source
The output of the above code is null.
Example: Empty list with a custom default value.
Power Query M
let Source = List.SingleOrDefault({}, "Ashish") in Source
The output of the above code is "Ashish". If the given list is empty, it returns the specified default value.