PIVOT
Transforms rows into columns, creating a cross-tabulation summary of data.
Syntax
result = PIVOT data, row_column, column_column, value_column
result = PIVOT data, row_column, column_column, value_column, aggregate
Parameters
| Parameter | Type | Description |
|---|---|---|
data | Array | The data array to pivot |
row_column | String | Column to use for row headers |
column_column | String | Column to use for column headers |
value_column | String | Column containing values to aggregate |
aggregate | String | Aggregate function: SUM, AVG, COUNT, MIN, MAX (default: SUM) |
Description
PIVOT reorganizes data from a normalized format into a cross-tabulation format, making it easier to compare values across two dimensions. This is useful for creating summary reports and dashboards.
Examples
Basic Pivot
sales = FIND "sales", "year=2025"
summary = PIVOT sales, "region", "quarter", "amount"
' Result: regions as rows, quarters as columns
' Shows total sales for each region/quarter combination
With Aggregate Function
orders = FIND "orders", "status=completed"
avg_order = PIVOT orders, "product", "month", "total", "AVG"
FOR EACH row IN avg_order
TALK row.product + ": Q1=$" + row.Q1 + ", Q2=$" + row.Q2
NEXT
Sales by Region and Product
data = FIND "sales", "year=2025"
pivot_table = PIVOT data, "region", "product", "revenue", "SUM"
TALK "Revenue by Region and Product:"
FOR EACH region IN pivot_table
TALK region.row_header + ":"
TALK " Widgets: $" + region.Widgets
TALK " Gadgets: $" + region.Gadgets
NEXT
Return Value
Returns an array of objects where:
- Each object represents a row
row_headercontains the row label- Dynamic properties contain pivoted column values
Sample Conversation
Use Cases
| Scenario | Row | Column | Value |
|---|---|---|---|
| Sales dashboard | Region | Quarter | Revenue |
| Attendance report | Employee | Month | Days |
| Product comparison | Product | Store | Units sold |
| Time tracking | Project | Week | Hours |
See Also
- GROUP BY - Group data by columns
- AGGREGATE - Calculate summary values
- TABLE - Display formatted tables