---
product: "AG Grid"
title: "Aggregation"
description: "Apply custom or provided functions to values to calculate group values in the grid."
enterprise: true
framework: javascript
version: "36.2.0"
related:
    - title: "Configure Columns"
      url: "https://www.ag-grid.com/archive/36.2.0/javascript-data-grid/aggregation-columns/"
    - title: "Custom Functions"
      url: "https://www.ag-grid.com/archive/36.2.0/javascript-data-grid/aggregation-custom-functions/"
    - title: "Total Rows"
      url: "https://www.ag-grid.com/archive/36.2.0/javascript-data-grid/aggregation-total-rows/"
    - title: "Filtering "
      url: "https://www.ag-grid.com/archive/36.2.0/javascript-data-grid/aggregation-filtering/"
    - title: "Show Values As"
      url: "https://www.ag-grid.com/archive/36.2.0/javascript-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

```ts
import {
  ClientSideRowModelModule,
  GridApi,
  GridOptions,
  ModuleRegistry,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";
import { ColumnMenuModule, RowGroupingModule } from "ag-grid-enterprise";
import { IOlympicData } from "./interfaces";

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

ModuleRegistry.registerModules([
  ClientSideRowModelModule,
  ColumnMenuModule,
  RowGroupingModule,
]);

let gridApi: GridApi<IOlympicData>;

const gridOptions: GridOptions<IOlympicData> = {
  columnDefs: [
    { field: "country", rowGroup: true, hide: true },
    { field: "bronze", aggFunc: "sum" },
    { field: "silver", aggFunc: "avg" },
    { field: "gold", aggFunc: "custom_Mode" },
  ],
  defaultColDef: {
    flex: 1,
    minWidth: 100,
  },
  autoGroupColumnDef: {
    minWidth: 200,
  },
  grandTotalRow: "bottom",
  groupTotalRow: "bottom",
  aggFuncs: {
    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 gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);

fetch("https://www.ag-grid.com/example-assets/small-olympic-winners.json")
  .then((response) => response.json())
  .then((data: IOlympicData[]) => gridApi!.setGridOption("rowData", data));
```

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

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/javascript-data-grid/aggregation-custom-functions/). [Group and Grand Total Rows](https://www.ag-grid.com/archive/36.2.0/javascript-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:

```js
const gridOptions = {
    columnDefs: [
        { field: 'gold', aggFunc: 'sum' },
        { field: 'silver', aggFunc: 'max' },
        { field: 'bronze', aggFunc: 'avg' },
        // ... other column definitions
    ],

    // other grid options ...
}
```

## 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/javascript-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/javascript-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/javascript-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.
