Guide

Data model layers in dbt: staging, intermediate and marts, explained with one example.

Layers are how a dbt project says which rules have already been applied to a table. This guide traces one small orders example from raw data to a reporting mart, and says what belongs in each layer, what does not, and how to name it.

What a data model layer is

A layer is a group of models that have all had the same kind of work done to them. Raw tables are what the source sent. Staging models clean and rename one source table each. Intermediate models hold reusable joins and reshaping. Marts are the tables people and BI tools are meant to read. A reader who knows which layer a table is in knows what they can trust about it without opening the SQL.

Layering does not change the design questions. You still have to decide the grain of each table, the keys and the metric definitions. It does decide where each of those decisions is coded, so it is written once and every later model reuses it.

The example: orders and payments from two sources

The example is invented. A shop database sends raw_shop.orders with one row per order and a status, and a payment provider sends raw_payments.charges with one row per charge attempt, amounts in cents. The business question is completed order value per day, counting an order only once it has a successful charge.

The two sources disagree on naming, units and grain: the shop uses id and created, the payment provider uses order_ref and amount_cents, and one order can have several charge attempts. Each layer below fixes one kind of problem.

Staging: one model per source table, light touch only

A staging model selects from exactly one source, renames columns to your conventions, casts types and converts units. It keeps every row, including cancelled orders and failed charges, because removing them is a business rule and belongs further on. Staging models are usually views, named stg_<source>__<table>.

If you are tempted to join or filter in staging, the logic probably belongs in an intermediate model or a mart. Keeping staging thin means that when a source changes a column name, only one model needs fixing.

models/staging/shop/stg_shop__orders.sql · vendor docs
select
    id as order_id,
    customer_id,
    cast(created as timestamp) as ordered_at,
    lower(status) as order_status
from {{ source('shop', 'orders') }}
models/staging/payments/stg_payments__charges.sql · vendor docs
select
    charge_id,
    order_ref as order_id,
    cast(amount_cents as numeric) / 100 as amount,
    lower(outcome) as charge_outcome,
    cast(charged_at as timestamp) as charged_at
from {{ source('payments', 'charges') }}

Intermediate: reusable joins and changes of grain

Intermediate models do the work several marts would otherwise repeat: deduplicating, changing grain and joining sources. Here, payments arrive at charge grain but the question is at order grain, so one intermediate model sums the successful charges per order. Name it after what it does, such as int_orders__paid_amount, and do not expose it to BI users.

Only add an intermediate model when it removes repetition or makes a hard step readable. A project where every mart has a one-to-one intermediate copy has added a layer without adding a rule.

models/intermediate/int_orders__paid_amount.sql · vendor docs
select
    order_id,
    sum(amount) as paid_amount,
    min(charged_at) as first_paid_at
from {{ ref('stg_payments__charges') }}
where charge_outcome = 'succeeded'
group by order_id

Marts: business entities at a declared grain

Marts are where business rules become visible and where people read from. fct_orders has one row per completed, paid order; the rule that cancelled or unpaid orders do not count is written here once. daily_orders aggregates it to one row per day for reporting. Facts are named fct_, dimensions dim_, and both are usually tables.

Write the grain of each mart in its YAML description and test it: a unique key on fct_orders.order_id, and a unique date on daily_orders. If a mart needs shared customer or product attributes, that is the point to add dimensions and a star schema.

models/marts/fct_orders.sql · vendor docs
select
    o.order_id,
    o.customer_id,
    date(o.ordered_at) as order_date,
    p.paid_amount as order_value
from {{ ref('stg_shop__orders') }} as o
inner join {{ ref('int_orders__paid_amount') }} as p
    on o.order_id = p.order_id
where o.order_status = 'completed'
models/marts/daily_orders.sql · vendor docs
select
    order_date,
    count(*) as completed_orders,
    sum(order_value) as completed_order_value
from {{ ref('fct_orders') }}
group by order_date

Folder layout, materialisations and tests per layer

Put each layer in its own folder and set defaults per folder in dbt_project.yml, so a new model inherits the right materialisation and schema without anyone remembering to configure it. Views for staging and intermediate keep storage low while the data is small; tables for marts keep dashboards fast. Switch a model to incremental only after measuring that a full rebuild is too slow or too expensive.

Test each layer for what it promises. Staging: source keys are unique and not null, and accepted values for status columns. Intermediate: the key is unique at the new grain. Marts: grain, relationships to dimensions, and a reconciliation of totals against a figure the business already trusts.

dbt_project.yml (models section) · vendor docs
models:
  my_project:
    staging:
      +materialized: view
    intermediate:
      +materialized: view
    marts:
      +materialized: table

Questions

Related questions

What are the layers of a dbt project?

Most projects use three: staging models that clean and rename one source table each, intermediate models that hold reusable joins and changes of grain, and marts that expose business entities for reporting. Raw source tables sit underneath and are declared as sources, not models.

Do I need an intermediate layer?

Not always. Small projects often go straight from staging to marts. Add an intermediate model when the same join or aggregation would otherwise be repeated in several marts, or when one step is hard enough that it deserves its own tested model.

Is this the same as the medallion (bronze, silver, gold) architecture?

Broadly similar. Bronze is close to raw sources, silver to cleaned staging and intermediate models, and gold to marts. The names matter less than agreeing what each layer guarantees and keeping business rules out of the early layers.

Should BI tools read from staging models?

No. Staging models keep every record and apply no business rules, so two dashboards reading them will apply their own filters and disagree. Point BI tools at marts or a reporting layer built on them.

The Divergence Point — monthly

Where the numbers stop agreeing, and what to do about it. One email a month on data quality, reconciliation and building warehouses that survive contact with a real business. No recycled LinkedIn posts.

Tell me what is broken.

A short call is usually enough to tell whether this is a two-week fix or a two-month one — and whether I am the right person for it.