Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

ParameterTypeDescription
dataArrayThe data array to pivot
row_columnStringColumn to use for row headers
column_columnStringColumn to use for column headers
value_columnStringColumn containing values to aggregate
aggregateStringAggregate 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_header contains the row label
  • Dynamic properties contain pivoted column values

Sample Conversation

Use Cases

ScenarioRowColumnValue
Sales dashboardRegionQuarterRevenue
Attendance reportEmployeeMonthDays
Product comparisonProductStoreUnits sold
Time trackingProjectWeekHours

See Also