How to combine data tables in DAX in Power BI using the UNION command
This guide shows you how to create a seamless data set from 2 or more tables to make further analysis and reporting much easier
https://www.selectdistinct.co.uk/2023/10/04/union-in-dax/
#PowerBI #DAX #UNION
2. Here we are looking at how to combine data in
DAX using the UNION command
This is important if you need to consolidate data
from different sources into a seamless data set
3. Assuming we have two
different sales data sources
We want to combine Sales A
with the data in Sales B
(Notice that Store ID 5 appears
in both)
Sales A Sales B
4. We already have two similar
tables from two different
sources already loaded into
Power BI
5. To create a new UNION table,
go into the table view pane
and click 'New Table'
The syntax is:
New Table Name = UNION(table1,table2)
We will call the new table 'Sales Combined'
In the DAX formula bar we enter
Sales Combined = UNION(SalesA,SalesB)
6. Once you press enter the DAX code runs and
the new table is produced
We can see that the new table contain all 5
rows from SalesA and all 5 rows of SalesB
This is very straight forward in this example and
simply combines the two datasets in a simple
way
7. For UNION to work there are a few considerations that must be in place, and these are
essentially the same as UNION ALL in SQL
The columns MUST be in the same order in both tables, if they are not it is possible that
the UNION could still run, but give strange mixed results
The data types of the columns must be compatible
Column names are inherited from the first table, they do not have to be the same
Tables must have the same number of columns
Duplicates are not eliminated (we will explore this)
Important points to note
8. To demonstrate this we firstly need to remove our Source column
SELECTCOLUMNS(SalesA,"Product_ID",SalesA[Product_ID], "Store_ID",
SalesA[Store_ID], "qty_sold", SalesA[qty_sold])
This DAX code just selects the three columns we need
Sales Combined = union(SELECTCOLUMNS(SalesA,"Product_ID",SalesA[Product_ID],
"Store_ID", SalesA[Store_ID], "qty_sold",
SalesA[qty_sold]),SELECTCOLUMNS(SalesB,"Product_ID",SalesB[Product_ID],
"Store_ID", SalesB[Store_ID], "qty_sold", SalesB[qty_sold]))
How to remove duplicates?
9. To remove the duplicate for store 5, we can now use the DISTINCT
command
Sales Combined =
DISTINCT (
union(SELECTCOLUMNS(SalesA,"Product_ID",SalesA[Product_ID], "Store_ID", SalesA[Store_ID],
"qty_sold", SalesA[qty_sold]),SELECTCOLUMNS(SalesB,"Product_ID",SalesB[Product_ID],
"Store_ID", SalesB[Store_ID], "qty_sold", SalesB[qty_sold]))
)
This results in 9 rows
10. UNION with more than two tables
Union needs to have at least 2 table references, but it is
not limited to a fixed number
If we want to union three tables we just add more
Sales Combined Three = UNION(SalesA,SalesB,SalesA)
This example also shows that you can repeat the same
table if you need to
11. With this way to combine data tables in Power BI you are now equipped
to perform your data consolidation
Having a seamless integration of multiple tables or queries is a great way
to simplify onward analysis
12. For more Tips, Tricks and
Timesavers, visit our website
Business Analytics Blog – Select Distinct
Credit: simon.harrison@selectdistinct.co.uk