0
0
dbtdata~30 mins

Calling macros across projects in dbt - Mini Project: Build & Apply

Choose your learning style9 modes available
Calling macros across projects
📖 Scenario: You are working on a data analytics platform using dbt. You have two projects: project_a and project_b. project_a contains a useful macro that formats dates, and you want to use this macro inside project_b to keep your code clean and consistent.
🎯 Goal: Learn how to call a macro defined in one dbt project (project_a) from another project (project_b).
📋 What You'll Learn
Create a macro named format_date inside project_a that formats a date string.
Create a variable sample_date in project_b with a specific date string.
Call the format_date macro from project_a inside project_b using the correct syntax.
Print the formatted date result in project_b.
💡 Why This Matters
🌍 Real World
In real data teams, macros help reuse code for common tasks like formatting dates. Sharing macros across projects avoids duplication and keeps code consistent.
💼 Career
Knowing how to call macros across dbt projects is important for data engineers and analysts working in modular, scalable data pipelines.
Progress0 / 4 steps
1
Create the format_date macro in project_a
In project_a/macros/formatting.sql, create a macro called format_date that takes a parameter date_str and returns the date formatted as 'YYYY-MM-DD' using the Jinja strftime filter.
dbt
Need a hint?

Use Jinja filters as_datetime and strftime to format the date string.

2
Create a variable sample_date in project_b
In project_b/models/sample_model.sql, create a variable called sample_date and assign it the string '2024-06-15 14:30:00'.
dbt
Need a hint?

Use Jinja set to create the variable.

3
Call the format_date macro from project_a inside project_b
In project_b/models/sample_model.sql, call the macro format_date from project_a using the syntax {{ project_a.format_date(sample_date) }} and assign the result to a variable called formatted_date.
dbt
Need a hint?

Use the project namespace project_a before the macro name.

4
Print the formatted date result in project_b
Add a select statement in project_b/models/sample_model.sql that returns the formatted_date variable as a column named formatted_date. Use select '{{ formatted_date }}' as formatted_date.
dbt
Need a hint?

Use a select statement with the formatted date inside single quotes.