The COALESCE Operator in M / Power Query

Coalesce is an operator that allows you to return a different value instead of null. When the value on the left-hand side of the operator is null, it returns the value on the rigth side of the operator. When is this useful?

When performing calculations in Power Query, a common requirement is to provide a default value instead of null. Null is a special case that represents that absence of a value, and is different from zero.

The tricky part is that some operators don’t know how to work with these values. For example, 5 + null = null, whereas you may have expected 5. For this reason (and others) it’s useful to know how to work with null.

One way to instruct what to do with null values, is by using an if statement. You can for example write:

= if [Sales Amount] = null then 0 else [Sales Amount]

In cases where Power Query finds a null it then returns zero instead. Yet there are easier ways.

The Coalesce Operator

Since handling nulls is a common requirement, the M-language also offers the Coalesce Operator to achieve the same. The coalesce operator is represented by ??.

This operator works as a COALESCE function but technically is not a function. You can write the above expression with the coalesce operator in the following way:

= [Sales Amount] ?? 0

If we describe this in words it says: try to return the [Sales Amount] and if this value equals null then return the default value 0. Using the coalesce operator makes the code shorter and more readable.

If you want to add more columns to your formula you can add the coalesce operator multiple times. Let’s say you want to return the first value that does not equal null from the columns period 4, 3, 2 or 1 respectively. And if all values are null then return a default value of 500. You can do that by writing:

if [Sales P4] <> null then
   [Sales P4]
   else if [Sales P3] <> null then
           [Sales P3]
           else if [Sales P2] <> null then
                   [Sales P2]
                   else if [Sales P1] <> null then
                           [Sales P1]
                           else 500
Elaborate if statement instead of Coalesce

The code is quite long and difficult to maintain. With the coalesce operator you can rewrite the formula to:

[Sales P4]  ??  [Sales P3]  ??  [Sales P2] ?? [Sales P1] ?? 500
Statement with Coalesce Operator in Power Query

This function returns the first column that does not return blank. And if all columns equal null it returns the default value of 500.

Coalesce Alternative

In case you want to get creative, there are alternative ways to replicate the coalesce function. You can use any of these to get identical results:

= List.First( List.RemoveNulls( { null, null, 1, 2 } ) )

= List.First( List.RemoveFirstN( { null, null, 1, 2 }, each _ = null ) )

= List.RemoveFirstN( { null, null, 1, 2 }, each _ = null ){0}

= List.Select( { null, null, 1, 2 }, each _ <> null ){0}

= null ?? null ?? 1 ?? 2

For learning purpose you can find those here, but in the end coalesce operator (??) is best-practice.

Under the hood the coalesce operator performs an if-condition. From a performance perspective you should not expect this code to perform any quicker than the if-statement. Yet it makes your code much shorter and more readable.

If you’re interested in other ways to shorten your code, make sure to check out this article that shows how to mimic an IN operator (multiple OR statements).

Enjoy Power Query!

Recommend Reading

>>> Power Query - Foundations <<<List Functions (200+ examples)Text functions (150+ examples)Creating Tables from Scratch (40+ examples)Understanding If Statements 
Generating a Date Table


>>>  Power Query - Advanced Topics <<<
Master List.Generate with Easy Examples
 Deep Dive into List.Accumulate
Create Calendar with Dynamic Language
Replacing Values (User Interface and Beyond)
Share on:
About Rick de Groot

Rick is the founder and editor of BI Gorilla. He believes learning is one of the great pleasures in life and wants to share his knowledge to help you improve your skills.

Learn more about him here, connect with him on Twitter, Facebook and LinkedIn and subscribe to his YouTube Channel.

2 thoughts on “The COALESCE Operator in M / Power Query”

Leave a comment