Jinja and Macros for Analytics Engineers
Jinja and macros bring templating into dbt SQL. Jinja adds variables, loops and conditionals to queries, and macros are reusable snippets, like functions, that generate SQL. They remove repetition, but over-use makes code hard to read, so clarity must stay the priority.
Stage: dbt in Depth · Part of the Analytics Engineer roadmap · Last reviewed 19 November 2026
What to learn in Jinja and Macros
- Write a Jinja for loop that generates repeated columns
- Define a macro and call it from several models
- Use the target and var objects to vary behaviour by environment
- Install and use a package of community macros
- Judge when plain SQL is clearer than a macro
Jinja and Macros interview questions
What is a dbt macro?
A macro is a reusable Jinja function stored in the macros folder that returns SQL text. You call it inside models, which lets you apply the same logic, such as a currency conversion, in many places from one definition.
What is the risk of too much Jinja?
Heavy templating makes the compiled SQL hard to predict and review, and new teammates struggle to follow it. Use macros for genuine repetition and check compiled output, preferring simple SQL when it is readable.
Where Jinja and Macros fits in the Analytics Engineer roadmap
Jinja and Macros belongs to the dbt in Depth stage (intermediate level), which takes about 7 hours. The core craft.
Other units in this stage
Back to the full Analytics Engineer roadmap