How semantic layers stop fan-out: measures, entities and join paths, tested with MetricFlow
A BI dashboard that joins orders to order lines can double revenue without any error. This guide reproduces the fan-out in DuckDB, then shows how a semantic layer, tested with MetricFlow 0.214 and dbt, computes each measure at its own grain and refuses unsafe joins.
- Reading time
- 14 min
- Sections
- 7
- Code examples
- 5
- Tables
- 7
- Sources
- 9
- Published
- 23 Nov 2026
- What changed
- MetricFlow 0.214.0 and dbt-metricflow 0.16.0 are released
- Why it matters
- The fan-out bug is not exotic.
- What to do
- Find one-to-many joins in dashboards
Ask three analysts for last week's revenue and you can get three numbers. Often none of them is a typo. Each wrote a slightly different query, and one of those queries joined a table at the wrong grain. The classic case is joining orders to order lines and summing the order total: every order is counted once per line, and revenue inflates quietly.
A semantic layer exists to stop this. It is a model that sits between tables and BI tools and stores the business definitions once: what an order is, how revenue is aggregated, which tables can be joined and in which direction. Tools and people then ask for "revenue by country" and the layer writes the SQL. Think of it like a recipe book in a restaurant kitchen. Every cook can still cook, but the sauce tastes the same whoever makes it.
This article reproduces the double counting with real queries in DuckDB, then builds the same model in MetricFlow, the engine behind the dbt Semantic Layer, and shows how it avoids the trap. All numbers below come from running the code with dbt Core 1.12.7, MetricFlow 0.214.0 and DuckDB 2.0.1.
What happened
MetricFlow 0.214.0 and dbt-metricflow 0.16.0 are released (16 November 2026)
MetricFlow, the open source engine that "translates a simple metric definition into reusable SQL", published version 0.214.0 on PyPI on 16 November 2026. The same day, dbt-metricflow 0.16.0, the package that provides the mf command for running MetricFlow against a dbt project, was released. Both were used for the tests in this article, installed with the DuckDB adapter.
Semantic models spread across warehouses and BI tools (2026)
Semantic definitions now live in several places: LookML in Looker, MetricFlow YAML in dbt, semantic views in Snowflake and metric views in Databricks Unity Catalog, which the Unity Catalog article covers. Industry comparisons published in November 2026 track which platforms can read which of these formats. The underlying problem they all solve is the same, and it is the subject of the rest of this article.
The fan-out problem in numbers
Take three small tables.
orders (one row per order)
| order_id | customer_id | order_total | order_date |
|---|---|---|---|
| 1 | 101 | 100.00 | 2026-11-20 |
| 2 | 102 | 50.00 | 2026-11-20 |
| 3 | 101 | 80.00 | 2026-11-21 |
order_items (one row per line)
| item_id | order_id | category | quantity |
|---|---|---|---|
| 11 | 1 | shoes | 2 |
| 12 | 1 | socks | 3 |
| 13 | 1 | hat | 1 |
| 21 | 2 | shoes | 1 |
| 31 | 3 | socks | 4 |
| 32 | 3 | hat | 2 |
customers: 101 is in TR, 102 is in DE.
True revenue is 230.00: 150.00 on 20 November and 80.00 on 21 November. Total units are 13.
The query a BI tool might write
A dashboard shows revenue and units by day. A tool that simply joins the tables and aggregates writes something like:
select o.order_date,
sum(o.order_total) as revenue,
sum(i.quantity) as units
from orders o
join order_items i using (order_id)
group by 1
order by 1;
The result in DuckDB:
| order_date | revenue | units |
|---|---|---|
| 2026-11-20 | 350.00 | 7 |
| 2026-11-21 | 160.00 | 6 |
Units are right. Revenue is 510.00 instead of 230.00, more than double. Order 1 has three lines, so its 100.00 is counted three times; order 3 has two lines, so its 80.00 is counted twice. This is a fan-out: joining from the "one" side of a one-to-many relationship repeats the one-side rows, and any sum over one-side columns inflates. No error is raised, and the number looks plausible.
A related trap, the chasm trap, appears when two fact tables both join to a shared dimension, for example orders and support tickets both joined through customers. Joining both facts in one query multiplies each by the other's row count per customer.
Three ways to get the right answer
1. Aggregate each measure at its own grain, then join. Sum revenue from orders and units from order lines separately, then join the two results on the shared dimension:
with r as (select order_date, sum(order_total) as revenue from orders group by 1),
u as (select order_date, sum(quantity) as units from order_items group by 1)
select order_date, revenue, units, round(revenue / units, 4) as revenue_per_unit
from r full join u using (order_date)
order by 1;
This returns 150.00 and 7 for 20 November, 80.00 and 6 for 21 November, with revenue per unit 21.4286 and 13.3333. Correct.
2. Symmetric aggregates. Keep the join, but make the sum count each order only once. The trick, used by Looker under the name symmetric aggregates, adds a large unique number derived from the primary key to each value, sums distinct values, and subtracts the sum of the distinct offsets:
select o.order_date,
(sum(distinct cast(hash(o.order_id) as hugeint) * 1000000
+ cast(o.order_total * 100 as hugeint))
- sum(distinct cast(hash(o.order_id) as hugeint) * 1000000)) / 100.0 as revenue
from orders o join order_items i using (order_id)
group by 1;
In DuckDB this returns 150.0 and 80.0. It works because each order contributes one distinct value however many times it is repeated. It is a simplified illustration: production implementations take care with hash collisions, numeric precision and very large sums, which is why you want a tool to generate it rather than writing it by hand.
3. Refuse the query. Some questions have no single right answer. "Revenue by product category" is one: an order containing shoes, socks and a hat has one total, not one per category. A semantic layer can decline to answer rather than guess. If the business wants it, someone must define an allocation rule. Allocating each order's total by quantity share gives shoes 83.33, socks 103.33 and hat 43.33, which sum to 230.00. That is a business decision, written down once, not something a join should decide silently.
How a semantic layer encodes the rules
A semantic layer needs three kinds of definition. MetricFlow names them like this:
| Concept | What it says | Example |
|---|---|---|
| Entity | A key and its role: primary (unique here) or foreign (points elsewhere) | order is primary in orders and foreign in order_items |
| Dimension | An attribute to group or filter by, categorical or time | country, category, order_date |
| Measure | A column plus an aggregation, defined on one table | order_total with sum |
| Metric | What users ask for, built from measures | revenue, units, revenue_per_unit |
The model for the three tables, in dbt YAML:
semantic_models:
- name: orders
model: ref('orders')
defaults:
agg_time_dimension: order_date
entities:
- name: order
type: primary
expr: order_id
- name: customer
type: foreign
expr: customer_id
dimensions:
- name: order_date
type: time
type_params:
time_granularity: day
measures:
- name: order_total
agg: sum
- name: order_items
model: ref('order_items')
defaults:
agg_time_dimension: item_date
entities:
- name: order_item
type: primary
expr: item_id
- name: order
type: foreign
expr: order_id
dimensions:
- name: category
type: categorical
- name: item_date
type: time
expr: order_date
type_params:
time_granularity: day
measures:
- name: quantity
agg: sum
- name: customers
model: ref('customers')
entities:
- name: customer
type: primary
expr: customer_id
dimensions:
- name: country
type: categorical
metrics:
- name: revenue
type: simple
type_params:
measure: order_total
- name: units
type: simple
type_params:
measure: quantity
- name: revenue_per_unit
type: derived
type_params:
expr: revenue / units
metrics:
- name: revenue
- name: units
The project also needs a time spine model, a table with one row per day, which MetricFlow uses for time-based joins and gaps.
What MetricFlow did with it
mf query --metrics revenue,units,revenue_per_unit --group-by metric_time__day returned:
| metric_time__day | revenue | units | revenue_per_unit |
|---|---|---|---|
| 2026-11-20 | 150 | 7 | 21.4286 |
| 2026-11-21 | 80 | 6 | 13.3333 |
Adding --explain printed the SQL. It is the first fix from above, generated automatically: one subquery sums order_total from orders grouped by day, a second sums quantity from order_items grouped by day, and a FULL OUTER JOIN on the day combines them. Each measure is aggregated on its own table before any join.
mf query --metrics revenue --group-by customer__country returned TR 180 and DE 50. That join is safe: each order has exactly one customer, so going from orders to customers is many-to-one and cannot repeat orders.
mf query --metrics units --group-by order__customer__country returned TR 12 and DE 1. MetricFlow walked two many-to-one hops, order lines to orders to customers, which is also safe.
Then the unsafe question: mf query --metrics revenue --group-by order_item__category. MetricFlow refused, with the message that the input "does not match any of the available group-by-items" for the revenue metric, listing "No valid join paths exist from the measure to the group-by-item" and noting "(fan-out join support is pending)". It suggested the dimensions that are reachable, such as customer__country and metric_time__day. The layer would rather say no than return 510.
Ratios: sum first, then divide
The derived metric revenue_per_unit shows a second rule. MetricFlow computed revenue and units at the requested grain and divided the totals. The tempting alternative, averaging per-order ratios, gives a different number:
| Method | Value |
|---|---|
| Ratio of sums, 230.00 / 13 | 17.6923 |
| Average of per-order ratios (16.67, 50.00, 13.33) | 26.6667 |
The small order 2, one unit for 50.00, pulls the average of ratios up. Ratio of sums is almost always what a business means by "revenue per unit", and defining it once in the layer stops each dashboard choosing differently.
Continue learning
How this article connects
9 pages link to or from it. Drag to turn, click a dot to open it.
- Snowflake
- dbt
- Text-to-SQL
- DuckDB
- Star schema, one big table or Data Vault? A measured guide to choosing a
- Slowly changing dimensions done right: SCD Type 2, surrogate keys and po
- Unity Catalog explained: namespaces, privileges, credential vending and
- BigQuery costs explained: bytes billed, partition pruning, clustering an
How the main tools handle it
| Tool | Where definitions live | How fan-out is handled |
|---|---|---|
| dbt Semantic Layer and MetricFlow | YAML semantic models and metrics in the dbt project | Measures aggregated per semantic model, joins limited to safe paths; unsafe group-bys refused |
| Looker | LookML views, explores and joins with a declared relationship |
Symmetric aggregates generated when a join fans out |
| Power BI | Semantic model with relationships, cardinality and DAX measures | Measures evaluated in filter context over a star schema rather than over a flat join |
| Warehouse-native views (Snowflake semantic views, Databricks metric views) | Objects in the warehouse catalog | Definitions governed and shared next to the data |
The approaches differ, but all rely on the same ingredients: known grain, declared relationships and measures that know how to aggregate. A well-designed star schema makes every one of them easier; the star schema, one big table or Data Vault article covers that choice, and the slowly changing dimensions article covers dimensions that change over time.
What it means in practice
The fan-out bug is not exotic. It appears whenever a tool lets users drag a measure from one table and a dimension from a table on the "many" side. In the example, revenue was overstated by 280.00 on a true total of 230.00, and nothing flagged it.
A semantic layer moves three decisions from each query to one place: the grain of each table, the direction of safe joins and the aggregation of each measure. That consistency matters more as more consumers query data directly, including AI assistants that turn questions into SQL. A text-to-SQL agent querying raw tables can repeat the fan-out mistake at scale; the same agent querying named metrics through a semantic layer cannot.
There are costs. The layer is another model to maintain, it must stay in sync with table changes, and its refusals frustrate users who want "revenue by category" now. Those refusals are features, though: they surface the places where the business has not agreed on a definition.
What to do now
- Find one-to-many joins in dashboards. List BI datasets that join a header table to a line table and check which measures come from the header.
- Reconcile one metric. Compare dashboard revenue with a direct
sum(order_total)from the orders table for the same period. - Declare grain for every table. Write down the primary key of each fact and dimension table, and test it for uniqueness in your pipeline.
- Define measures where they live. Attach each aggregation to the table at its natural grain, not to a flattened view.
- Use ratio-of-sums metrics. Define ratios as derived metrics over summed numerators and denominators.
- Write allocation rules explicitly. For questions like revenue by category, agree on a rule (by quantity, by line value) and model it as its own measure.
- Read the generated SQL. Use
mf query --explainor your tool's SQL view to confirm each measure is aggregated before joins. - Start small. Model the ten most-used metrics first and point one important dashboard at the layer before migrating the rest.
Risks and open questions
Silent wrong numbers persist outside the layer. Any tool or notebook that queries raw tables can still fan out; the layer only protects queries that go through it.
Symmetric aggregates have edge cases. Hash-based tricks depend on unique primary keys and careful numeric handling; a non-unique key breaks them silently.
Definitions can fork across tools. LookML, MetricFlow YAML, Power BI models and warehouse semantic views can each hold a version of "revenue". Without one source of truth, the layer becomes another place to disagree.
Performance moves to the layer. Generated SQL with several subqueries and full outer joins can be slower than a hand-tuned query; caching or materialized aggregates may be needed.
Refusals need a path forward. If users hit "no valid join path" errors with no guidance, they go back to raw SQL. Pair refusals with documented allocation metrics.
Watch next: whether interchange between semantic formats matures enough that one definition can serve several BI tools, and how AI assistants use semantic layers instead of raw tables.
Frequently asked questions
What is a fan-out in SQL?
It is what happens when you join a table to another table with several matching rows per key, so each original row appears several times. Sums and counts over the repeated columns then inflate. In the example, joining orders to their lines turned 230.00 of revenue into 510.00.
How does a semantic layer prevent double counting?
It knows the grain and keys of each table and attaches each measure to its own table. When a query mixes measures, it aggregates each one separately and joins the results on shared dimensions, and it only follows joins that cannot repeat rows. MetricFlow refuses group-bys that would require a fan-out.
What are symmetric aggregates?
A technique, used by Looker, for computing correct sums and averages over a join that fans out. It makes each repeated row contribute once by summing distinct values tagged with a key-derived offset and removing the offsets afterwards. It needs a unique primary key on the repeated table.
Why did MetricFlow refuse revenue by category?
Because revenue is defined on orders and category lives on order lines, so reaching category from revenue means a one-to-many join. There is no single correct answer without an allocation rule, so the tool refuses rather than inflating the number. Define an allocated revenue measure if the business needs that view.
Do I need a semantic layer if I have a star schema?
A clean star schema prevents many problems, because facts join to dimensions many-to-one. You still benefit from a layer when several tools and teams query the same data, when metrics combine several facts, or when ratios must be defined consistently. The layer and the schema work best together.
Summary
Joining a table to one with many rows per key repeats rows, and sums over the repeated side inflate without any error; in the test, revenue of 230.00 became 510.00. Semantic layers prevent this by declaring entities, dimensions, measures and metrics, aggregating each measure at its own grain before joining and allowing only safe join paths. MetricFlow 0.214.0, run against DuckDB through dbt, generated exactly that SQL, computed ratios as ratio of sums and refused revenue by item category with an explicit fan-out message. Model grain and relationships once, read the generated SQL, and write down allocation rules where the business needs them.
Sources
- Code PyPI: metricflow
- Code PyPI: dbt-metricflow
- Web Data Lakehouse Hub: Semantic Model Interchange Matrix, November 2026
- Docs dbt Docs: Join logic
- Docs dbt Docs: Entities
- Docs dbt Docs: Semantic models
- Docs Google Cloud: Understanding symmetric aggregates in Looker
- Docs Snowflake Docs: Overview of semantic views
- Code PyPI: duckdb
Cite this article
Kelahmet, Ö. C. (2026, November 23). How semantic layers stop fan-out: measures, entities and join paths, tested with MetricFlow. Özer Cem KELAHMET. https://www.ozercemkelahmet.com/articles/semantic-layer-fan-out-metricflow-measures
BibTeX
@online{kelahmet2026semantic,
author = {Kelahmet, {\"O}zer Cem},
title = {{How semantic layers stop fan-out: measures, entities and join paths, tested with MetricFlow}},
year = {2026},
month = {nov},
url = {https://www.ozercemkelahmet.com/articles/semantic-layer-fan-out-metricflow-measures}
}Version historyfirst version
- Published
Factual corrections are listed here with what changed. Wording and formatting edits are not listed.