Level 2: AI layer

Make the bad join impossible

Grain, keys and allowed joins: rules a semantic layer can enforce.

Guide 1 showed a join that doubled April revenue without an error. Write the grain down, type every key, and the join stops being a warning in a document and becomes one the engine refuses.

Updated November 2026Example data, not from a client.
On this page
  1. Start with the grain line
  2. Type every key
  3. The join the engine refuses
  4. In a BI model
  5. When the business does link them
  6. What I check

In guide 1, April revenue was 100.00. Join Revenue to Terms on customer and product, and it became 200.00. Nothing errored. Each revenue row matched both contract terms, so each one was counted twice.

Most teams respond with documentation: “don't join these two.” That works until someone new writes the query, or an AI tool writes it for them. A semantic layer can do better. It can make the join impossible to express.

Start with the grain line

Guide 2 asked for one sentence per fact table: what one row means and what identifies it. For the example:

  • Revenue. One row is one recognized revenue entry, identified by id.
  • Terms. One row is one recorded contract term, identified by id.

Customer and Product are the dimensions both facts share. Kimball calls them conformed dimensions: one table each, with the same keys and the same meaning, used by every fact that needs them. Those two sentences and two dimensions are the whole safety rule. The rest of this piece is about getting a tool to enforce them.

Type every key

A semantic layer needs to know what kind of key each column is. dbt's MetricFlow calls a key an entity and uses four entity types. The two that matter here: a primary entity (PK on the diagrams) has one row per value and no nulls; a foreign entity (FK) can repeat, and can be null. Here's Revenue and Customer, written in the dbt 1.12 YAML format:

models:
  - name: revenue
    description: >-
      One row per recognized revenue entry,
      identified by id.
    semantic_model:
      enabled: true
    agg_time_dimension: reporting_date
    columns:
      - name: id
        entity:
          type: primary
          name: revenue_entry
      - name: customer
        entity:
          type: foreign
          name: customer
      - name: product
        entity:
          type: foreign
          name: product
      - name: reporting_date
        granularity: day
        dimension:
          type: time

  - name: customer
    semantic_model:
      enabled: true
    columns:
      - name: customer
        entity:
          type: primary
          name: customer

Terms gets the same shape, with contract_term as its primary entity and customer and product as foreign entities. Product mirrors Customer.

A type is a claim, so test it. If id isn't unique, calling it primary is a lie the engine will believe:

-- Every key you call primary should
-- identify exactly one row.
SELECT 'revenue' AS model,
       COUNT(*) - COUNT(DISTINCT id)
         AS duplicate_keys
FROM revenue
UNION ALL
SELECT 'terms',
       COUNT(*) - COUNT(DISTINCT id)
FROM terms
UNION ALL
SELECT 'customer',
       COUNT(*) - COUNT(DISTINCT customer)
FROM customer
UNION ALL
SELECT 'product',
       COUNT(*) - COUNT(DISTINCT product)
FROM product;

All four return 0 duplicates on the example.

The join the engine refuses

Diagram · Grain, keys and allowed joins: rules the semantic layer enforces

Grain, keys and allowed joins: rules the semantic layer enforces Wrong: Revenue and Terms, both fact tables with PK id and FK customer and product, joined to each other FK to FK on customer and product, many to many, so April's 100.00 becomes 200.00. Right: the dimension tables Customer (PK customer) and Product (PK product) each relate exactly one to zero or many to both Revenue and Terms. A dashed line between Revenue and Terms is crossed out and labeled refused. Fact tables have dark headers tagged FACT and dimension tables have green headers tagged DIM. A legend shows the line-end marks: exactly one, zero or many, and one or many. Example data, not from a client. WRONG · JOINED BEFORE THE SUM Revenue FACT PK id FK customer FK product Terms FACT PK id FK customer FK product FK to FK on customer + product. Rows multiply: April's 100.00 becomes 200.00. RIGHT · TYPED KEYS, ENFORCED JOINS Customer DIM PK customer Revenue FACT PK id FK customer FK product Terms FACT PK id FK customer FK product Product DIM PK product refused exactly one zero or many one or many
Declare each key as PK or FK and the engine walks only allowed joins, so the fact-to-fact join that doubled April is refused instead of documented. Example data, not from a client. Open full size

MetricFlow builds a graph from these declarations and only walks certain edges. Its join rules allow foreign to primary, which is a fact looking up a dimension. They refuse primary to foreign and foreign to foreign as fan-out joins. Revenue to Terms on customer is foreign to foreign. It's exactly the join that doubled April:

-- The guide 1 join: foreign key to
-- foreign key.
SELECT r.id, COUNT(*) AS rows_after_join
FROM revenue r
JOIN terms t
  ON t.customer = r.customer
 AND t.product = r.product
GROUP BY r.id;
Result
idrows_after_join
R-0012
R-0022
R-0032

Result on the example. Example data, not from a client.

I built the example as a small dbt project and asked MetricFlow for net revenue grouped by a contract term field. It stopped at query resolution with “No valid join paths exist from the measure to the group-by-item.” The same request grouped by customer returned 100.00 for April. The bad question doesn't produce a wrong number. It produces no number and a reason.

Diagram · Join rules by key: what the engine allows

Join rules by key: what the engine allows Grain: Revenue has one row per revenue entry, keyed by id; Terms has one row per contract term, keyed by id. Join rules applied by dbt MetricFlow, by key type: FK to PK allowed; PK to PK allowed; PK to FK refused as fan-out; FK to FK refused as fan-out. A join that lands on an FK could fan out. Example data, not from a client. GRAIN, ONE LINE PER TABLE Revenue: one row per revenue entry (id) Terms: one row per contract term (id) JOIN RULES BY KEY (DBT METRICFLOW) FK → PK allowed PK → PK allowed PK → FK refused: fan-out FK → FK refused: fan-out A join that lands on an FK could fan out.
One line of grain per table, and the join rules the engine applies. The two allowed joins land on a PK. A join that lands on an FK could fan out, so it's refused. Example data, not from a client. Open full size

When you ask for metrics from both facts at once, MetricFlow aggregates each one and then combines them with a full outer join on the shared dimensions. That's the “total each side first” fix from guide 1, done by the engine.

In a BI model

Power BI enforces a similar shape through relationships. Microsoft's star schema guidance says the one side of a one-to-many relationship is always a dimension and the many side a fact. Power BI relationships also aggregate each table before combining them, so they wouldn't double this total in the first place. A direct fact-to-fact relationship still limits how you can filter and group, and Microsoft's many-to-many guidance generally recommends against it. The danger is anywhere a join runs before the sum: a view, a Power Query merge, or SQL an agent writes.

When the business does link them

Sometimes revenue really is tied to a term, for example the term in force on the reporting date. That's a business rule, not a key, so write it down and join on the date range instead:

-- A written business rule: the term in
-- force on the reporting date.
SELECT r.id, t.id AS term_id, r.amount
FROM revenue r
JOIN terms t
  ON  t.customer = r.customer
  AND t.product = r.product
  AND r.reporting_date
      BETWEEN t.term_start AND t.term_end
WHERE r.reporting_date >= '2025-04-01'
  AND r.reporting_date < '2025-05-01'
ORDER BY r.id;

-- And the test that keeps it honest:
-- every revenue row matches exactly one
-- term.
SELECT r.id, COUNT(t.id) AS matching_terms
FROM revenue r
LEFT JOIN terms t
  ON  t.customer = r.customer
  AND t.product = r.product
  AND r.reporting_date
      BETWEEN t.term_start AND t.term_end
GROUP BY r.id
HAVING COUNT(t.id) <> 1;
Result
idterm_idamount
R-001T-002120.00
R-002T-002-20.00

Result on the example. Example data, not from a client.

Both April rows fall inside T-002, so April still sums to 100.00. The second query returns no rows: every revenue entry matches exactly one term. If that test ever returns a row, the rule broke, and the number can't be trusted until someone looks. A range rule like this is usually built in the warehouse as its own modeled table, then exposed with its own primary key.

What I check before a semantic layer goes live

  1. Does every fact table have a written grain line?
  2. Is every key declared with a type, and is every primary key tested for uniqueness?
  3. Does a fact-to-fact request fail with a reason instead of returning a number?
  4. Is every link between facts a written rule with a test, not a key someone guessed?

Document the rule, then let the engine enforce it.

About to put AI on a Power BI model?

I review one model, fixed scope, and tell you what to fix first.

See the model review

Privacy

Related