Daily DAX : Day 406 UNION

DAX UNION Function in Power BI

Syntax

UNION(<table_expression1>, <table_expression2> [, <table_expression3>, …])

What It Does

The UNION function combines multiple tables into a single table by stacking rows vertically (row-wise append).

It is the DAX equivalent of SQL's UNION ALL — it includes duplicate rows unless you wrap it with DISTINCT or SUMMARIZECOLUMNS.

Key Requirements

  • All input tables must have the same number of columns
  • Corresponding columns must have compatible data types
  • Column names in the result come from the first table

Common Use Cases

Use Case Description Example
Combine similar data from multiple tables Merge sales data from different regions or years stored in separate tables Union East_Sales and West_Sales tables
Create a dynamic calendar or date table Combine custom holidays, fiscal periods, etc. Add company-specific non-working days to standard calendar
Append filtered or calculated tables Add a "Total" or "Budget" row to actual data Show actual sales + budget row in the same visual
Build scenario analysis tables Combine Actuals, Budget, Forecast into one table with a Scenario column Switchable visuals using a scenario slicer

Practical Example: Actuals + Budget + Forecast

// Step 1: Create base tables with same structure
Actuals = 
SELECTCOLUMNS(
    Sales,
    "Year", YEAR(Sales[Date]),
    "Category", Sales[Product Category],
    "Amount", Sales[Sales Amount],
    "Scenario", "Actual"
)

Budget = 
SELECTCOLUMNS(
    BudgetTable,
    "Year", BudgetTable[Year],
    "Category", BudgetTable[Category],
    "Amount", BudgetTable[Budget Amount],
    "Scenario", "Budget"
)

Forecast = 
ADDCOLUMNS(
    SUMMARIZECOLUMNS(
        Sales[Product Category],
        "Amount", SUM(Sales[Forecast Amount])
    ),
    "Year", 2025,
    "Scenario", "Forecast"
)

// Step 2: Union them all
Sales_AllScenarios = UNION(Actuals, Budget, Forecast)
Tip: Always use SELECTCOLUMNS or ADDCOLUMNS to ensure column order, names, and data types match perfectly before using UNION.

When NOT to Use UNION

  • If you need to join tables side-by-side → use NATURALINNERJOIN, CROSSJOIN, etc.
  • If you want automatic removal of duplicates → wrap with DISTINCT(UNION(...)) (like SQL UNION)

UNION = Stack tables vertically
Perfect for scenario modeling, combining disjoint datasets, and building flexible reporting tables in Power BI.

Comments

Popular posts from this blog

Daily DAX : Day 131 SELECTEDMEASURE

Daily DAX : Day 446 INFO.CSDLMETADATA

Daily DAX : Day 453 ENDOFWEEK