Saltar al contenido

Lección 9 · Grouping and Aggregation

Groupings and Calculations

Group a Sigma table, add calculations at each level, build multi-level groupings, handle totals and distinct counts, and keep your row grain clear.

Grouping is the heart of analysis in Sigma. A grouping collects rows that share the same value, such as all rows for one region, so you can calculate one result for each group. Unlike a plain SQL GROUP BY result, a grouped Sigma table lets you expand a group and see more detail.

Create a grouping

In the table's settings you add a grouping and choose a column. Under that grouping you add calculations, and each calculation produces one value per group.

TEXT
Grouping:      Region
Calculations:  Sum([Revenue])
               CountDistinct([Order ID])
               Sum([Revenue]) / CountDistinct([Order ID])

The third calculation is the average order value: total revenue divided by the number of orders. It is built from aggregates, which is the safe way to make a ratio.

When you add a numeric column to a calculation, Sigma picks a default aggregate, often Sum. You can change it, for example to a distinct count. Always read the formula bar to see what is being computed.

SQL Stand-in: group by region with three calculations

Press Run (or Ctrl + Enter) to see the result here.

Try replacing the distinct count with COUNT(*). You will get "revenue per line", which is a different measure.

Levels

You can add several groupings to make levels, such as year, then region, then product. Each calculation belongs to the level where you put it.

flowchart TD
  A["Whole table: summaries"] --> B["Level 1: Region"]
  B --> C["Level 2: Product"]
  C --> D["Base rows: order lines"]

Each level calculates over the rows beneath it.

A Sum([Revenue]) at the region level adds every row in that region. The same formula at the product level adds only that product within that region.

Using values from other levels

A column at a lower level can use a value from a higher level, because each product row belongs to exactly one region. For example, the share of a product within its region:

TEXT
Product level:  [Product Revenue] / [Region Revenue]

Going the other way needs an aggregate. A region row has many products, so "the product" has no single value there. You must say how to combine them, such as with Max or Count. Sigma's functions Subtotal, PercentOfTotal and GrandTotal also help, as the next lesson shows.

Conditional measures

Combine grouping with conditional aggregates to compare parts of a group:

TEXT
Paid revenue:      SumIf([Revenue], [Status] = "Paid")
Refunded orders:   CountDistinctIf([Order ID], [Status] = "Refunded")
Refund rate:       CountDistinctIf([Order ID], [Status] = "Refunded")
                   / CountDistinct([Order ID])

With one grouping and many measures, you avoid building a separate table for every status.

Totals

Sigma's documentation notes that totals are hidden in a grouped table by default. You can show a grand total or a subtotal for a column from its menu, or show all totals. You can also change what a subtotal computes.

Be careful with totals of ratios and distinct counts. Distinct customers in North plus distinct customers in South may be more than total distinct customers, because one customer may buy in both regions.

Python Stand-in: why distinct counts do not add up

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A correct grand total of distinct customers is calculated from the base rows, not by adding the group results.

Manage groupings

  • Expand or collapse groups to move between summary and detail.
  • Remove a column from a grouping, or remove the whole grouping, from its menu.
  • Change the order of grouping levels to change the story.
  • Collapse levels you do not need. Sigma's performance guidance also suggests collapsing groups.

If a child element uses a grouped table as its source, check which level of data the child receives before you aggregate again. Averaging group averages gives a different answer from averaging the base rows.

Common mistake: summing a value that repeats on every row, such as an order-level shipping fee copied onto each order line. The sum multiplies the fee by the number of lines. Fix the grain: keep the fee in an order-level table, or take it once per order.

Verify a grouping

Pick one small group, expand it, and add its rows by hand. Then compare the grand total with a trusted number from the source system. Do this once per new report, and again after any change to joins or filters.

A worked example in words

Imagine a support team with one row per ticket. Columns include team, priority, opened date, closed date and a satisfaction score that is often empty.

Group by team first. Add a ticket count, then a count of open tickets using a conditional count on a missing closed date. Next, add the average score. Pause here, because the average describes only the tickets that have a score. A team with two scored tickets out of two hundred can look excellent by accident. Add a second calculation that counts scored tickets, and show it beside the average so that readers can judge how much to trust it.

Now add priority as a second grouping level under team. The same counts are calculated for each team and priority pair. If you want each priority's share of its team's tickets, use the team-level count as the denominator. Expand one team and check that the priority counts add up to the team count. If they do not, look for filters or an unexpected value in the priority column, such as an empty one.

This habit of pairing every average or rate with the count behind it takes one extra column and prevents many bad decisions. It is especially useful at lower levels, where groups become small and a single unusual row can move the result a long way.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does a calculation under a grouping produce?

    Calculations in a grouping produce one value for each group.
  2. 2. Why might the sum of per-region distinct customers exceed the total?

    A customer in two regions is counted in each region but once overall.
  3. 3. How is average order value best calculated when rows are order lines?

    That divides by the number of orders, not the number of lines.
  4. 4. What is true about totals in a grouped table according to the documentation?

    You enable grand totals or subtotals when you need them.

Summary

  • Groupings create levels; calculations belong to a level.
  • Build ratios from aggregates, and use conditional aggregates for parts of a group.
  • Distinct counts and ratios do not simply add up.
  • Totals are hidden by default; show and verify them.
  • Keep the row grain clear to avoid double counting.

Next: Percent of Total and Level of Detail

Tu progreso se guarda en este navegador. No necesitas una cuenta.