dbt macros let analytics engineers package common SQL transformations and call them across models, so shared logic stays consistent and easier to maintain. With Jinja powering the syntax, a small macro can cut repeated code and keep a project tidy as its data models grow.

Multiple Choice

What feature in dbt allows for the reuse of common transformations across models?

The feature in dbt that allows for the reuse of common transformations across models is macros. Macros are reusable pieces of SQL code that can be defined in your dbt project, enabling you to encapsulate complex logic or common transformations. Once a macro is created, it can be invoked in multiple models or other macros, which promotes DRY (Don't Repeat Yourself) principles and streamlines maintenance. This makes it easier for data teams to implement consistent transformations without duplicating code, leading to cleaner and more maintainable projects. In contrast, models represent the tables and views in your data warehouse and do not inherently support code reuse like macros do. Migrations are used for database schema changes, while seeds refer to static data files that can be loaded into your database, neither of which contributes to the reuse of SQL transformations.

In a dbt project, the same transformation can crop up in several places: trimming a text field, applying a standard date rule, or converting a currency value. Copying that logic into every model may seem quick at first. Later, when a rule changes, those copies become a maintenance chore. dbt macros offer a cleaner way to reuse SQL logic across models and keep common transformations consistent.

A macro is a reusable block of code written with Jinja, the templating language dbt uses alongside SQL. You define the logic once, then call the macro wherever it’s needed. Think of it as a small recipe: the macro holds the steps, and each model can use that recipe without rewriting them.

Why reusable SQL matters

dbt models are usually SQL files that describe how data should be transformed in a warehouse. A model might clean up customer records, join sales data to product details, or calculate monthly revenue. As a project grows, many models can begin to share small pieces of logic.

For instance, several models may need to turn blank strings into null values. Or they may need to apply the same rule for identifying a valid email address. If each model contains its own copy of that logic, small differences can creep in. One file might handle spaces around a value; another might not. A third might use an older version of the rule.

That’s where macros help. By placing shared logic in one reusable block, a team can make a change in one place and have that change flow through every model that calls it. This supports the familiar software principle of DRY: “Don’t Repeat Yourself.” The idea isn’t to remove every repeated line of SQL. It’s to avoid maintaining several separate versions of logic that should behave the same way.

A small macro, used in more than one model

Macros live in a project’s macros directory and are commonly written in .sql files. Here’s a simple example:


{% macro clean_text(column_name) %}

nullif(trim({{ column_name }}), '')

{% endmacro %}

This macro accepts a column name. It removes spaces at the start and end of the value, then returns NULL if the result is an empty string. A model can call it like this:


select

{{ clean_text('customer_name') }} as customer_name

from {{ source('crm', 'customers') }}

The macro call is wrapped in Jinja syntax. When dbt compiles the model, it replaces that call with the SQL expression generated by the macro. The warehouse ultimately receives ordinary SQL; the macro is a way to generate that SQL consistently.

Another model can use the same macro for a different column:


select

customer_id,

{{ clean_text('company_name') }} as company_name

from {{ ref('stg_accounts') }}

The result is simple but useful: one definition, several uses. If the team later decides that text should be cleaned in a different way, there’s one shared place to update.

Macros can do more than tidy up a column

Some macros are small helpers. Others can produce larger blocks of SQL, accept arguments, or use control flow such as loops and conditions. That makes them useful for patterns that are tedious to repeat by hand.

For example, a team might have several event tables with the same set of metric columns. A macro could generate a consistent calculation for those columns. Another macro might build a date filter, or help create a repeated section of a query. Macros can also call other macros, allowing a project to organize related logic into reusable pieces.

This flexibility comes with a caveat: a macro can make SQL harder to understand if it hides too much. If a model contains a simple expression, writing that expression directly may be clearer than creating a macro for it. A macro earns its place when it removes meaningful repetition, captures a shared rule, or makes a complex pattern easier to maintain.

A helpful rule of thumb is to ask: “Would changing this logic in one place matter?” If the answer is yes because several models depend on it, a macro may be a good fit. If the code appears only once and is easy to read, abstraction may add more work than value.

Macros and models have different jobs

It helps to keep the main dbt building blocks distinct. A model describes a dataset that dbt builds in the warehouse, often as a table or view. It contains a query and contributes to the project’s data flow. A macro, by contrast, is a reusable code generator. It can help write part of a model’s SQL, but it is not itself a warehouse table or view.

That distinction is easy to blur because both models and macros are often written in .sql files. Their roles, though, are different: models define transformed data, while macros help produce the SQL that performs transformations.

Seeds are different again. They are static data files, typically CSV files, that dbt can load into the warehouse. A seed might contain a small mapping table, such as country codes or product categories. It provides data; it doesn’t package repeated SQL logic.

Database migrations serve another purpose: they manage changes to database structures over time. They’re not the dbt feature for sharing transformations across models. Knowing these differences makes project design easier, especially when a repository contains many folders and file types.

Keep macros readable and predictable

Because macros can affect multiple models at once, clear names and simple behavior matter. A name like clean_text gives a quick clue about the macro’s job. A vague name like helper_2 doesn’t. It’s also useful to document unusual rules, especially when a macro handles an important business definition.

Arguments should be clear, too. If a macro accepts a column name, a date range, or a formatting rule, the call site should make those inputs easy to recognize. This helps someone reading a model understand what the generated SQL is meant to do.

When a macro changes, it’s sensible to review the models that call it. A shared macro can have a broad reach, which is its strength—and its main risk. A change to a commonly used cleaning rule might affect dozens of downstream datasets. Clear ownership and sensible naming help teams understand that reach before making edits.

It’s also worth remembering that a macro’s output may not look exactly like the template that produced it. dbt’s compiled SQL can be inspected to see the final query sent to the warehouse. Looking at that output is a practical way to understand what a macro is generating, particularly when Jinja conditions or loops make the source file less obvious.

A tool for consistency, not a shortcut for everything

Macros are one of the most useful ways to reuse transformations in dbt. They help data teams express shared logic once, reduce copy-and-paste code, and keep common rules aligned across models. Just as important, they make those rules easier to update when business needs change.

Still, abstraction works best when it clarifies a project rather than obscuring it. A tidy macro should have a clear purpose, a sensible name, and an output that teammates can follow. Use one for a genuinely shared transformation; keep straightforward, one-off SQL close to the model that needs it.

That balance is what makes a dbt project easier to live with. Models describe the data a team wants to build. Macros help those models share the same reliable building blocks—without turning every query into a puzzle.