Pivoting and Grouping

Create pivot tables with row/column grouping and aggregations

Rowsncolumns Spreadsheet supports advanced pivot table functionality powered by DuckDB, allowing you to create dynamic data summaries with row and column grouping, aggregations, and filtering.

Features

  • Server-side pivot operations using DuckDB for high performance

  • Row and column grouping with multiple levels of nesting

  • Dynamic filtering with multiple filter types

  • Sorting by any field

  • Aggregation functions: sum, count, avg, min, max, var, stddev, median, product

  • React hook (usePivot) for easy integration

  • Multiple pivot tables support with pivotId-based API

  • Interactive pivot editor UI component

Installation

The pivot functionality is available as a separate package:

npm install @rowsncolumns/pivot

Basic Usage

Setting up the Pivot Hook

Creating a Pivot Table

You can create pivot tables programmatically or through the UI:

Adding Pivot Fields

Understanding getGridValues

The getGridValues function is critical - it extracts data from your spreadsheet and formats it for pivoting:

Using the Pivot Editor Component

The package includes a UI component for managing pivot tables:

API Reference

Core Operations

Function
Description
Parameters

executePivot

Execute pivot and update the spreadsheet

(pivotId: string)

refreshPivot

Recalculate an existing pivot

(pivotId: string)

getPivotState

Get current pivot configuration

(pivotId: string)

getPivotManager

Get the pivot manager instance

(pivotId: string)

Field Management

Function
Description
Parameters

addRowPivot

Add a field to the rows area

(pivotId, field: PivotField)

addColumnPivot

Add a field to the columns area

(pivotId, field: PivotField)

addValue

Add a field to the values area

(pivotId, field: PivotField)

removePivotField

Remove a field from rows/columns/values

(pivotId, field, area)

changeFieldOrder

Reorder fields within an area

(pivotId, area, fromIndex, toIndex)

expandRowPivot

Expand a row group to show details

(pivotId, groupKeys: string[])

Filtering and Aggregation

Function
Description
Parameters

addFilter

Add a filter to the pivot

(pivotId, field, filterValue)

removeFilter

Remove a filter

(pivotId, field)

setAggregationFunction

Set aggregation for a value field

(pivotId, field, aggFunc)

UI Integration

Function
Description
Parameters

onRequestCreatePivotTable

Trigger pivot creation dialog

()

onCreatePivotTable

Create a new pivot table

(config: PivotTableConfig)

onRequestEditPivotTable

Open pivot editor for a table

(pivotId: string)

onRequestDeletePivotTable

Trigger delete confirmation

(pivotId: string)

onClosePivotSettings

Close the pivot editor

()

updatePivotSourceRange

Update the source data range

(pivotId, range: SheetRange)

Transform Functions

Function
Description
Return Type

transformPivotResults

Convert pivot results to 2D array for display

{ data: any[][], metadata: PivotMetadata }

transformChildRows

Get child rows for drill-down

PivotRow[]

Aggregation Functions

The following aggregation functions are supported:

  • sum - Sum of values

  • count - Count of values

  • avg - Average of values

  • min - Minimum value

  • max - Maximum value

  • var - Variance

  • stddev - Standard deviation

  • median - Median value

  • product - Product of values

Complete Example

Advanced Features

Formatting Pivot Results

You can apply custom formatting to pivot table results:

Drill-Down Support

Expand row groups to see detailed data:

Database Initialization

The usePivot hook handles DuckDB initialization automatically:

  1. Auto-initialization (default): The hook initializes DuckDB on first use

  2. Manual initialization: Optionally provide a pre-initialized database

  3. Singleton pattern: The default database is shared across pivot instances

Check initialization status:

PivotTable Type Structure

Performance Considerations

  • Pivot operations run in DuckDB for optimal performance

  • Large datasets are handled efficiently through SQL aggregation

  • Results are cached and only recalculated when needed

  • Use refreshPivot to manually trigger recalculation when source data changes

Troubleshooting

Pivot table not updating

Make sure you call executePivot after making configuration changes:

Database initialization issues

Check the isInitializing state before using pivot features:

Source data not updating

Ensure your getGridValues function is using the latest spreadsheet data and is properly memoized with dependencies.

Last updated

Was this helpful?