Level 2: AI layer
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.
On this page
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
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;
| id | rows_after_join |
|---|---|
| R-001 | 2 |
| R-002 | 2 |
| R-003 | 2 |
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
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;
| id | term_id | amount |
|---|---|---|
| R-001 | T-002 | 120.00 |
| R-002 | T-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
- Does every fact table have a written grain line?
- Is every key declared with a type, and is every primary key tested for uniqueness?
- Does a fact-to-fact request fail with a reason instead of returning a number?
- 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.
Related
- All 8 parts of this seriesSeries overview
- All the diagrams in one placeDiagrams
- Level 1: Model the data (7 guides)Read level 1
- The 4-question checklistChecklist