> ## Documentation Index
> Fetch the complete documentation index at: https://docs.invoca.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Flexible Aggregation Functions

> The group\_aggregate function aggregates Measures at different granularities than the dimensions used in the search columns.

Use the `group_aggregate` function to aggregate Measures at different granularities than the dimensions used in the search columns.

## How aggregation formulas work

Typically, the groupings and filters used in a formula use the same fields as columns returned in the search results — a grouping equates to an attribute column. For example, in the search `revenue ship mode`, `revenue` is the Measure and `ship mode` is the attribute, or grouping. The result shows total revenue for each ship mode.

Aggregation formulas in general are described in [Aggregation formulas](/s/article/aggregation-formulas).

## About flexible aggregation

Invoca provides flexible aggregation with the `group_aggregate` function, letting you group and filter query results on different dimensions and granularities than the columns already used in the search bar query.

`group_aggregate` uses a sub-query to perform these custom aggregations. If the sub-query is at a higher, less detailed level, Invoca adds the result column to the original query's result. When the sub-query is at a finer detail level than the original query, Invoca uses *roll-up*, or *reaggregation* — particularly useful for comparison analysis.

To use groupings and filters, specify them with the `query_groupings` and `query_filters` keywords. You can also add or exclude groupings or filters.

## Best practices for flexible aggregations

`group_aggregate` calculates a result at a specific aggregation level and returns it at a different aggregation level. For this reaggregation to return correctly, follow these guidelines:

* Wrap `group_aggregate` in an aggregate function, such as `sum` or `average`.
* Make the wrapping function the immediate preceding function.
* Don't use it with conditional operators.

## Examples

For a search on `revenue monthly ship mode`, add a formula to calculate yearly revenue by ship mode:

```text theme={null}
group_aggregate(
  sum(revenue),
  {ship mode, year(commit date)},
  {}
)
```

## When group formula results are finer-grained than the search

Because groupings for group formulas are flexible, the computed column a formula creates can be finer or coarser grained than the search itself. When an additional aggregation is specified by the formula, the results get *reaggregated*.

## Reaggregation scenarios

Some scenarios require aggregation on an already-aggregated result. For example, computing minimum monthly sales per ship mode requires two aggregations: first a **sum** to compute total monthly sales per ship mode, then a **min** to compute the minimum sale for any given month for that ship mode.

## Groupings and filters

Flexible group aggregate formulas allow flexibility in both [groupings](/s/article/group-aggregation-functions) and [filters](/s/article/filtered-aggregation-functions), giving you the ability to specify only groupings, or only filters.

## Where to go next

* For more examples, see the `group_aggregate` function in the [Formula function reference](/s/article/formula-function-reference).
* [Aggregation formulas](/s/article/aggregation-formulas)
* [Group aggregation functions](/s/article/group-aggregation-functions)
