---
product: "AG Grid"
title: "Aggregation"
description: "Apply custom or provided functions to values to calculate group values in the grid."
enterprise: true
framework: angular
version: "36.2.0"
related:
    - title: "Configure Columns"
      url: "https://www.ag-grid.com/archive/36.2.0/angular-data-grid/aggregation-columns/"
    - title: "Custom Functions"
      url: "https://www.ag-grid.com/archive/36.2.0/angular-data-grid/aggregation-custom-functions/"
    - title: "Total Rows"
      url: "https://www.ag-grid.com/archive/36.2.0/angular-data-grid/aggregation-total-rows/"
    - title: "Filtering "
      url: "https://www.ag-grid.com/archive/36.2.0/angular-data-grid/aggregation-filtering/"
    - title: "Show Values As"
      url: "https://www.ag-grid.com/archive/36.2.0/angular-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 { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { AgGridAngular } from "ag-grid-angular";
import {
  AutoGroupColumnDef,
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GridApi,
  GridOptions,
  GridReadyEvent,
  IAggFuncs,
  ModuleRegistry,
  UseGroupTotalRow,
  enableDevValidations,
} from "ag-grid-community";
import { ColumnMenuModule, RowGroupingModule } from "ag-grid-enterprise";

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

ModuleRegistry.registerModules([
  ClientSideRowModelModule,
  ColumnMenuModule,
  RowGroupingModule,
]);
import { IOlympicData } from "./interfaces";

@Component({
  selector: "my-app",
  standalone: true,
  imports: [AgGridAngular],
  template: `<ag-grid-angular
    style="width: 100%; height: 100%;"
    [columnDefs]="columnDefs"
    [defaultColDef]="defaultColDef"
    [autoGroupColumnDef]="autoGroupColumnDef"
    [grandTotalRow]="grandTotalRow"
    [groupTotalRow]="groupTotalRow"
    [aggFuncs]="aggFuncs"
    [rowData]="rowData"
    (gridReady)="onGridReady($event)"
  /> `,
})
export class AppComponent {
  columnDefs: ColDef[] = [
    { field: "country", rowGroup: true, hide: true },
    { field: "bronze", aggFunc: "sum" },
    { field: "silver", aggFunc: "avg" },
    { field: "gold", aggFunc: "custom_Mode" },
  ];
  defaultColDef: ColDef = {
    flex: 1,
    minWidth: 100,
  };
  autoGroupColumnDef: AutoGroupColumnDef = {
    minWidth: 200,
  };
  grandTotalRow: "top" | "bottom" | "pinnedTop" | "pinnedBottom" = "bottom";
  groupTotalRow: "top" | "bottom" | UseGroupTotalRow = "bottom";
  aggFuncs: IAggFuncs = {
    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;
    },
  };
  rowData!: IOlympicData[];

  constructor(private http: HttpClient) {}

  onGridReady(params: GridReadyEvent<IOlympicData>) {
    this.http
      .get<
        IOlympicData[]
      >("https://www.ag-grid.com/example-assets/small-olympic-winners.json")
      .subscribe((data) => (this.rowData = data));
  }
}
```

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

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

```ts
<ag-grid-angular
    [columnDefs]="columnDefs"
    /* other grid options ... */ />

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

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