Power Query is often the engine used for combining data tables, especially using Merge or Append. However, sometimes, you might need to do that operation in DAX. An example of that is when you want to create that combination only virtually as part of a measure calculation that evaluates dynamically. In this article, I’ll explain three DAX functions and what are their meanings: Union, Except, and Intersect.
I have two really simple data tables, each with one column: Column 1;
For the operations below, each table can have more than one column. However, I keep it simple to understand.
If you want to have all the data rows of the two tables appended to each other, you can use UNION function in DAX. This function, simply gets the two input tables, and returns the appended result.
The Union function is a tabular function, and cannot be used directly in a measure. it has to be used either in a calculated table or inside another function.
Union function does not remove duplicate values that might happen after the operation. You can use Distinct, or Values functions for that.
Intersect only returns the rows that exist in both tables. All of those rows that exist in only one of the tables will be removed from the resultset. This is how you can use Intersect;
As you can see, the syntax that INTERSECT and UNION are used are exactly the same. The same rule applies on EXCEPT as well. For these three functions, you just need two input parameters; the two tables.
For the UNION and INTERSECT, the order of passing the tables to the function doesn’t matter (the only impact would be the final order of items in the result set). However, for the Except, the order of tables is important.
If you want all rows from table1 that does not exist in table2, then you can write as below;
The result would be all rows that only exist in table1.
If you change the order of tables, then you get a different result;
This would be all rows that exists in table2 only.
In all of the functions above, you need two tables to have the same structure. The same structure means the same number of columns. The matching is based on the position of the column in the table.
If you are using the techniques above to create a calculated table, I strongly recommend you to have a look at Append and Merge transformations in Power Query. Often they can be a much better option if the purpose of this work is transformation. Only use it in DAX if you are targeting a dynamic combine approach.