---
product: "AG Grid"
title: "Aggregation"
description: "Apply custom or provided functions to values to calculate group values in the grid."
enterprise: true
framework: react
version: "36.2.0"
related:
    - title: "Configure Columns"
      url: "https://www.ag-grid.com/archive/36.2.0/react-data-grid/aggregation-columns/"
    - title: "Custom Functions"
      url: "https://www.ag-grid.com/archive/36.2.0/react-data-grid/aggregation-custom-functions/"
    - title: "Total Rows"
      url: "https://www.ag-grid.com/archive/36.2.0/react-data-grid/aggregation-total-rows/"
    - title: "Filtering "
      url: "https://www.ag-grid.com/archive/36.2.0/react-data-grid/aggregation-filtering/"
    - title: "Show Values As"
      url: "https://www.ag-grid.com/archive/36.2.0/react-data-grid/aggregation-show-values-as/"
llms: "https://www.ag-grid.com/archive/36.2.0/llms.txt"
---

# Aggregation

Apply custom or provided functions to values to calculate group values in the grid.

#### Aggregation Overview

```tsx
"use client";

import React, {
  useCallback,
  useMemo,
  useRef,
  useState,
  StrictMode,
} from "react";
import { createRoot } from "react-dom/client";
import { AgGridReact, AgGridProvider } from "ag-grid-react";
import {
  AutoGroupColumnDef,
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GridApi,
  GridOptions,
  IAggFuncs,
  ModuleRegistry,
  UseGroupTotalRow,
  enableDevValidations,
} from "ag-grid-community";
import { ColumnMenuModule, RowGroupingModule } from "ag-grid-enterprise";
import { IOlympicData } from "./interfaces";
import { useFetchJson } from "./useFetchJson";

if (process.env.NODE_ENV !== "production") {
  // Enable extended validations only for development
  enableDevValidations();
}

const modules = [ClientSideRowModelModule, ColumnMenuModule, RowGroupingModule];

const GridExample = () => {
  const containerStyle = useMemo(() => ({ width: "100%", height: "100%" }), []);
  const gridStyle = useMemo(() => ({ height: "100%", width: "100%" }), []);

  const [columnDefs, setColumnDefs] = useState<ColDef[]>([
    { field: "country", rowGroup: true, hide: true },
    { field: "bronze", aggFunc: "sum" },
    { field: "silver", aggFunc: "avg" },
    { field: "gold", aggFunc: "custom_Mode" },
  ]);
  const defaultColDef = useMemo<ColDef>(() => {
    return {
      flex: 1,
      minWidth: 100,
    };
  }, []);
  const autoGroupColumnDef = useMemo<AutoGroupColumnDef>(() => {
    return {
      minWidth: 200,
    };
  }, []);
  const aggFuncs = useMemo<IAggFuncs>(() => {
    return {
      custom_Mode: (params) => {
        const counts = new Map<number, number>();
        let mode = null;
        let maxCount = 0;
        for (const value of params.values) {
          if (value == null) continue;
          const count = (counts.get(value) ?? 0) + 1;
          counts.set(value, count);
          if (count > maxCount) {
            maxCount = count;
            mode = value;
          }
        }
        return mode;
      },
    };
  }, []);

  const { data, loading } = useFetchJson<IOlympicData>(
    "https://www.ag-grid.com/example-assets/small-olympic-winners.json",
  );

  return (
    <AgGridProvider modules={modules}>
      <div style={containerStyle}>
        <div style={gridStyle}>
          <AgGridReact<IOlympicData>
            rowData={data}
            loading={loading}
            columnDefs={columnDefs}
            defaultColDef={defaultColDef}
            autoGroupColumnDef={autoGroupColumnDef}
            grandTotalRow={"bottom"}
            groupTotalRow={"bottom"}
            aggFuncs={aggFuncs}
          />
        </div>
      </div>
    </AgGridProvider>
  );
};

const root = createRoot(document.getElementById("root")!);
root.render(
  <StrictMode>
    <GridExample />
  </StrictMode>,
);
```

[Live example: Aggregation Overview](https://www.ag-grid.com/archive/36.2.0/examples/aggregation/aggregation-overview/reactFunctionalTs/)

This example also demonstrates using the built in aggregations of `sum` and `avg` as well as a custom `mode` implementation via a [Custom Aggregation Function](https://www.ag-grid.com/archive/36.2.0/react-data-grid/aggregation-custom-functions/). [Group and Grand Total Rows](https://www.ag-grid.com/archive/36.2.0/react-data-grid/aggregation-total-rows/) are also enabled.

## Enabling Aggregation

Aggregations can be enabled in the grid by setting the `aggFunc` column definition value to one of: `sum`, `min`, `max`, `count`, `avg`, `first`, or `last`.

The following configuration demonstrates how to enable aggregation on a column:

```jsx
const [columnDefs, setColumnDefs] = useState([
    { field: 'gold', aggFunc: 'sum' },
    { field: 'silver', aggFunc: 'max' },
    { field: 'bronze', aggFunc: 'avg' },
    // ... other column definitions
]);

<AgGridReact columnDefs={columnDefs} />
```

## API Reference

> **Note**
>
> The aggregation state can be saved and restored as part of [Grid State](https://www.ag-grid.com/archive/36.2.0/react-data-grid/grid-state/).

Aggregations can be configured using the following column properties:

| Property | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `aggFunc` | `string \| IAggFunc \| null` |  |  |  |
| `initialAggFunc` | `string \| IAggFunc` |  |  |  |
| `valueIndex` | `number` |  |  |  |
| `initialValueIndex` | `number` |  |  |  |
| `enableValue` | `boolean` |  |  |  |
| `allowedAggFuncs` | `string[]` |  |  |  |
| `defaultAggFunc` | `string` |  |  |  |
| `showValuesAs` | `ShowValuesAsType \| ShowValuesAs \| null` |  |  |  |
| `initialShowValuesAs` | `ShowValuesAsType \| ShowValuesAs` |  |  |  |
| `showValuesAsDef` | `ShowValuesAsDef \| null` |  |  |  |
| `enableShowValuesAs` | `boolean` |  |  |  |

Aggregation functions can be registered with the grid using the following grid options:

| Property | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `aggFuncs` | `IAggFuncs` |  |  |  |
| `groupTotalRow` | `'top' \| 'bottom' \| UseGroupTotalRow` |  |  |  |
| `grandTotalRow` | `'top' \| 'bottom' \| 'pinnedTop' \| 'pinnedBottom'` |  |  |  |
| `suppressAggFuncInHeader` | `boolean` |  |  |  |
| `aggregateOnlyChangedColumns` | `boolean` |  |  |  |
| `suppressAggFilteredOnly` | `boolean` |  |  |  |
| `groupAggFiltering` | `boolean \| IsRowFilterable` |  |  |  |
| `groupSuppressBlankHeader` | `boolean` |  |  |  |
| `suppressStickyTotalRow` | `boolean \| 'grand' \| 'group'` |  |  |  |
| `alwaysAggregateAtRootLevel` | `boolean` |  |  |  |
| `getGroupRowAgg` | `GetGroupRowAgg` |  |  |  |

After the grid is initialised aggregations can be applied / retrieved / removed via the `api` with the following methods:

| Property | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `getValueColumns` | `Function` |  |  |  |
| `addValueColumns` | `Function` |  |  |  |
| `removeValueColumns` | `Function` |  |  |  |
| `setValueColumns` | `Function` |  |  |  |
| `setColumnAggFunc` | `Function` |  |  |  |
| `addAggFuncs` | `Function` |  |  |  |
| `clearAggFuncs` | `Function` |  |  |  |

## Editing Aggregated Values

Group row cells displaying aggregated values can be made editable, allowing users to edit a group total and have the change distributed back down to descendant rows. The built-in distribution supports all standard aggregation functions and can be customised per column.

See [Editing Group Rows](https://www.ag-grid.com/archive/36.2.0/react-data-grid/grouping-edit/) for full details on `groupRowEditable`, `groupRowValueSetter`, and distribution strategies.

## Retrieving Aggregated Children

The method `rowNode.getAggregatedChildren(colKey)` with Client Side Row Model returns the immediate children that contribute to the aggregation of a group row. This is useful when implementing custom logic based on aggregated data or when [Editing Group Rows](https://www.ag-grid.com/archive/36.2.0/react-data-grid/grouping-edit/) to update child rows accordingly.

- For regular group columns, returns the direct children used for aggregation (respecting `suppressAggFilteredOnly` and `groupAggFiltering` settings).
- For pivot columns on leaf groups, returns only the children matching the column's pivot keys.
- Returns an empty array for leaf (non-group) rows.

### Retrieving All Leaf Descendants

Pass `true` as the second argument to collect all descendant leaf (data) rows recursively:

```ts
const allLeaves = groupNode.getAggregatedChildren(colKey, true);
```

This traverses the full group hierarchy and returns every non-group row that ultimately contributes to the group's aggregated value, respecting the same filtering and pivot rules as the non-recursive call.

> **Note**
>
> Calling `getAggregatedChildren(colKey, true)` allocates a new array and visits every descendant node. On large or deeply nested datasets this can be expensive, so prefer the non-recursive form when only immediate children are needed.
