Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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:

BrandProductMonthSales
Super-coolPaper towels
AAATowelsJanuary95
Super-coolPaper towels
AAANapkinsJanuary113
Just-cool
BTowelsJanuary99
Just-cool
BTissuesJanuary88
Super-coolPaper towels
AAATowelsFebruary108
Super-cool
AAANapkinsFebruary91
Just-coolPaper towels
BTowelsFebruary85
Just-cool
BTissuesFebruary105
Super-coolPaper towels
AAATowelsMarch81
Super-cool
AAANapkinsMarch92
Just-coolPaper towels
BTowelsMarch112
Just-cool
BTissuesMarch104

Transform:

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-coolAAA 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:

Month
average
avg_Sales_
Just_cool
B_
Paper_
Towels
average
avg_Sales_
Just_cool_Paper
B_Tissues
average
avg_Sales_
Super
AAA_
cool_
Napkins
average
avg_Sales_
Super_cool_Paper
AAA_Towels
January998811395
February8510591108
March1121049281

Example - Aggregate Values

...