Module 5 of 16

Intermediate Models

Build reusable transformation steps without exposing half-finished business tables.

Updated

95 minutes1 exercisesFree

Start here

Learning objectives

  • Know when to create an intermediate model
  • Separate reusable logic from final reporting shape
  • Reduce duplication across marts
Intermediate Models Follow the arrows. Each box is one idea you will practice in this module. Stage step 1 Join step 2 Derive step 3 Reuse step 4 Mart step 5 Production analytics engineering turns raw records into governed, trusted business meaning.

The Mental Model

Intermediate models hold reusable transformation logic that is too complex for staging but not final enough for business users.

Intermediate models are the prep bowls in a kitchen. They are useful while cooking, but you do not serve them as the final dish.

Dataset reference: ecommerce tables and grain assumptions.

Aggregate refunds before a reusable join

An order can have several refunds. Produce one refund total per order before sharing that logic with finance and customer-support marts.

with orders(order_id, amount) as (values (101, 100), (102, 50)),
refunds(order_id, amount) as (values (101, 10), (101, 15)),
refund_totals as (
  select order_id, sum(amount) as refunded
  from refunds group by order_id
)
select o.order_id, o.amount - coalesce(r.refunded, 0) as net_amount
from orders o left join refund_totals r on r.order_id = o.order_id;

Expected rows: (101, 75) and (102, 50). The left join preserves an order with no refunds. In dbt, put the reusable result behind a ref and test its order_id for uniqueness. Decide explicitly how pending refunds and cross-currency refunds are handled before applying this pattern to production data.

Interactive Check

Question: Two marts need the same order refund calculation. Should both copy the SQL?

Reveal the answer

No. Put the shared refund logic in an intermediate model, then let both marts ref it.

Practice: Extract Shared Logic

Move repeated refund and order status logic into one intermediate model.

Use the guided lab below to record your result, assumptions, and the check that would catch an incorrect result.

Production notes

Keep these close

  • Intermediate models are useful, but too many create a maze. Each one should remove real duplication or clarify complex logic.

Common mistakes

What usually breaks

  • Creating intermediate models for every tiny SELECT
  • Letting BI tools query intermediate models directly
  • Hiding important business definitions without documentation

Key terms

Vocabulary used in this module

Intermediate model

A model that captures reusable logic between staging and final marts.

DRY

Do not repeat yourself; centralize shared logic once.

Exercises

Practice inside the lesson

30-45 minutesBeginner to Intermediate

Extract Shared Logic

Move repeated refund and order status logic into one intermediate model.

  1. Find duplicated CASE expressions
  2. Create int_order_status_enriched
  3. Point downstream marts to the intermediate model
  4. Explain what duplication disappeared

Expected evidence

A short answer, SQL/YAML snippet, or lineage map that can live directly in the course page notes.

Recap

Key takeaways

  • Intermediate models reduce repeated business logic
  • They should usually not be consumed directly by BI users
  • Good naming makes hidden transformation steps easier to debug

Related resources

Keep learning across CodersSecret