Project Structure, Modularity, Jinja, and Macros
dbt Analytics Engineering · Lesson 2 of 17 · 9:18
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the dbt Analytics Engineering Certification exam. In this lesson: the staging, intermediate, and marts layers and their naming conventions, modularity and the DRY principle, Jinja expressions and statements, and macros, with their arguments and package macros. Includes three original practice questions, with Orchardline Meal Kits, a fictional meal-kit company, as the example.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
The same logic, eleven times
Orchardline's analysts convert prices from cents to dollars in eleven different models, and two of them round differently. The finance and marketing teams also built their own orders tables, and their totals never agree. This lesson covers how a dbt project is structured, what modularity and the dry principle mean, and how Jinja and macros help you write logic once.
Three layers
A dbt project usually has three layers of models. Staging models have a one to one relationship with source tables, sit in one folder per source system, and only rename, cast types, do basic computations like cents to dollars, and sort values into categories, with no joins and no aggregations. They are usually views, and their names start with the stg prefix, then the source system, a double underscore, and the entity in the plural. Intermediate models are purpose built steps, such as changing the grain or isolating complex logic, and they are not exposed to end users. Marts are wide tables named after one business entity, like customers or orders, and materialized as tables or incremental models.
Modularity and the dry principle
Modularity means building a project from small models that each do one job, and that other models reuse through ref. The dry principle, short for don't repeat yourself, says that each piece of logic should live in exactly one place. Renaming and casting happen once, in staging, and every downstream model inherits them. Logic that several models need, like converting cents, becomes a macro, and logic that produces a dataset becomes its own model. A model with a single job is also easier to test, to reuse, and to debug.
Jinja
dbt models are SQL files with Jinja, a templating language. Double curly braces print the result of an expression, like a ref or a variable, while curly braces with percent signs run statements, such as set, if, and for loops. Here, a for loop writes one column per subscription plan, so nobody copies the same case expression by hand. dbt compiles the Jinja into plain SQL, which you find in the compiled folder, under target. And string arguments need quotes inside Jinja: without them, Jinja looks for a variable with that name, and compiles to nothing.
Macros
A macro is a reusable piece of Jinja, comparable to a function, stored in a SQL file in the macros folder. It starts with the macro keyword, a name, and arguments, which can have default values, and it ends with end macro. Models call it inside double curly braces, and dbt replaces the call with the SQL it returns. Macros from a package are called with the package name first, like dbt utils, dot, generate surrogate key. And since the macro is defined once, fixing its rounding fixes every model that calls it.
Recap
Let's recap. Staging models clean one source table each, with no joins or aggregations, usually as views. Intermediate models are purpose built steps, and marts are wide tables named after one entity. The dry principle puts each piece of logic in one place, through ref and macros. Jinja expressions print, statements run, and the compiled SQL lands in the target folder. And string arguments inside Jinja always need quotes.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
Finance and marketing each want orders data with a few columns of their own. The first draft builds two separate orders models with different logic. What does the recommended structure suggest?
- Keep both models and document which one each team uses
- Move each team’s logic into its own staging model
- Materialize both orders models as ephemeral
- Build one orders mart, a single source of truth
Show the answer
Build one orders mart, a single source of truth
Building the same concept differently for two teams is an anti pattern, so one orders mart serves both teams, with the columns they need. “Keep both models and document which one each team uses” is wrong, since two versions of one concept drift apart, documented or not. “Move each team’s logic into its own staging model” is wrong, since team logic does not belong in staging, which only cleans one source table. And “Materialize both orders models as ephemeral” is wrong, since ephemeral models are not built in the warehouse, so neither team could query them.
A model contains {{ cents_to_dollars(amount_cents) }}, with no quotes. In the compiled SQL, nothing appears where the column name should be. Why?
- Unquoted, Jinja reads it as a variable that does not exist
- Macros must be called inside {% %} to return SQL
- Macros cannot take column names as arguments
- Macro arguments must be declared in dbt_project.yml
Show the answer
Unquoted, Jinja reads it as a variable that does not exist
Without quotes, Jinja looks for a variable named amount cents, finds none, and compiles to nothing. “Macros must be called inside {% %} to return SQL” is wrong, since a macro that returns SQL is called inside double curly braces, exactly as written. “Macros cannot take column names as arguments” is wrong, since passing a column name as a quoted string is how such macros are normally called. And “Macro arguments must be declared in dbt_project.yml” is wrong, since a macro declares its arguments itself, in the macro file.
Eleven staging models, across three source systems, convert different cents columns with the same expression. The rounding must now change everywhere. What is the best approach?
- Convert cents in one mart and have staging models ref it
- Keep the expression in each model and comment it
- Write one macro for the conversion and call it in each model
- Merge the eleven models into one large staging model
Show the answer
Write one macro for the conversion and call it in each model
A macro defines the conversion once, each staging model calls it, and the rounding changes in one place. “Convert cents in one mart and have staging models ref it” is wrong, since staging models would then depend on a mart, and the DAG would flow backwards. “Keep the expression in each model and comment it” would only document the copies, which can still drift apart. And “Merge the eleven models into one large staging model” is wrong, since staging keeps one model per source table, and one huge model is harder to test and reuse.