Skip to main content

Pivoting

Turn a flat dataset into a summary table: one row per combination of the row fields, one column per value of the column fields, and a measure aggregated at every intersection.

Loading demo…
<DataTable
columns={columns}
data={deals}
enablePivoting
initialState={{
pivot: {
rows: ['region'], // one row per region
columns: ['quarter'], // one column per quarter
values: [{ field: 'amount', aggregation: 'sum' }], // summed at each intersection
},
}}
/>

The pivot model​

FieldTypeMeaning
rowsstring[]Fields that become the row dimensions. Several make one row per combination.
columnsstring[]Fields whose values become columns. Several nest the headers.
values{ field, aggregation, header? }[]The measures: aggregation is 'sum' | 'avg' | 'min' | 'max' | 'count'. With several measures, each column value gets one sub-column per measure.

Fields are read the way the matching column reads them (accessorKey with dot paths, accessorFn, valueGetter), so a computed column can be a dimension or a measure. Dimension columns keep the source column's header, type and formatter.

Switch the pivot at runtime:

apiRef.current.pivot.setPivotModel({ rows: ['rep'], columns: ['region'], values: [{ field: 'amount', aggregation: 'avg' }] });
apiRef.current.pivot.setPivotModel(null); // back to the normal table
apiRef.current.pivot.getPivotModel();

onPivotModelChange(model) reports changes. The pivot is saved in saved views and persistence.

How it behaves​

  • The pivot table is a normal table: sort by any column, filter, page, export it, add footer totals (exact for sum, count, min and max; averages get none, because an average of averages would be wrong), or group its rows.
  • Rows are summaries, not your records. While pivoted, row actions, the detail panel, tree rows and onRowClick are off, so your code never receives a summary row by mistake. Selection works on the summary rows.
  • Empty values become a (blank) column or row.
Client data only

Pivoting is ignored for server data: the grid holds one page of rows, so the summary would only cover that page. Pivot on your backend and pass the result as data.

Building a pivot yourself​

The transform is exported, for a pivot you want to render or post-process outside the grid:

import { buildPivot } from '@ackplus/mui-tanstack-data-grid';

const { data, columns } = buildPivot(deals, dealColumns, { rows: ['region'], columns: ['quarter'], values: [{ field: 'amount', aggregation: 'sum' }] });

Props​

PropTypeDescription
enablePivotingbooleanTurn pivoting on (client data).
initialState.pivotPivotModel | nullThe starting pivot.
onPivotModelChange(model: PivotModel | null) => voidThe pivot changed.