Pivot
Spread the values of one column across the top of the table. Get one row for each group.
What it does
Pivot builds a cross-tab. Each value in the column you choose becomes a new column header. Each cell holds one aggregated number.
Before. One row for each region and category:
| region | category | revenue |
|---|---|---|
| North | Laptops | 1200 |
| North | Phones | 800 |
| South | Laptops | 950 |
| South | Phones | 400 |
After. Rows = region, Columns = category, Values = SUM of revenue:
| region | Laptops | Phones |
|---|---|---|
| North | 1200 | 800 |
| South | 950 | 400 |
The three fields
- Rows (group by): the columns that stay on the left. You can pick more than one.
- Columns (pivot on): one column. Each of its values becomes a header.
- Values: the column to aggregate, plus the function to use.
Build a pivot table
- Type pivot in the Search transforms box, in the Pipeline panel.
- Select Pivot in the results. The panel opens below the grid.
- Open Select row grouping columns (leave empty for all)… and pick your row columns.
- Open Choose column whose values become headers… and pick one column.
- In Values, click a function: COUNT, SUM, AVG, MIN or MAX.
- Open Choose value column… and pick the column to aggregate.
- Read Pivot Preview. It shows up to five result rows.
- Click Apply.
The grid updates at once. The step appears in your pipeline, where you can edit or delete it later.
Which function to use
- COUNT counts the rows in each cell. It works with any column.
- SUM and AVG need a number column.
- MIN and MAX give the smallest or the largest value in the cell.
Tips
- Click SUM or AVG first. The Values list then shows only number columns.
- Leave Rows (group by) empty to group by all the other columns.
- Each column that you use disappears from the other two lists. This stops a double pick.
- Every different value in the header column becomes a new column. Check the value count first with the small grid icon next to the column name.
- Use Unpivot to turn a pivot table back into long rows.
For SQL users
The step runs as a DuckDB PIVOT statement:
PIVOT (SELECT "region", "category", "revenue" FROM data) ON "category" USING SUM("revenue")
Try Pivot with sample data →
Related Operations
- Window Function - Rank, row number, lead, lag
- Smallest - Get nth smallest value as a column
- Largest - Get nth largest value as a column