---
schema_version: "1"
id: 0ae2d09714421c436988fda274d943db248bfdc314a3b6543d9b79c494090835
title: grouping()
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/analytics/bi_dev_guide_saql/guides/concept/bi-saql-fn-agg-grouping.md
canonical_url: https://developer.salesforce.com/docs/analytics/bi-dev-guide-saql/guide/bi-saql-fn-agg-grouping.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: d5bbbb5f339af678c43218fc692dd16cc7f81e4f75b1ad1f1ef4fc5685b4bf5e
products:
  - Analytics SAQL Developer Guide
  - analytics
---

# grouping()

Returns 1 if null dimension values are due to higher-level aggregates (which usually means the row is a subtotal or grand total), otherwise returns 0.

The `grouping()` function is most useful when paired with the `rollup` modifier on the `group` statement. Invoking `grouping()` lets work with subtotaled data.

## Example - Label Subtotaled Data

Suppose that you have a dataset of opportunity information with amounts totaled by lead source and type. Calculate totals with `rollup`. Then use `grouping()` with a `case` statement to check whether a row is a total and if it is then label it as "all" values.

```nolang
q = load "opportunityData";

--Modify the group statement with rollup to calculate subtotals of grouped measures
q = group q by rollup('Type', 'LeadSource');

q = order q by ('Type', 'LeadSource');

--Determine which rows are totals with grouping(), which returns 1 if a row is a total
q = foreach q generate
    (case
        when grouping('Type') == 1 then "All Types"
        else 'Type'
    end) as 'Type',
    (case
        when grouping('LeadSource') == 1 then "All Lead Sources"
        else 'LeadSource'
    end) as 'LeadSource',
    sum('Amount') as 'sum_Amount';
```

![Total rows labeled with grouping()](https://developer.salesforce.com/media/bi_dev_guide_saql/bi_saql_rollup_grouping_example.png)
