There are a couple of nice new features to do with table joins (or merges as they are known in M) and query folding in Power Query in the April release of Power BI Desktop that I want to highlight.
Anti Joins now fold
First of all, a few months ago I wrote a post about how the built-in anti join functionality didn’t fold in Power Query. The good news is that it now does on SQL Server-related sources, so no more workarounds are needed. For example, if you have two tables in a SQL Server database called Fruit1 and Fruit2 and two Power Query queries that get data from those tables:
…then the following M code:
let
Source = Table.Join(
Fruit1,
{"Fruit1"},
Fruit2,
{"Fruit2"},
JoinKind.LeftAnti
)
in
Source
…returns the following table of fruits that are in the Fruit1 table and not in the Fruit2 table:
Of course that’s what the code above returned in previous versions of Power Query too. The difference now is that query folding occurs and the following SQL code is generated:
select [$Outer].[Fruit1],
cast(null as nvarchar(50)) as [Fruit2]
from
(
select [_].[Fruit] as [Fruit1]
from [dbo].[Fruit1] as [_]
) as [$Outer]
where not exists
(
select 1
from
(
select [_].[Fruit] as [Fruit2]
from [dbo].[Fruit2] as [_]
) as [$Inner]
where [$Outer].[Fruit1] = [$Inner].[Fruit2] or [$Outer].[Fruit1] is null and [$Inner].[Fruit2] is null
New join kind: semi joins
There are also two brand new join kind you can use in the Table.Join and Table.NestedJoin functions: JoinKind.LeftSemi and JoinKind.RightSemi. Semi joins allow you to select the rows in one table that have matching values in another table. Using the Fruit1 and Fruit2 tables above, the following M code:
let
Source = Table.Join(
Fruit1,
{"Fruit1"},
Fruit2,
{"Fruit2"},
JoinKind.LeftSemi
)
in
Source
Returns all the rows in Fruit1 where there is a matching value in Fruit2:
Here’s the SQL that is generated:
select [$Outer].[Fruit1],
cast(null as nvarchar(50)) as [Fruit2]
from
(
select [_].[Fruit] as [Fruit1]
from [dbo].[Fruit1] as [_]
) as [$Outer]
where exists
(
select 1
from
(
select [_].[Fruit] as [Fruit2]
from [dbo].[Fruit2] as [_]
) as [$Inner]
where [$Outer].[Fruit1] = [$Inner].[Fruit2] or [$Outer].[Fruit1] is null and [$Inner].[Fruit2] is null
The ?? operator now folds
The M language’s ?? coalesce operator is used for replacing null values and this now folds on SQL Server-related sources too now. For example, the M query in the previous section that did a semi join on Fruit1 and Fruit2 returns a table where all the rows in the Fruit2 colum contain null values. The following M query adds a new custom column that returns the text value “Nothing” when the Fruit2 column contains a null:
let
Source = Table.Join(
Fruit1,
{"Fruit1"},
Fruit2,
{"Fruit2"},
JoinKind.LeftSemi
),
ReplaceNulls = Table.AddColumn(
Source,
"NullReplacement",
each [Fruit2] ?? "Nothing"
)
in
ReplaceNulls
Here’s the SQL generated for this, where the ?? operator is folded to a CASE statement:
select [_].[Fruit1] as [Fruit1],
[_].[Fruit2] as [Fruit2],
case
when [_].[Fruit2] is null
then 'Nothing'
else [_].[Fruit2]
end as [NullReplacement]
from
(
select [$Outer].[Fruit1],
cast(null as nvarchar(50)) as [Fruit2]
from
(
select [_].[Fruit] as [Fruit1]
from [dbo].[Fruit1] as [_]
) as [$Outer]
where exists
(
select 1
from
(
select [_].[Fruit] as [Fruit2]
from [dbo].[Fruit2] as [_]
) as [$Inner]
where [$Outer].[Fruit1] = [$Inner].[Fruit2] or [$Outer].[Fruit1] is null and [$Inner].[Fruit2] is null
)
) as [_]
[Thanks to Curt Hagenlocher for the information in this post]