Keeping models modular and avoiding repeated logic
Modularity in dbt means breaking transformation logic into small, reusable, single-purpose models rather than writing one large monolithic SQL file. DRY (Don't Repeat Yourself) principles are achieved by centralizing shared logic in staging models, macros, and packages so that changes only need to be made in one place.
1 · Learn the must-know
- Staging models should do minimal transformation (renaming, casting, light cleanup) and one staging model per source table, so downstream models can reference clean, standardized building blocks instead of re-cleaning raw data repeatedly.
- Use the ref() function to build on other models rather than duplicating SQL logic across multiple models; this also ensures proper dependency resolution and lineage in the DAG.
- Repeated SQL snippets (e.g., business logic like calculating a discount or a common CASE WHEN) should be extracted into Jinja macros so the logic is defined once and reused across many models.
- Intermediate models are used to encapsulate complex or reusable transformation logic between staging and mart layers, preventing the same joins or aggregations from being rewritten in multiple downstream marts.
- Variables (vars) and
dbt_utils/date-spine style packages help avoid hardcoding values or reinventing common utility logic, supporting DRY across an entire project. - Over-modularizing (excessive layering of trivial models) can hurt performance and readability, so modularity should be balanced with the actual reuse and clarity benefits it provides.
2 · Check your understanding
You notice the following CASE expression duplicated across five mart models:
case
when status in ('shipped','delivered') then 'fulfilled'
when status in ('cancelled','returned') then 'unfulfilled'
else 'pending'
end as order_statusWhich approach best follows dbt's DRY principle to eliminate this duplication?
What you have tried across dbt Analytics Engineering's objectives, not a readiness score.
Developing and optimizing dbt models45.16% of the exam*0 of 14 tried
Managing dbt models governance9.68% of the exam*0 of 3 tried
Debugging data modeling errors16.13% of the exam*0 of 5 tried
Troubleshooting and optimizing dbt pipelines6.45% of the exam*0 of 2 tried
Implementing dbt tests9.68% of the exam*0 of 3 tried
Implementing and maintaining external dependencies6.45% of the exam*0 of 2 tried
Leveraging the dbt state6.45% of the exam*0 of 2 tried
* Our estimate. dbt Labs publishes no section weights.
3 · Keep going
Ready for more? Take a weighted mock or try free practice questions.