GENERATE
Each row of a table joined with the rows a second table expression gives for it; rows that get none left out.
Group: Table manipulation. Compatible with DAX.
Syntax
GENERATE ( <table1>, <table2> )
| Parameter | Type | Description |
|---|---|---|
table1 | Table | The rows to start from. |
table2 | Table evaluated for each row | The table worked out for each row of the first; it can use that row's columns. |
Returns
Table. Each row of the first table paired with each row its second table gives, the columns of both side by side.
Remarks
- A row whose second table is empty does not appear at all.
GENERATEALLlists it once, with blanks. - The second table sees the first's row as a row context only. Table functions such as
VALUESignore a row context: wrap them inCALCULATETABLEto turn the row into a filter, as the second example does. - The two tables must not share a column name.
- When the second table does not depend on the row, the result is the same as
CROSSJOIN.
Examples
One row per crate
Each shop gets a row for every crate slot it fills; Storo fills none and drops out.
EVALUATE
VAR Shops =
DATATABLE (
"Shop", STRING, "Crates", INTEGER,
{ { "Frogner", 2 }, { "Storo", 0 }, { "Sagene", 1 } }
)
VAR Slots = DATATABLE ( "Slot", INTEGER, { { 1 }, { 2 }, { 3 } } )
RETURN
GENERATE ( Shops, FILTER ( Slots, [Slot] <= [Crates] ) )
ORDER BY [Shop], [Slot]
| Shop | Crates | Slot |
|---|---|---|
| Frogner | 2 | 1 |
| Frogner | 2 | 2 |
| Sagene | 1 | 1 |
The best-selling product of each category
EVALUATE
GENERATE (
VALUES ( Products[Category] ),
CALCULATETABLE ( TOPN ( 1, VALUES ( Products[Product] ), [Liters sold] ) )
)
ORDER BY Products[Category]
| Products[Category] | Products[Product] |
|---|---|
| Beer | Pils |
| Cider | Cider |