dbt in Practice · Lesson 3 of 5 · 12 min

Macros and documentation

Write a rule once and reuse it everywhere, then explain your models so others can use them.

dbt models can contain Jinja, a small template language. A macro is a reusable piece of SQL. Instead of copying the same CASE into ten models, you write it once:

{% macro temp_band(col) %} CASE WHEN {{ col }} < 10 THEN 'cold' WHEN {{ col }} <= 18 THEN 'mild' ELSE 'warm' END {% endmacro %}

← Previous: Tests that catch real problems