...
Example - Multi-column Pivot
The pivot
transform transform supports the ability to use the values in multiple columns to specify the columns that are generated. When two or more columns are used in the pivot, columns containing values of all possible combinations from the source columns are generated.
In the following source data, sales data on individual products is organized by brand, product name, and month:
Super-cool | Paper towelsSuper-coolPaper towelsJust-cool | Just-coolSuper-cool | Paper towelsSuper-coolJust-cool | Paper towelsJust-coolSuper-cool | Paper towelsSuper-coolJust-cool | Paper towelsJust-coolTransform:
If you wanted to create summary sales information for each product by month, you might choose to create a pivot
on on the Product
column. However, if you look at the column values, you might notice that the Paper towels
product product is available for both the Super-cool
and Just-cool
AAA
and B
brands. In this case, you must perform a multi-column pivot on these two columns, like the following:
D code |
---|
pivot col:Brand,Product value:AVERAGE(Sales) group:Month |
Results:
averageJust_coolPaper_ averageJust_cool_Paper averageSupercool_ averageSuper_cool_PaperAAA_Towels |
---|
January | 99 | 88 | 113 | 95 |
February | 85 | 105 | 91 | 108 |
March | 112 | 104 | 92 | 81 |
Example - Aggregate Values
...