Full Outer Join
According to Wikipedia -
“Conceptually, a full outer join combines the effect of applying both left and right outer joins. Where rows in the FULL OUTER JOINed tables do not match, the result set will have NULL values for every column of the table that lacks a matching row. For those rows that do match, a single row will be produced in the result set (containing columns populated from both tables).”
So, the Full Outer Join can be acheived by creating -
- Left Outer Join
- Right Anti Join
= +
To begin with, we have Department table as -
DepID | Dep Name | |
---|---|---|
0 | 1 | Marketing |
1 | 2 | HR |
2 | 4 | Finance |
3 | 5 | Operations |
and Employee tables as -
Emp Id | Name | Income | DepID | |
---|---|---|---|---|
0 | 1 | Shivani | 200 | 1 |
1 | 2 | Rob | 133 | 1 |
2 | 3 | Chris | 190 | 2 |
3 | 4 | Tom | 200 | 2 |
4 | 5 | Ria | 120 | 3 |
The two tables relates on DepID column with one to many relatioship between Department to Employee table.
The DAX query for the same in PowerBI can be written as -
FullOuterJoin = UNION(
var DepartmentLeftOuterJoinEmp = NATURALLEFTOUTERJOIN(Department,RELATEDTABLE(Employee))
return SELECTCOLUMNS(DepartmentLeftOuterJoinEmp,
"DepID", Department[DepID],
"EmpID", [Emp Id],
"Income", [Income],
"Name",[Name],
"DepName",[Dep Name]
),
var DepartmentUniqueIds = DISTINCT(Department[DepID])
return SELECTCOLUMNS(CALCULATETABLE(Employee, NOT(Employee[DepID] in DepartmentUniqueIds)),
"DepID", [DepID],
"EmpID", [Emp Id],
"Income", [Income],
"Name",[Name],
"DepName"," "
)
)
Output:
DepID | Dep Name | Emp Id | Name | Income | |
---|---|---|---|---|---|
0 | 1 | Marketing | 1 | Shivani | 200 |
1 | 1 | Marketing | 2 | Rob | 133 |
2 | 2 | HR | 3 | Chris | 190 |
3 | 2 | HR | 4 | Tom | 200 |
4 | 3 | 5 | Ria | 120 | |
5 | 4 | Finance | |||
6 | 5 | Operations |
Note:
- Another way to solve the same can be: Left Outer Join + Right Outer Join - Inner Join
This can be achived in DAX in PowerBI by using: Distinct(Union(LeftOuterJoin,RightOuterJoin))