botbook/src/06-gbdialog/keyword-pivot.md

3.7 KiB

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_header contains the row label
  • Dynamic properties contain pivoted column values

Sample Conversation

Show quarterly sales by region

10:15

📊 Quarterly Sales Report 2025

Region | Q1 | Q2 | Q3 | Q4

━━━━━━━━━━━━━━━━━━━━━━━━

North | $125K | $142K | $138K | $167K

South | $98K | $105K | $112K | $128K

East | $87K | $92K | $95K | $103K

West | $156K | $168K | $175K | $189K

10:15

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