Module 12 of 16

MetricFlow and the dbt Semantic Layer

See how dbt semantic models produce governed SQL at query time.

115 minutes1 exercisesFree

Start here

Learning objectives

  • Understand semantic model YAML at a high level
  • Know what MetricFlow does
  • Explain how governed metrics can serve BI, apps, and AI
MetricFlow and the dbt Semantic Layer Follow the arrows. Each box is one idea you will practice in this module. YAML step 1 Graph step 2 Metric step 3 SQL step 4 Result step 5 Production analytics engineering turns raw records into governed, trusted business meaning.

The Mental Model

MetricFlow powers the dbt Semantic Layer by using semantic model and metric definitions to generate SQL dynamically for requested metrics and dimensions.

You define the rules once. MetricFlow acts like a careful translator that writes the SQL for each question using those rules.

Tiny Example

We will use a small ecommerce dataset throughout the course. Think of these as the only tables in your first warehouse:

TableGrainExample columns
raw_ordersone row per order eventorder_id, customer_id, amount, status, created_at
raw_order_itemsone row per item inside an orderorder_id, product_id, quantity, item_price
raw_customersone row per customercustomer_id, email, country, created_at

Interactive Check

Question: Why is generated SQL safer than each dashboard author writing their own revenue SQL?

Reveal the answer

The generated SQL comes from one governed metric definition, so all tools use the same calculation, joins, and time rules.

Inline Practice Lab

This lab is intentionally small. You can solve it by reading the table, writing the SQL/YAML mentally, or pasting the snippet into any SQL scratchpad later.

-- Example starter table
select
  order_id,
  customer_id,
  amount,
  status,
  created_at
from raw_orders;

The goal is not tooling setup. The goal is learning the production habit: state the grain, clean one thing, test one assumption, and explain the downstream impact.

Self-Check Quiz

  1. What is the grain of the table you are building?
  2. Which downstream metric or dashboard would be wrong if this model broke?
  3. What test would catch the most likely beginner mistake here?

Real world

Where this shows up

  • Reliable executive dashboards that do not disagree across teams
  • AI analytics agents that query governed metrics instead of guessing SQL
  • Auditable metric changes where owners can see downstream impact before merge

Production notes

Keep these close

  • Keep semantic definitions close to the dbt models they describe. Distance creates drift.

Common mistakes

What usually breaks

  • Confusing measures and metrics
  • Creating semantic definitions on untested models
  • Exposing dimensions that create unsafe joins

Think like an engineer

Questions to answer before shipping

  • Can you explain the grain of this model in one sentence?
  • What breaks downstream if this field becomes null tomorrow?
  • Where should this logic live so it is reused instead of copied?

Key terms

Vocabulary used in this module

MetricFlow

The query engine used by the dbt Semantic Layer to generate metric SQL.

Semantic model

A definition that describes entities, measures, and dimensions for a dbt model.

Exercises

Practice inside the lesson

30-45 minutesBeginner to Intermediate

Read a Semantic Model YAML

Identify entities, measures, dimensions, and metrics in a simplified YAML snippet.

  1. Circle the primary entity
  2. Find the revenue measure
  3. Find the order date time dimension
  4. Find the revenue metric
  5. Explain how a query could ask for revenue by month

Recap

Key takeaways

  • MetricFlow generates SQL from semantic definitions
  • The dbt Semantic Layer connects governed metrics to many consumers
  • Semantic YAML should be reviewed like production code

Related resources

Keep learning across CodersSecret