Pivot
Build a pivot from flat rows: the config, the measures, and the result you render or export.
createPivot
functionPivot data and return rows and columns ready for TanStack Table.
Without a license key it still runs and logs a notice once.
createPivot<TData>(
data: ReadonlyArray<TData>,
config: PivotConfig<TData>,
opts?: ToColumnsOptions,
) => PivotTableParameters
| Name | Type | Description |
|---|---|---|
data | readonly TData[] | Flat source rows. Not changed. |
config | PivotConfig<TData> | Row fields, column fields and measures. Throws when values is empty or names an unknown aggregator. |
optsoptional | ToColumnsOptions | Settings for every value column, such as an Excel number format. |
Returns
PivotConfig<TData>
typeA pivot definition: the row axis, the column axis, and the measures at their intersections.
Properties
| Name | Type | Description |
|---|---|---|
rows | readonly (keyof TData & string)[] | Outer-most first. Each entry becomes one level of row grouping. |
cols | readonly (keyof TData & string)[] | Outer-most first. Each entry becomes one level of column grouping. |
values | readonly PivotValueConfig<TData>[] | One or more measures aggregated under each column-axis leaf. |
rowHeaderoptional | string | Header of the first column. Default: the row fields joined with /. |
grandTotalRowoptional | boolean | Grand-total row at the bottom. Default true. |
grandTotalColoptional | boolean | Grand-total column on the right. Default true. |
rowSubtotalsoptional | boolean | Group rows carry their subtotals. When false, group rows stay (they still expand and collapse) but show no numbers. Default true. |
colSortoptional | ((a: unknown, b: unknown, level: number) => number) | Optional sort for column-axis values per dim level (defaults to alpha). |
rowSortoptional | ((a: unknown, b: unknown, level: number) => number) | Optional sort for row-axis values per dim level (defaults to alpha). |
PivotValueConfig<TData>
typeOne measure: the field to aggregate and how to aggregate it.
Properties
| Name | Type | Description |
|---|---|---|
field | keyof TData & string | Row field whose values get aggregated. |
agg | PivotAggregatorId | PivotAggregator | Built-in aggregator id, or a custom reducer. |
labeloptional | string | Display name in the column header. Defaults to ${field} (${agg}). |
formatoptional | ((value: unknown) => string) | Turns a value cell into display text. Default: numbers with grouping separators. |
PivotAggregatorId
typeBuilt-in pivot measures, named in PivotValueConfig.
type PivotAggregatorId = 'sum'
| 'avg'
| 'min'
| 'max'
| 'count'
| 'countDistinct'
| 'first'
| 'last'PivotAggregator
typeA custom measure: reduce the values under one cell to the number to display.
type PivotAggregator = (values: ReadonlyArray<unknown>) => unknownToColumnsOptions
typeOptions for the generated value columns.
Properties
| Name | Type | Description |
|---|---|---|
numFmtoptional | string | Excel number format for every value column, e.g. '#,##0.00'. |
PivotTable
typeWhat createPivot returns: the pivot as TanStack data and columns, and as a table the exporters read.
Properties
| Name | Type | Description |
|---|---|---|
data | PivotTreeRow[] | Rows for useReactTable({ data }), with getSubRows: (r) => r.subRows. |
columns | PivotColumnDef[] | Columns for useReactTable({ columns }). |
rows | PivotRow[] | The flat rows, as the engine produced them. |
table | TableLike | The pivot as a table the exporters read: exportXlsx(pivot.table). |
PivotTreeRow
typeA pivot row with its children attached.
Properties
| Name | Type | Description |
|---|---|---|
__pivotId | string | Unique id built from the row's path of values. The grand-total row is 'row__grand_total'. |
__pivotKind | PivotRowKind | 'group' (holds the group's subtotals unless rowSubtotals is false), 'leaf' or 'grandTotal'. |
__pivotDepth | number | Level on the row axis: 1 under the first row field, 2 under the second, and so on. 0 on the grand-total row and on the single row of a pivot with no row fields. |
__pivotLabel | string | Text of the first column: the row's value on the row axis, 'Grand total' on the grand-total row, '(All)' on the single row of a pivot with no row fields. |
__pivotParentId | string | null | __pivotId of the group row this row sits under, or null at the top level. |
__pivotExpandable | boolean | True on group rows, which always have rows under them. |
subRowsoptional | PivotTreeRow[] | The rows under this one. Absent on rows with none. |
PivotColumnDef
typeA generated column: an accessor leaf or a header group. Fits TanStack's ColumnDef<PivotTreeRow>.
type PivotColumnDef = { id: string; header: string; accessorFn: (row: PivotTreeRow) => unknown; cell?: (ctx: { getValue(): unknown; }) => string; size?: number; meta: PivotColumnMeta; }
| { id: string; header: string; columns: PivotColumnDef[]; meta: PivotColumnMeta; }PivotColumnMeta
typeMetadata on every generated column.
Properties
| Name | Type | Description |
|---|---|---|
rowpine | RowpineColumnMeta<PivotTreeRow> | Export settings: the row-header column carries its header; value columns carry numFmt, format and right alignment. |
pivot | { isRowHeader: boolean; isTotal: boolean; measure?: number; } | Where the column sits in the pivot: the row-header column, a grand-total column, and the measure index into config.values for a value leaf. |
pivotRowStyle
functionBold group, subtotal and grand-total rows in exports. Pass as rowStyle.
pivotRowStyle(original: unknown) => { bold?: boolean; } | undefinedParameters
| Name | Type | Description |
|---|---|---|
original | unknown | A row's original data: a PivotTreeRow. |
Returns
{ bold?: boolean; } | undefined
pivotAggregators
constantPublic registry of built-in aggregators. Useful for surfacing the available options in a pivot designer UI.
Type Record<PivotAggregatorId, PivotAggregator>
createPivotModel
functionBuild a pivot table from flat rows: cross the row axis with the column axis, aggregate each intersection, and return rows plus the generated column tree. No license check.
createPivotModel<TData>(
data: ReadonlyArray<TData>,
config: PivotConfig<TData>,
) => PivotResultParameters
| Name | Type | Description |
|---|---|---|
data | readonly TData[] | Flat source rows. Not changed. |
config | PivotConfig<TData> | Row fields, column fields and measures. Throws when values is empty or names an unknown aggregator. |
Returns
PivotResult
typeA built pivot: the rows to display, and the generated columns for its column axis.
Properties
| Name | Type | Description |
|---|---|---|
rows | PivotRow[] | Every pivot row, flat, in display order: each group row before its rows, the grand-total row last. |
columns | PivotColumn[] | The column tree: the row-header column, the value columns, then the 'Total' group when there is one. |
PivotRow
typeShape of one entry in result.rows. The first-column label lives at
__pivotLabel; every value cell lives at the column id matching the
leaf column generated for its (col-path × measure).
__pivotParentId is the __pivotId of the row's row-axis parent
(or null for top-level groups, the grand-total row, and the one
synthetic "All" row when no row dims are configured). Use it with
filterCollapsedPivotRows to hide leaves whose ancestor is
collapsed - that's how expandable pivots are built on top of the
model.
__pivotExpandable is true for group rows that have at least
one descendant. A renderer uses this to decide whether to show a
chevron next to the label.
Properties
| Name | Type | Description |
|---|---|---|
__pivotId | string | Unique id built from the row's path of values. The grand-total row is 'row__grand_total'. |
__pivotKind | PivotRowKind | 'group' (holds the group's subtotals unless rowSubtotals is false), 'leaf' or 'grandTotal'. |
__pivotDepth | number | Level on the row axis: 1 under the first row field, 2 under the second, and so on. 0 on the grand-total row and on the single row of a pivot with no row fields. |
__pivotLabel | string | Text of the first column: the row's value on the row axis, 'Grand total' on the grand-total row, '(All)' on the single row of a pivot with no row fields. |
__pivotParentId | string | null | __pivotId of the group row this row sits under, or null at the top level. |
__pivotExpandable | boolean | True on group rows, which always have rows under them. |
PivotRowKind
typeWhat a pivot row represents: a group row (which carries the group's subtotals),
a leaf, or the grand total. The engine does not emit subtotal; it is kept
in the type so existing code that checks for it still compiles.
type PivotRowKind = 'group'
| 'subtotal'
| 'leaf'
| 'grandTotal'PivotColumn
typeOne generated column. A leaf has a measure; a header group has columns.
Properties
| Name | Type | Description |
|---|---|---|
id | string | Column id. A value leaf's id is the key of its cell in each PivotRow; the first column is '__pivotRowHeader'. |
header | string | Header text: the measure's label on a value leaf, the column-axis value on a header group ('(blank)' for null), 'Total' on the grand-total group. |
columnsoptional | PivotColumn[] | Child columns, for a column-axis header group. |
measureoptional | number | Index into config.values, for a value leaf. |
formatoptional | ((value: unknown) => string) | The measure's formatter, for a value leaf. |
widthoptional | number | Suggested width in px. |
isRowHeaderoptional | boolean | True for the first column, which holds each row's label. |
isTotaloptional | boolean | True for the grand-total column group and its leaves. |
filterCollapsedPivotRows
functionFilter a result.rows array down to only the rows the user should
see given the current expansion state. A row stays in the output if
its entire ancestor chain (via __pivotParentId) is in expandedIds.
Pass true to bypass filtering (everything visible - the default
shape createPivotModel already returns). Pass an empty array /
empty Set to collapse every group to its subtotal row.
The result is a new array; the input is not mutated.
filterCollapsedPivotRows(
rows: ReadonlyArray<PivotRow>,
expandedIds: ReadonlyArray<string> | Set<string> | true,
) => PivotRow[]Parameters
| Name | Type | Description |
|---|---|---|
rows | readonly PivotRow[] | Rows as createPivotModel returns them. |
expandedIds | any | Ids of the expanded group rows, or true to keep every row. |
Returns
PivotRow[]
pivotAsTable
functionA pivot as a read-only table for the exporters, with no TanStack instance
behind it, so exporting a pivot works the same under any TanStack version.
It holds every row, nested by subRows; collapsing groups on screen does not change it.
pivotAsTable(data: PivotTreeRow[], columns: PivotColumnDef[]) => TableLikeParameters
| Name | Type | Description |
|---|---|---|
data | PivotTreeRow[] | Nested rows from pivotToTree. |
columns | PivotColumnDef[] | Column definitions from toTanStackColumns. |
Returns
pivotToTree
functionNest flat pivot rows under their parents. The grand-total row stays last. The rows are copied; the input is not changed.
pivotToTree(rows: ReadonlyArray<PivotRow>) => PivotTreeRow[]Parameters
| Name | Type | Description |
|---|---|---|
rows | readonly PivotRow[] | Flat rows from createPivotModel, each parent before its children. |
Returns
toTanStackColumns
functionTurn a pivot's column tree into TanStack column definitions. Value columns
format with the measure's format, else numbers in the browser locale
with at most two decimals, and null as an empty string.
toTanStackColumns(
columns: ReadonlyArray<PivotColumn>,
opts?: ToColumnsOptions,
) => PivotColumnDef[]Parameters
| Name | Type | Description |
|---|---|---|
columns | readonly PivotColumn[] | The column tree from createPivotModel. |
optsoptional | ToColumnsOptions | Settings for every value column. |