# Grand Total Rows

Canonical page: https://www.adaptabletools.com/docs/handbook-aggregation-grand-total-row

- AdapTable Layouts can include a Grand Total Row which displays aggregated information

[Layouts](https://www.adaptabletools.com/docs/handbook-layouts/index.md) can include a Grand Total Row which will display the totals for all Aggregated Cells.

- Grand Total Rows use whichever `aggFunc` is being applied in the Column being aggregated
- Unlike "normal" aggregations, Grand Total Rows display even if [Row Grouping](https://www.adaptabletools.com/docs/handbook-grouping-rows/index.md) is not applied

This is done via the `GrandTotalRow` property which can be set to one of 6 values:

| Property Value | Behaviour                                         |
| -------------- | ------------------------------------------------- |
| `top`          | Grand Total Row is displayed at top of grid       |
| `bottom`       | Grand Total Row is displayed at bottom of grid    |
| `pinnedTop`    | Grand Total Row is pinned to the top of grid      |
| `pinnedBottom` | Grand Total Row is pinned to the bottom of grid   |
| `true`         | Equivalent to `top`                               |
| `false`        | Grand Total Row is not displayed (same as `null`) |

Users can pick any of these positions (or None) in the Grand Total Row card of the Layout Editor, in both the Aggregations section of a Table Layout and the Pivot Aggregations section of a Pivot Layout. A Layout with `GrandTotalRow: true` shows as Top.

The pinned positions do not need AG Grid [Row Pinning](https://www.ag-grid.com/javascript-data-grid/row-pinning/) to be enabled

A Layout's [Row Summaries](https://www.adaptabletools.com/docs/handbook-summarising-rows/index.md) are not displayed while its Grand Total Row is pinned (`pinnedTop` or `pinnedBottom`); use `top` or `bottom` to show both

- The `GrandTotalRow` property is in the [`LayoutBase`](https://www.adaptabletools.com/docs/reference/layoutbase.md) object, and therefore available in both [Table](https://www.adaptabletools.com/docs/handbook-layouts-table/index.md) and [Pivot](https://www.adaptabletools.com/docs/handbook-layouts-pivot/index.md) Layouts
- [Row Summaries](https://www.adaptabletools.com/docs/handbook-summarising-rows/index.md) are a more powerful and configurable alternative (but available only in Table Layouts)

**Example: Aggregations: Grand Total Row**

Providing a Row showing Total of all Aggregations

- This demo provides 3 Layouts all of which have 3 Aggregated Columns - `Github Stars` (sum), `Github Watchers` (sum) and `Issue Change` (min) - and a Grand Total Row
  - `Grouped Layout` - the Grid is Row Grouped and the Grand Total Row is at the top
  - `Pivot Layout` - the Grid is Pivoted and the Grand Total Row is at the bottom
  - `Standard Layout` - the Grid has now Row Groups and the Grand Total Row is pinned to the top

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts  [template]
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {AdaptableOptions} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Aggregation Grand Total Row',
  initialState: {
    Dashboard: {
      Tabs: [{Name: 'Demo', Toolbars: ['Layout']}],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Grouped Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'week_issue_change',
              AggFunc: 'min',
            },
          ],
          SuppressAggFuncInHeader: true,
          GrandTotalRow: 'pinnedTop',
          TableColumns: [
            'name',
            'github_watchers',
            'github_stars',
            'week_issue_change',
            'updated_at',
            'license',
            'created_at',
          ],
        },
        {
          Name: 'Grouped Layout',
          RowGroupedColumns: ['language'],
          TableAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'week_issue_change',
              AggFunc: 'min',
            },
          ],
          SuppressAggFuncInHeader: true,
          GrandTotalRow: 'top',
          TableColumns: [
            'name',
            'github_watchers',
            'github_stars',
            'week_issue_change',
            'updated_at',
            'license',
            'created_at',
          ],
        },
        {
          Name: 'Pivot Layout',
          PivotColumns: [],
          PivotGroupedColumns: ['language'],
          SuppressAggFuncInHeader: true,
          PivotAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'week_issue_change',
              AggFunc: 'min',
            },
          ],
          GrandTotalRow: 'bottom',
        },
      ],
    },
  },
};
```

## Formatting

By default, Grand Total Rows will be included in all [Column Formatting and Styling](https://www.adaptabletools.com/docs/handbook-column-formatting/index.md).

This behaviour can be changed by configuring the `RowScope` property when defining the Format Column.

The [`RowScope`](https://www.adaptabletools.com/docs/reference/rowscope.md) object enables 4 types of rows to be **excluded** from the Format Column:

- Data Rows
- Grouped Rows
- Summary Rows
- Grand Total Rows - the focus of this page

Use the `ExcludeDataRows` option to create a Format Column that is **only** rendered in a Grand Total Row

**Example: Aggregations: Grand Total Row Formatting**

Formatting Grand Total Rows

- This demo contains a Grand Total Row with 3 Aggregated Columns, and 3 Format Columns each with a different `RowScope`:
  - `Github Watchers` **includes** Grand Total Row but **excludes** Data Rows
  - `Github Stars` **excludes** Grand Total Row but **includes** Data Rows
  - `Issue Change` **includes both** Grand Total Row and Data Rows

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts  [template]
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {AdaptableOptions} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Aggregation Grand Total Row Formatting',
  initialState: {
    Dashboard: {
      Tabs: [{Name: 'Demo', Toolbars: ['Layout']}],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Table Layout',
      Layouts: [
        {
          Name: 'Table Layout',
          //    RowGroupedColumns: ['language'],
          TableAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'week_issue_change',
              AggFunc: 'sum',
            },
          ],
          SuppressAggFuncInHeader: true,
          GrandTotalRow: 'top',
          TableColumns: [
            'name',
            'github_watchers',
            'github_stars',
            'week_issue_change',
            'updated_at',
            'license',
            'created_at',
          ],
        },
      ],
    },
    FormatColumn: {
      FormatColumns: [
        {
          Name: 'formatColumn-github_watchers',
          Scope: {
            ColumnIds: ['github_watchers'],
          },
          Style: {
            ForeColor: 'LightBlue',
            FontWeight: 'Bold',
            Alignment: 'Center',
          },
          RowScope: {
            ExcludeDataRows: true,
          },
        },
        {
          Name: 'formatColumn-github_stars',
          Scope: {
            ColumnIds: ['github_stars'],
          },
          Style: {
            ForeColor: 'LightGreen',
            FontWeight: 'Bold',
          },
          RowScope: {
            ExcludeTotalRows: true,
          },
        },
        {
          Name: 'formatColumn-week_issue_change',
          Scope: {
            ColumnIds: ['week_issue_change'],
          },
          Style: {
            ForeColor: 'var(--ab-color-palette-30)',
            FontWeight: 'Bold',
            Alignment: 'Center',
          },
        },
      ],
    },
  },
};
```
