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.
select
id as order_id,
customer_id,
cast(created as timestamp) as ordered_at,
lower(status) as order_status
from {{ source('shop', 'orders') }}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.
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_idMarts: 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.
- Star schema example: facts, dimensions and keys without double-counting
- The reporting layer: where metric definitions live above the marts
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'select
order_date,
count(*) as completed_orders,
sum(order_value) as completed_order_value
from {{ ref('fct_orders') }}
group by order_dateFolder 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 data tests: runnable examples you can break on purpose
- SQL data reconciliation: prove two tables match
models:
my_project:
staging:
+materialized: view
intermediate:
+materialized: view
marts:
+materialized: table