Aller au contenu

Leçon 10 · Grouping and Aggregation

Percent of Total and Level of Detail

Calculate shares with PercentOfTotal, Subtotal and GrandTotal in Sigma, choose the right denominator, and compare values across grouping levels safely.

"What share of sales does this product have?" sounds simple. Share of what? All sales, sales in its region, or sales in the current chart color? The denominator is the real question. Sigma has functions that let you state it directly.

GrandTotal

GrandTotal(aggregate) calculates an aggregate over all the data, even when the formula sits inside a group:

TEXT
Sum([Revenue]) / GrandTotal(Sum([Revenue]))

Each group's revenue is divided by overall revenue.

Subtotal

Subtotal(aggregate, mode, [parameters]) calculates at a chosen level. The documentation lists modes for different element types:

Mode Where Meaning
"grand_total" All Everything (default for charts and pivots)
"parent_grouping" Tables Use the parent grouping's keys
"column", "row" Pivots Column or row total
"column_parent", "row_parent" Pivots Total relative to the parent dimensions
"color", "x_axis" Charts Total per color or per axis category
"trellis_column", "trellis_row" Charts Total per small multiple panel

The optional parameter sets how many parent levels to ignore, for the parent-based modes. The aggregate must be written out in full, such as Sum([Revenue]). It cannot be a reference to a column that already holds an aggregate.

PercentOfTotal

PercentOfTotal(aggregate, [mode], [parameters]) computes the share in one step. It uses the same kinds of modes. For tables, the documented default is parent_grouping; for charts and pivot tables it is grand_total.

TEXT
PercentOfTotal(Sum([Revenue]))
PercentOfTotal(Sum([Revenue]), "grand_total")

It is meant for aggregated data in a grouped table, pivot table or chart. Because defaults differ between element types, the same formula can mean different things after you move it from a table to a chart. State the mode when it matters.

Two denominators, two answers

SQL Stand-in: share of region versus share of company

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

North's Desk is 75 percent of North but only 30 percent of the company. Both are correct. Name the columns so no reader can mix them up: "Share of region" and "Share of company".

What about filters?

Totals are calculated from the data available to the element. If a filter removes South, "share of company" becomes a share of what remains. When you need a denominator that ignores a filter, compute it in a place that the filter does not reach, such as a separate unfiltered element, and bring the value in with a Lookup or Rollup. Document which filters the denominator respects, and test that the result is what you expect.

Level of detail thinking

Analysts often need a value at one level to be used at another. Examples:

  • Compare each order with the average order value of its customer.
  • Show each product's rank and its region's total in the same row.
  • Count how many customers placed more than one order.

For the third question, aggregate in two steps: first count orders per customer, then count customers with a count above one.

Python Stand-in: two-step aggregation

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.

In Sigma, one way to do this is a grouping by customer with CountDistinct([Order ID]), then a second element or summary that counts customers where that value is above 1. A self-rollup is another option.

Checking shares

A quick test: do the shares in one group add up to 100 percent? If they do not, check whether rows were filtered after the denominator was calculated, whether a Top N filter hides some items, or whether the mode points at a different level than you expect. With a Top N list, shares of all products can add to less than 100 percent. That may be correct, but you should say so.

Percent change versus percentage points

A rise from 10 percent to 12 percent is 2 percentage points, but a 20 percent relative increase. Label which one you show. Misreading this can turn a small change into a dramatic headline.

TEXT
Points change:    [Share This Year] - [Share Last Year]
Relative change:  [Share This Year] / [Share Last Year] - 1

Common mistake: adding or averaging percentages from different groups. 50 percent of a tiny region and 50 percent of a huge one do not carry equal weight. Recompute from the underlying totals.

Naming your denominator

A good habit is to finish this sentence before you write the formula: "Out of all ______, this is the share that ______." For example, "out of all paid revenue in this region, this is the share that came from desks". The first blank is your denominator and the second is your numerator.

The sentence exposes hidden choices. Are refunded orders part of "all"? Is the period the same for the top and the bottom of the fraction? Does "all customers" mean everyone on file, or only customers who bought something in the period? Each answer gives a different percentage.

Put the finished sentence in the column description or in a short note under the chart. When a colleague gets a different number, compare sentences before you compare formulas. Very often the two of you answered different questions.

Also show the underlying amounts near any share. A product that grew from 1 percent to 2 percent of sales has doubled its share, yet may still be too small to matter. The amount lets readers judge that for themselves.

A blank or zero denominator

A share is undefined when the denominator is zero or missing. Return an empty value or a clear label in that case, rather than showing zero percent, which suggests that something was measured. Also watch for negative amounts such as refunds, which can produce shares below zero or above 100 percent.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What do you put inside GrandTotal?

    GrandTotal calculates the given aggregate over all the data.
  2. 2. According to the documentation, what is the default PercentOfTotal mode for tables?

    Tables default to the parent grouping, while charts and pivots default to the grand total.
  3. 3. A share rises from 10% to 12%. Which statement is correct?

    The difference is 2 points; relative to 10, that is 20 percent.
  4. 4. How do you find the share of customers with more than one order?

    This needs two levels of aggregation.

Summary

  • A share is only meaningful with a clear denominator.
  • GrandTotal, Subtotal and PercentOfTotal state the level directly.
  • Modes and defaults differ between tables, pivots and charts.
  • Filters change denominators unless you calculate them elsewhere.
  • Distinguish percentage points from relative change.

Next: Pivot Tables

Votre progression est enregistrée dans ce navigateur. Aucun compte nécessaire.