Value.NullableEquals

D

T

The M Code Behind the Power Query M function Value.NullableEquals

In this article, we will take a deep dive into the M code behind the `Value.NullableEquals` function, understand how it works, and explore some use cases.

Understanding the `Value.NullableEquals` Function

The `Value.NullableEquals` function takes two arguments and returns a boolean indicating whether the values are equal. The function can handle null values, which makes it particularly useful.

The syntax for the function is as follows:


Value.NullableEquals(value1 as any, value2 as any) as logical


The `value1` and `value2` arguments can be any value. If either of the arguments is null, the function returns false. However, if both arguments are null, the function returns true.

Let’s take a look at some examples to understand how the function works.

Example 1: Comparing Two Non-Null Values

Suppose we want to compare two non-null values, 10 and 20. We can use the `Value.NullableEquals` function as follows:


Value.NullableEquals(10, 20)


The function will return `false` because the two values are not equal.

Example 2: Comparing a Null Value with a Non-Null Value

Suppose we want to compare a null value with a non-null value, say null and 10. We can use the `Value.NullableEquals` function as follows:


Value.NullableEquals(null, 10)


The function will return `false` because one of the values is null.

Example 3: Comparing Two Null Values

Suppose we want to compare two null values. We can use the `Value.NullableEquals` function as follows:


Value.NullableEquals(null, null)


The function will return `true` because both values are null.

Use Cases for the `Value.NullableEquals` Function

The `Value.NullableEquals` function is useful in various scenarios. Here are some use cases:

Use Case 1: Joining Tables with Null Values

Suppose we have two tables with some null values, and we want to join them using specific criteria. We can use the `Value.NullableEquals` function to handle null values in the join.

For example, suppose we have two tables, `Table1` and `Table2`, with the following data:

| Table1 | Table2 |

|—————|—————|

| 1, null, A | 1, 10, X |

| 2, 20, B | 2, null, Y |

| 3, 30, C | 3, 30, Z |

Suppose we want to join these tables on the first column, but we also want to include rows where the first column is null. We can use the following code:


let

T1 = Table.FromRecords({

[Col1 = 1, Col2 = null, Col3 = "A"],

[Col1 = 2, Col2 = 20, Col3 = "B"],

[Col1 = 3, Col2 = 30, Col3 = "C"]

}),

T2 = Table.FromRecords({

[Col1 = 1, Col2 = 10, Col3 = "X"],

[Col1 = 2, Col2 = null, Col3 = "Y"],

[Col1 = 3, Col2 = 30, Col3 = "Z"]

}),

Joined = Table.Join(

T1, {"Col1"}, T2, {"Col1"},

JoinKind.FullOuter,

(Left, Right) => Value.NullableEquals(Left[Col1], Right[Col1])

)

in

Joined


In the join condition, we use the `Value.NullableEquals` function to compare the values in the first column of the two tables, handling null values gracefully.

Use Case 2: Filtering Rows with Null Values

Suppose we have a table with some null values, and we want to filter the rows where a specific column has a null value. We can use the `Value.NullableEquals` function to achieve this.

For example, suppose we have a table `Table1` with the following data:

| Col1 | Col2 | Col3 |

|——|——|——|

| 1 | null | A |

| 2 | 20 | B |

| 3 | 30 | C |

| 4 | null | D |

Suppose we want to filter the rows where `Col2` has a null value. We can use the following code:


let

T1 = Table.FromRecords({

[Col1 = 1, Col2 = null, Col3 = "A"],

[Col1 = 2, Col2 = 20, Col3 = "B"],

[Col1 = 3, Col2 = 30, Col3 = "C"],

[Col1 = 4, Col2 = null, Col3 = "D"]

}),

Filtered = Table.SelectRows(

T1,

each Value.NullableEquals([Col2], null)

)

in

Filtered


In the filter condition, we use the `Value.NullableEquals` function to compare the value in `Col2` with null.

The `Value.NullableEquals` function is a powerful tool in the Power Query M language that allows users to compare two values, including null values. This function is useful in scenarios where one needs to compare two values and take different actions based on the comparison result. We hope this article helps you understand the M code behind the `Value.NullableEquals` function and provides some insights into its use cases.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)