UNION
Stacks the rows of two or more tables with the same number of columns, duplicates included.
Group: Table manipulation. Compatible with DAX.
Syntax
UNION ( <table_expression1>, <table_expression2> [, <table_expression>] … )
| Parameter | Type | Description |
|---|---|---|
table_expression1 | Table | The first table; its column names are the result's. |
table_expression2 | Table | The second table, with as many columns as the first. |
table_expression | Table optional, repeatable | Further tables, again with as many columns. |
Returns
Table. The rows of all the tables, one table after another, under the first table's column names.
Remarks
- Columns are matched by position: each table's first column goes into the first column of the result, whatever its name. A different number of columns is an error.
- Duplicates are not removed. Wrap it in
DISTINCTto get each row once. - When one table's column is text and another's holds numbers or dates, the column becomes text: a whole number and a text give text. When none is text but one holds values of different kinds, such as
IFERROR ( x, "error" )or an IF that gives a number or a text, each value keeps its own type. - A column stays tied to the model only when it comes from the same model column in every table; otherwise it holds plain values.
Examples
Two lists of shops
Frogner is in both and appears twice. The second table's column names do not matter.
EVALUATE
VAR Oslo =
DATATABLE ( "Shop", STRING, "Liters", INTEGER, { { "Frogner", 120 }, { "Storo", 80 } } )
VAR Bergen =
DATATABLE ( "Name", STRING, "Volume", INTEGER, { { "Bryggen", 95 }, { "Frogner", 120 } } )
RETURN
UNION ( Oslo, Bergen )
ORDER BY [Shop]
| Shop | Liters |
|---|---|
| Bryggen | 95 |
| Frogner | 120 |
| Frogner | 120 |
| Storo | 80 |
A number and a text in one column
EVALUATE
VAR Numbers = DATATABLE ( "Code", INTEGER, { { 1 } } )
VAR Texts = DATATABLE ( "Code", STRING, { { "x" } } )
RETURN
UNION ( Numbers, Texts )
ORDER BY [Code]
| Code |
|---|
| 1 |
| x |
A union of the same column filters it
EVALUATE
ROW (
"Pils and IPA",
CALCULATE (
[Liters sold],
UNION (
FILTER ( VALUES ( Products[Product] ), Products[Product] = "Pils" ),
FILTER ( VALUES ( Products[Product] ), Products[Product] = "IPA" )
)
)
)
| Pils and IPA |
|---|
| 1336210 |