Bird
Raised Fist0
dbtdata~5 mins

Calling macros across projects in dbt - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a macro in dbt?
A macro in dbt is a reusable piece of code written in Jinja that helps automate repetitive tasks in your data transformations.
Click to reveal answer
beginner
How do you call a macro from another project in dbt?
You call a macro from another project by prefixing the macro name with the project name and a dot, like this: {{ project_name.macro_name() }}.
Click to reveal answer
intermediate
Why would you want to call macros across projects in dbt?
Calling macros across projects helps you reuse common logic and avoid duplication, making your code cleaner and easier to maintain.
Click to reveal answer
intermediate
What must be configured to enable calling macros across projects in dbt?
You must list the other project as a dependency in your packages.yml file and run dbt deps to install it.
Click to reveal answer
beginner
Example: How to call a macro named calculate_metrics from a project named analytics_utils?
Use {{ analytics_utils.calculate_metrics() }} inside your dbt model or macro to call it from the analytics_utils project.
Click to reveal answer
How do you reference a macro from another dbt project?
AUse <code>{{ macro_name() }}</code> without prefix
BUse <code>{{ project_name.macro_name() }}</code>
CImport the macro with Python import
DMacros cannot be called across projects
What file do you update to add a dependency on another dbt project?
Apackages.yml
Bdbt_project.yml
Cprofiles.yml
Dmacros.yml
What command installs dependencies listed in packages.yml?
Adbt deps
Bdbt run
Cdbt install
Ddbt build
Why is calling macros across projects useful?
ATo slow down builds
BTo duplicate code
CTo reuse common logic
DTo avoid using macros
Which syntax is correct to call macro foo from project bar?
A{{ foo.bar() }}
B{{ foo() }}
C{{ bar::foo() }}
D{{ bar.foo() }}
Explain how to set up and call a macro from another dbt project.
Think about configuration and syntax steps.
You got /3 concepts.
    Why is it beneficial to call macros across projects in dbt?
    Consider how sharing code helps in teamwork.
    You got /3 concepts.

      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

      1. Step 1: Understand the purpose of macros

        Macros are reusable pieces of code that help avoid repetition.
      2. Step 2: Recognize the benefit of calling macros across projects

        Calling macros across projects allows sharing code, which keeps projects clean and organized.
      3. Final Answer:

        To reuse code and keep projects organized -> Option C
      4. 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

      1. Step 1: Recall the correct macro call syntax

        Macros from other projects are called using {{ project_name.macro_name() }}.
      2. Step 2: Match the syntax to the options

        {{ sales_project.calculate_total() }} uses the correct dot notation and double curly braces.
      3. Final Answer:

        {{ sales_project.calculate_total() }} -> Option A
      4. Quick Check:

        Use dot notation inside {{ }} = {{ sales_project.calculate_total() }} [OK]
      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

      1. Step 1: Check the packages.yml for dependencies

        The sales_project is listed as a dependency, so it is available.
      2. Step 2: Verify the macro call syntax

        The macro is called correctly using {{ sales_project.calculate_discount(100) }}.
      3. Final Answer:

        The macro will run and return the discount value -> Option A
      4. 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

      1. Step 1: Identify common errors in cross-project macro calls

        Missing the other project in packages.yml means dbt cannot find the macro.
      2. Step 2: Compare other options

        Misspelling or syntax errors cause different errors, but missing dependency is most common.
      3. Final Answer:

        The other project is not listed in packages.yml -> Option D
      4. 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

      1. Step 1: Confirm dependency is added

        utils_project is added in packages.yml, so macro can be called.
      2. Step 2: Check correct string passing in macro call

        Strings in Jinja macros should be double-quoted inside {{ }} to avoid syntax issues.
      3. 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.
      4. Final Answer:

        SELECT {{ utils_project.format_date("2024-01-01") }} AS formatted_date -> Option B
      5. 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 unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard