📖 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
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
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
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
Hint
Use a select statement with the formatted date inside single quotes.
Practice
(1/5)
1. What is the main reason to call macros across different dbt projects?
easy
A. To make the project run slower
B. To increase the size of the project
C. To reuse code and keep projects organized
D. To avoid using any dependencies
Solution
Step 1: Understand the purpose of macros
Macros are reusable pieces of code that help avoid repetition.
Step 2: Recognize the benefit of calling macros across projects
Calling macros across projects allows sharing code, which keeps projects clean and organized.
Final Answer:
To reuse code and keep projects organized -> Option C
Quick Check:
Code reuse = To reuse code and keep projects organized [OK]
Hint: Macros help reuse code across projects easily [OK]
Common Mistakes:
Thinking macros increase project size
Believing macros slow down the project
Ignoring the need for dependencies
2. Which syntax correctly calls a macro named calculate_total from a project named sales_project?
easy
A. {{ sales_project.calculate_total() }}
B. {{ calculate_total() }}
C. {% sales_project.calculate_total() %}
D. {{ sales_project->calculate_total() }}
Solution
Step 1: Recall the correct macro call syntax
Macros from other projects are called using {{ project_name.macro_name() }}.
Step 2: Match the syntax to the options
{{ sales_project.calculate_total() }} uses the correct dot notation and double curly braces.
Hint: Use {{ project_name.macro_name() }} to call macros [OK]
Common Mistakes:
Using {% %} instead of {{ }} for macro calls
Missing the project name before the macro
Using incorrect symbols like ->
3. Given the following packages.yml snippet and macro call, what will happen?
packages:
- package: sales_project
version: 1.0.0
-- In a model SQL file:
SELECT {{ sales_project.calculate_discount(100) }} AS discount_amount
medium
A. The macro will run and return the discount value
B. Error because the macro call syntax is wrong
C. Error because sales_project is not added as a dependency
D. The SQL will run but ignore the macro call
Solution
Step 1: Check the packages.yml for dependencies
The sales_project is listed as a dependency, so it is available.
Step 2: Verify the macro call syntax
The macro is called correctly using {{ sales_project.calculate_discount(100) }}.
Final Answer:
The macro will run and return the discount value -> Option A
Quick Check:
Dependency + correct syntax = macro runs [OK]
Hint: Add project in packages.yml before calling macros [OK]
Common Mistakes:
Forgetting to add dependency in packages.yml
Using wrong macro call syntax
Assuming macro calls run without dependencies
4. You try to call a macro from another project but get an error. Which of these is the most likely cause?
medium
A. The macro name is misspelled in the call
B. You used single curly braces instead of double
C. The macro is called without parentheses
D. The other project is not listed in packages.yml
Solution
Step 1: Identify common errors in cross-project macro calls
Missing the other project in packages.yml means dbt cannot find the macro.
Step 2: Compare other options
Misspelling or syntax errors cause different errors, but missing dependency is most common.
Final Answer:
The other project is not listed in packages.yml -> Option D
Quick Check:
Missing dependency = macro call error [OK]
Hint: Always add dependencies in packages.yml first [OK]
Common Mistakes:
Ignoring packages.yml dependencies
Using wrong macro call syntax
Forgetting parentheses in macro calls
5. You want to call a macro format_date from a project utils_project inside your project. You added utils_project in packages.yml. Which is the correct way to pass a date string '2024-01-01' to the macro and use its result in a model?
hard
A. SELECT {{ utils_project.format_date(`2024-01-01`) }} AS formatted_date
B. SELECT {{ utils_project.format_date("2024-01-01") }} AS formatted_date
C. SELECT {{ utils_project.format_date(2024-01-01) }} AS formatted_date
D. SELECT {{ utils_project.format_date('2024-01-01') AS formatted_date }}
Solution
Step 1: Confirm dependency is added
utils_project is added in packages.yml, so macro can be called.
Step 2: Check correct string passing in macro call
Strings in Jinja macros should be double-quoted inside {{ }} to avoid syntax issues.
Step 3: Validate SQL syntax
SELECT {{ utils_project.format_date("2024-01-01") }} AS formatted_date correctly uses double quotes inside the macro call and proper SQL aliasing.
Final Answer:
SELECT {{ utils_project.format_date("2024-01-01") }} AS formatted_date -> Option B
Quick Check:
Double quotes for strings in macro calls = SELECT {{ utils_project.format_date("2024-01-01") }} AS formatted_date [OK]
Hint: Use double quotes for string args inside {{ }} [OK]
Common Mistakes:
Using unquoted strings causing errors
Incorrect SQL alias syntax
Missing quotes or wrong quote types
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.