Zum Inhalt springen

Lektion 13 · Joins and Data Modeling

Lookup and Rollup

Pull related values into a table with Lookup and Rollup in Sigma, including aggregates, composite keys, null keys, self-rollups and choosing between them.

Sometimes you do not want to build a new joined table. You just want one more column: the customer's country, or the total of all invoices for each customer. Sigma's join functions Lookup and Rollup do that from inside a column formula.

Lookup

TEXT
Lookup(formula, local key 1, external key 1, [local key 2], [external key 2], ...)
  • formula: what you want from the other element.
  • local key: a column in the current table.
  • external key: the matching column in the other element.
TEXT
Lookup([Customers/Country], [Customer ID], [Customers/Customer ID])

Read it as: "For this row's Customer ID, find the matching row in Customers and give me its Country."

Each key pair is written as local first, then external. Add more pairs for a composite key, such as region and year:

TEXT
Lookup([Targets/Target],
       [Region], [Targets/Region],
       [Year],   [Targets/Year])

A single Lookup can reference only one external element. Using external keys from two different elements causes an error.

Many matches

If several external rows can match one local row, say how to combine them. Wrap the target in an aggregate such as Sum, or use Rollup. Do not rely on an unstated choice among several rows.

Rollup

Rollup looks up matching rows and aggregates them:

TEXT
Rollup(formula, local key 1, external key 1, [local key 2], [external key 2], ...)

In a Customers table, the total of each customer's orders:

TEXT
Rollup(Sum([Orders/Amount]), [Customer ID], [Orders/Customer ID])

The customer table keeps one row per customer. The orders are summed before they arrive, so there is no fan-out. When you use Sigma's add lookup interface and apply an aggregate, the result becomes a Rollup.

SQL Stand-in: rollup as a pre-aggregated join

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

Chen has no orders and shows nulls. Decide whether to display zero: for an order total, "no orders" really can mean zero, so Zn fits if the order data is complete.

Self-rollup

Rollup can also point back at the same element. For example, give every order line the average profit of its order:

TEXT
Rollup(Avg([Profit]), [Order Number], [Order Number])

Now you can compare each line with its order, such as [Profit] - [Order Avg Profit]. A self-rollup is a handy way to compare an item with its group while keeping every row.

Python Stand-in: compare each line with its order average

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.

Null keys

A null key never matches, even against another null. The documentation says that rows with a null key return no match. If you really want nulls to match each other, use LookupMatchNulls. Think before you do: a missing customer ID is usually a problem, not a shared identity.

Matching problems

Why does a lookup return empty values?

Cause How to check
Extra spaces or different case Compare trimmed, lowercased keys
Different data types (text vs number) Check both column types
Codes with lost leading zeros Inspect raw values
Key really missing Count unmatched rows
Filter on the other element Review its filters

Remember that filters on an element affect what is built from it. If a lookup suddenly returns fewer values, look at the filters on the target element.

Lookup, join or relationship?

  • Use Lookup for a few columns from a source with one row per key.
  • Use Rollup to bring a summary of many rows.
  • Use a join when you need a new combined row set.
  • Use a relationship in a data model when many workbooks need the same link.

Sigma's performance guidance suggests doing lookups and heavy transformations in a parent or in the data model, so child elements reuse them instead of repeating them.

Note: According to Sigma's documentation, column-level security carries through lookups, but not through certain SQL or Python references. The security lesson explains this.

Common mistake: swapping the key order. Local comes first, then external. If your results look random or empty, check that each pair matches the same business meaning.

A worked decision

Suppose you maintain a Customers table with one row per customer, and your manager asks for three new columns: the account owner's name, the total value of paid orders, and the date of the latest order.

The owner's name comes from an account owners table that has one row per owner. One local key finds one row, so a Lookup fits.

The paid total comes from many order rows, so it needs a Rollup with a conditional aggregate. In other words, you describe which orders count and how to add them. The latest order date also comes from many rows, so it is a Rollup too, with a maximum.

Before you trust the new columns, test three customers by hand. Choose one with several orders, one with only unpaid orders and one with no orders at all. The second and third cases show whether your empty values mean "nothing paid" or "nothing found". Write down what each empty value means in the column description, so that the next person does not have to guess.

Finally, compare the sum of the new total column with the total of paid orders in the source table. If the source total is higher, some orders probably have a customer ID that does not exist in your Customers table. Those orders are real money that your customer view cannot show, so report them to the data owner.

Repeat these checks whenever the source tables or their keys change. A quick recheck is much cheaper than a wrong report.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does Rollup do with matching rows?

    Rollup evaluates an aggregate over the matching rows for each local row.
  2. 2. In a key pair, which comes first?

    The syntax lists the local key, then the external key.
  3. 3. What happens when both keys are null in a normal Lookup?

    Null keys never match; LookupMatchNulls exists for the other behavior.
  4. 4. What is a self-rollup useful for?

    It brings a group value onto each row of the same element.

Summary

  • Lookup fetches related values using local and external key pairs.
  • Rollup aggregates matching rows and avoids fan-out in the local table.
  • Self-rollups compare rows with their group.
  • Null keys do not match by default.
  • Diagnose empty results by checking spaces, types, filters and missing keys.

Next: Data Models, Relationships and Metrics

Dein Fortschritt wird in diesem Browser gespeichert. Kein Konto nötig.