When to use this
Sometimes your data is in the wrong shape for the analysis you need. Pivoting turns distinct row values into columns (e.g., turning monthly rows into one column per month). Unpivoting does the reverse — turning columns into rows (e.g., turningq1_revenue, q2_revenue, q3_revenue columns into quarter and revenue rows).
Sample input — Pivot
Amonthly_sales table in the Raw layer, with one row per product per month:
We want one row per product with a column for each month.
Sample input — Unpivot
Aquarterly_revenue table where each quarter is a separate column:
We want to normalize this into
product, quarter, and revenue columns.
Implementation — Pivot
- Nekt Express / BigQuery
- Athena SQL
- Python (Nekt SDK)
BigQuery supports a native
PIVOT clause for cleaner syntax.Pivot — Expected output
Implementation — Unpivot
- Nekt Express / BigQuery
- Athena SQL
- Python (Nekt SDK)
BigQuery supports a native
UNPIVOT clause.Unpivot — Expected output
Tips and gotchas
After pivoting, column names come from data values and may contain spaces, special characters, or start with numbers. Clean them up with Rename columns if needed.