# Aggregation

> Pivot aggregation types over mergeable states, show values as, the getCellValue override, calculated field formulas and automatic data compression.

Canonical: https://salmonui.dev/docs/pivot-table/aggregation

Each entry in `pivotModel.values` is one measure: a source `field`, an
`aggregate`, and an optional `caption`, `showAs` and `customId`.

```ts
values: [
  { field: 'amount', aggregate: 'sum' },
  { field: 'amount', aggregate: 'average', caption: 'Avg amount' },
  { field: 'country', aggregate: 'distinctCount' },
]
```

## Aggregation types

| `aggregate` | Notes |
| --- | --- |
| `sum`, `product`, `min`, `max` | Numeric values only; empty when a cell has none |
| `count` | Non-blank values of any type |
| `distinctCount` | Distinct non-blank values of any type |
| `average` | Sum and count, merged exactly |
| `median` | Exact; computed only when used |
| `populationVar`, `sampleVar`, `populationStdev`, `sampleStdev` | Welford states, merged exactly |
| `custom` | `customAggregates[customId]`: `{ init, add, merge, result }` |

Aggregates are kept as mergeable states in typed arrays, so subtotals, grand
totals, column drill and collapse never re-read the raw rows. A non-numeric
field supports `count` and `distinctCount`; any other aggregate over it
returns empty cells.

**Measure ids.** Every value field has an id that is unique within
`values`. It is the `field` itself, `field#aggregate` when another value field
uses the same field (`amount#sum`, `amount#average`), and
`field#aggregate#index` if even that repeats. The id appears in
`PivotHeaderNode.measure`, `PivotCell.measure` and header keys, and
`measureId(values, index)` computes it. `valueSort.measure`, value filters and
`formats` also accept the plain field name.

## Show values as

`showAs` transforms the aggregate after it is computed. It is separate from
`aggregate`.

| `showAs.kind` | Value |
| --- | --- |
| `percentOfGrandTotal`, `percentOfColumnTotal`, `percentOfRowTotal` | v / grand total, column total, row total |
| `percentOfParentRowTotal`, `percentOfParentColumnTotal` | v / the parent member's total on that axis |
| `percentOfParentTotal` | v / the total of the ancestor at `baseField` |
| `runningTotal`, `percentOfRunningTotal` | Cumulative along the members of `baseField` (default: the innermost row field) |
| `differenceFrom`, `percentOfDifferenceFrom` | v − the value at `baseItem` of `baseField` (empty for the base item itself) |
| `index` | (v × grand total) / (row total × column total) |

`cellAt().rawValue` keeps the aggregate before the transform. Percent kinds
are formatted as percentages unless `formats[measure]` says otherwise.

## Overriding a cell

`getCellValue(cell)` is a pure function. It
receives the computed `PivotCell` and returns the value to show, and the
formatted text follows the new value:

```ts
createPivotEngine({ data, model, getCellValue: (c) => (c.kind === 'grandTotal' ? null : c.value) });
```

In React, pass `getCellValue` (and `customAggregates`) to `usePivot()` or
`PivotTable`. Both are read when the store is created: keep them stable
(module-level functions), and remount with a new `key` to switch them.

## Changing the aggregate at run time

The menu of a value field button on the axis bar or in the field list has
"Summarize values by". It offers the
`aggregateOptions` prop of `PivotTable`, `PivotAxisBar` or `PivotFieldList`
(default: every built-in aggregate; a non-numeric field offers only `count`
and `distinctCount`), and `onBeforeAggregateMenuOpen` receives
`{ field, index, items }` with a mutable `items` copy to change the list per
field, or `preventDefault()` to leave the submenu out. From code,
`pivot.setAggregate(index, aggregate)` changes `values[index].aggregate`.

```tsx
<PivotTable
  data={data}
  defaultPivotModel={model}
  axisBar
  aggregateOptions={['sum', 'count', 'average', 'min', 'max']}
  onBeforeAggregateMenuOpen={(event) => {
    if (event.field === 'units') event.items.push('distinctCount');
  }}
/>
```

## Calculated fields

`calculatedFields` defines measures computed from other aggregated measures.
To show one, add its `name` to `values` (the `aggregate` you give it there is
ignored). Write references as `"Sum(Amount)"` (quoted) or
`Sum(Amount)`. The aggregate names are `Sum`, `Count`, `DistinctCount`,
`Avg`, `Average`, `Min`, `Max`, `Product`, `Median`, `PopulationStDev`,
`SampleStDev`, `PopulationVar` and `SampleVar`.

```ts
calculatedFields: [{ name: 'price', formula: '"Sum(amount)" / "Sum(units)"', format: { number: { maximumFractionDigits: 2 } } }],
values: [{ field: 'price', aggregate: 'sum' }],
```

| Supported | |
| --- | --- |
| Operators | `+ - * / % ^`, `= == != <> < <= > >=`, `&& \|\|`, unary `- + !`, parentheses |
| Functions | `IF`, `AND`, `OR`, `NOT`, `ABS`, `MIN`, `MAX`, `ROUND`, `CEILING`, `FLOOR`, `TRUNC`, `SIGN`, `SQRT`, `POWER`, `EXP`, `LOG`, `LOG10`, `SIN`, `COS`, `TAN`, `ASIN`, `ACOS`, `ATAN` |
| Constants | `TRUE`, `FALSE`, `PI` |

Formulas are parsed into closures and are never run as code: no `eval` and no
`Function` constructor, so they work under a strict Content Security Policy.
An empty measure makes arithmetic empty, and a non-finite result such as
division by zero is empty too. `compileFormula(formula, fields)` (also at
`@salmonui/pivot-table/engine/formula`, 2 KB) validates a formula. It throws
a `PivotFormulaError` with the character `position`. If a formula in the model
does not compile, the result reports it in `errors`
(`{ name, message, position }`) and its cells stay empty.

## Data compression

Data compression needs no prop and never makes an aggregate coarser. When
grouping the rows by their row and column member tuples is guaranteed to
cut the work at least 4×, the engine pre-aggregates them into one record per
tuple and aggregates the records instead of the rows. The records keep exact
states, so averages, variances and custom aggregates come out identical, and
distinct count and median still read the raw rows. Compression is skipped when
it cannot pay off, for example when almost every row has its own tuple.

### Live demo: Aggregates and show values as

Live demo: https://salmonui.dev/demos/pivot-table/pivot-table-aggregation

Every aggregate and show-values-as kind on a value field, value field menus on the axis bar with a restricted aggregate list edited per field by onBeforeAggregateMenuOpen, and a getCellValue override.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { PivotTable, type PivotAggregate, type PivotCell, type PivotModel, type PivotModelInput, type PivotValueField, type PivotFieldDefinition } from '@salmonui/pivot-table';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { Select } from '@salmonui/dropdowns/select';

type Sale = { region: string; country: string; year: string; amount: number; units: number };

// Captions for the headers, the axis bar and the field list (the raw field names otherwise).
const FIELDS: PivotFieldDefinition<Sale>[] = [
  { field: 'region', caption: 'Region', type: 'string' },
  { field: 'country', caption: 'Country', type: 'string' },
  { field: 'year', caption: 'Year', type: 'string' },
  { field: 'amount', caption: 'Amount', type: 'number' },
  { field: 'units', caption: 'Units', type: 'number' },
];

const COUNTRIES: Record<string, string[]> = { Asia: ['Japan', 'India'], Europe: ['France', 'Germany'], Americas: ['Brazil', 'Canada'] };

function makeSales(): Sale[] {
  const rows: Sale[] = [];
  let n = 0;
  for (const [region, countries] of Object.entries(COUNTRIES)) {
    for (const country of countries) {
      for (const year of ['2024', '2025', '2026']) {
        for (let k = 0; k < 4; k++) {
          n++;
          const amount = ((n * 523) % 1700) + 120;
          rows.push({ region, country, year, amount, units: (amount % 29) + 1 });
        }
      }
    }
  }
  return rows;
}

const SALES = makeSales();

const AGGREGATES: PivotAggregate[] = ['sum', 'product', 'count', 'distinctCount', 'min', 'max', 'average', 'median', 'populationStdev', 'sampleStdev', 'populationVar', 'sampleVar'];
const SHOW_AS = [
  'none',
  'percentOfGrandTotal',
  'percentOfColumnTotal',
  'percentOfRowTotal',
  'percentOfParentTotal',
  'percentOfParentRowTotal',
  'percentOfParentColumnTotal',
  'runningTotal',
  'percentOfRunningTotal',
  'differenceFrom',
  'percentOfDifferenceFrom',
  'index',
] as const;
type ShowAsKind = (typeof SHOW_AS)[number];
const AGGREGATE_OPTIONS = AGGREGATES.map((a) => ({ value: a, label: a }));
const SHOW_AS_OPTIONS = SHOW_AS.map((k) => ({ value: k, label: k }));

// Aggregates the value field menus offer.
const MENU_AGGREGATES: PivotAggregate[] = ['sum', 'count', 'average', 'min', 'max', 'median'];

const INITIAL: PivotModelInput = {
  rows: [{ field: 'region' }, { field: 'country' }],
  columns: [{ field: 'year' }],
  values: [{ field: 'amount', aggregate: 'sum' }],
  expanded: [['rows', 'Asia']],
};

function withShowAs(field: PivotValueField, kind: ShowAsKind): PivotValueField {
  const { showAs: _drop, ...rest } = field;
  if (kind === 'none') return rest;
  // Base-field kinds compare years; differences are against 2025.
  if (kind === 'differenceFrom' || kind === 'percentOfDifferenceFrom') return { ...rest, showAs: { kind, baseField: 'year', baseItem: '2025' } };
  if (kind === 'percentOfParentTotal') return { ...rest, showAs: { kind, baseField: 'region' } };
  if (kind === 'runningTotal' || kind === 'percentOfRunningTotal') return { ...rest, showAs: { kind, baseField: 'year' } };
  return { ...rest, showAs: { kind } };
}

// A pure override of the aggregated value of a cell.
const hideSmallCells = (cell: PivotCell): number | null => (cell.kind === 'value' && cell.rawValue !== null && cell.rawValue < 4000 && cell.measure === 'amount' ? null : cell.value);

/** Every aggregate and show-values-as kind, value field menus with a restricted aggregate list, and a getCellValue override. */
export default function PivotTableAggregationDemo() {
  const [model, setModel] = useState<PivotModelInput>(INITIAL);
  const [override, setOverride] = useState(false);
  const [menuLog, setMenuLog] = useState('Open the menu of the amount value button on the axis bar.');
  const value = model.values?.[0] ?? { field: 'amount', aggregate: 'sum' };
  const kind: ShowAsKind = value.showAs?.kind ?? 'none';

  const setValue = (next: PivotValueField) => setModel((m) => ({ ...m, values: [next, ...(m.values ?? []).slice(1)] }));

  return (
    <div>
      <div className="demo-toolbar" role="group" aria-label="Value field">
        <label>
          Aggregate
          <Select aria-label="Aggregate" data={AGGREGATE_OPTIONS} value={value.aggregate} onValueChange={(next) => next != null && setValue({ ...value, aggregate: next as PivotAggregate })} />
        </label>
        <label>
          Show values as
          <Select aria-label="Show values as" data={SHOW_AS_OPTIONS} value={kind} onValueChange={(next) => next != null && setValue(withShowAs(value, next as ShowAsKind))} />
        </label>
        <Checkbox checked={override} onCheckedChange={(checked) => setOverride(checked)}>
          getCellValue: hide amounts under 4,000
        </Checkbox>
      </div>
      <PivotTable<Sale>
        // getCellValue is read when the store is created, so toggling it remounts the table; the model is app state and survives.
        key={override ? 'override' : 'plain'}
        data={SALES}
        fields={FIELDS}
        pivotModel={model}
        onPivotModelChange={(event) => setModel(event.model as PivotModel)}
        {...(override ? { getCellValue: hideSmallCells } : {})}
        axisBar={{ showFieldsPanel: true }}
        aggregateOptions={MENU_AGGREGATES}
        onBeforeAggregateMenuOpen={(event) => {
          // Edit the offered list per field: Units also offers distinct count.
          if (event.field === 'units') event.items.push('distinctCount');
          setMenuLog(`Aggregate menu of ${event.field} offers ${event.items.join(', ')}`);
        }}
        emptyCellText="·"
        locale="en-US"
        height={330}
        aria-label="Sales aggregation"
      />
      <p className="demo-status" aria-live="polite">
        Value field <code>{JSON.stringify(value)}</code>
      </p>
      <p className="demo-status" aria-live="polite">
        {menuLog}
      </p>
    </div>
  );
}
```
