Lompat ke konten

Pelajaran 12 ยท Joins and Data Modeling

Joins and Unions

Combine data in Sigma with inner, left, right and full outer joins and unions, check join keys and grain, and detect fan-out that silently inflates totals.

Useful answers often need more than one table. Orders live in one place, products in another. A join adds columns by matching rows. A union adds rows by stacking tables. This lesson shows both, and the check that protects your totals.

Join types

Sigma's documentation lists four join types:

Join type Keeps
Inner Only rows with a match on both sides
Left outer All left rows, plus matches from the right
Right outer All right rows, plus matches from the left
Full outer All rows from both sides

Columns without a match are null. For a "list every order, with product details when known" report, put orders on the left and use a left outer join. An inner join would silently drop orders whose product is missing.

Create a join

You can start a join when you add a table element, or from an existing table's element source options. You choose the left table and its columns, add a right table, choose the join type and set one or more join keys. Each key pair must have the same data type. Besides equality, other comparison operators are available, including null-safe ones. Preview the output before you accept it.

SQL Stand-in: inner versus left join

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

Order 3 points to a product that does not exist in the product table. The inner join loses 4 units; the left join keeps them with a null product name.

Fan-out: the silent total killer

If the right table has more than one row per key, each matching left row is repeated. Suppose a price table has two rows for P1 because prices changed over time. Every order for P1 now appears twice, and its quantity is doubled. This is called fan-out.

SQL Stand-in: fan-out and how to detect it

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

Before every join, check that the key is unique on the side that should be "one". After the join, compare row counts and a key total with the original. In Sigma, you can group the lookup table by its key and count rows to find duplicates.

Fixing a duplicate key

  • Add a second key, such as a validity date range, so only one row matches.
  • Pre-aggregate the lookup side to one row per key.
  • Filter it to the current record.
  • Fix the data at the source.

Do not "fix" fan-out by taking a distinct sum of money values. Two different orders can have the same amount.

Unions

A union stacks tables with the same kind of columns, for example sales files from two systems. According to Sigma's documentation:

  • Columns are suggested as matches by data type and name, and you can change them.
  • Matched columns must have the same data type.
  • The default is union all, which keeps duplicates.
  • Tables must be on the same connection.
TEXT
Sales 2025:  order_date, region, amount
Sales 2026:  date,       area,   total
Union:       order_date <- date,  region <- area,  amount <- total

Check that matched columns really mean the same thing. "total" might include tax while "amount" does not. Add a column to each input that names the source, so you can trace rows later.

Because duplicates are kept, decide whether overlapping periods exist. If both systems loaded the same week during a migration, your total includes it twice.

Join or Lookup?

A join builds a combined table. A Lookup adds a column to an existing table from inside a formula. In data models, relationships describe joins that run only when someone uses a related column. Sigma's performance guidance suggests relationships when only some analyses need the extra columns.

Where should the join happen?

Place Good when
Data platform (SQL or pipeline) Many tools need the same joined table
Sigma data model Several workbooks share the logic
Workbook One-off analysis or prototype

Common mistake: choosing an inner join "to keep the data clean". It hides missing matches, which are often the very records that need attention. Use a left join and count the nulls.

A join checklist

  1. State the grain of each table.
  2. Confirm that key types match.
  3. Check uniqueness on the "one" side.
  4. Choose the join type on purpose.
  5. Compare row counts and totals before and after.
  6. Count unmatched rows.

Reading the row counts

Row counts are the fastest evidence that a join behaves. If you left join orders to a lookup table with unique keys, the number of rows stays exactly the same. More rows mean duplicate keys. With an inner join, fewer rows mean unmatched keys. Both can happen at once and partly cancel each other, so also count unmatched rows and duplicate keys separately.

For a union, the expected row count is the sum of the inputs. Then check the date range that each input covers, and look for periods that appear in both.

Example: a growing lookup table

A join that is correct today can break later. Imagine that someone adds a second row for one product to record a new name. The next refresh silently doubles that product's sales. Protect yourself with a standing duplicate-key check on every lookup table, shown on your checks page, and ask the table's owner what guarantees the key is unique. A rule enforced in the data pipeline is stronger than a promise.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which join keeps every order even when no product matches?

    A left outer join keeps all left rows and fills missing columns with nulls.
  2. 2. What causes fan-out?

    Each matching row repeats the left row, which can inflate sums.
  3. 3. What is the default behavior of a Sigma union according to its documentation?

    The default is union all, so overlapping data appears twice.
  4. 4. Can you union tables from two different connections?

    The documentation says unions cannot cross connections.

Summary

  • Joins add columns; unions add rows.
  • Choose inner, left, right or full joins by which rows must survive.
  • Check key uniqueness to avoid fan-out, then compare totals.
  • Unions keep duplicates by default and need matching types on one connection.
  • Put shared join logic where many users can reuse it.

Next: Lookup and Rollup

Progres Anda disimpan di browser ini. Tidak perlu akun.