Unpivot
SQL Gem
Use the Unpivot Gem to transform your data from a wide format to a long format.
Parameters
Parameter | Description |
---|---|
Column(s) to use as identifiers | The column(s) that will identify to which group or entity the observation corresponds to. |
Columns to unpivot | The columns (wide format) that you would like to transform into a single column (long format). |
Variable column name | The name of the column that contains the names of the unpivoted columns. This helps describe the values in the value column. |
Value column name | The name of the column that will contain the values from the unpivoted columns. |
Example
Transforming your data into a long format can be beneficial when creating visualizations, comparing variables, handling dynamic data, and more.
Let's think about a time series example. If you have product sales data in a wide format, you may want to transform it into a long format before modeling the time series and analyzing the seasonal patterns in sales.
The image below shows sample input and output tables for this scenario.
This table describes how this transformation was achieved:
Parameter | Input |
---|---|
Column(s) to use as identifiers | The Product column is the identifier because it defines which product the sales correspond to. |
Columns to unpivot | All of the quarterly sales columns will be unpivoted. |
Variable column name | The variable column is named Quarter because it identifies the sales period. |
Value column name | The value column is named UnitsSold because it contains information about number of units sold. |