Creating a Model Leveraging dbt Macros

Lab Steps

lock
Understanding What Are dbt Jinja Macros
lock
Connecting to the dbt Web IDE
lock
Defining a Custom dbt Macro
lock
Creating a Model Leveraging dbt Macros
lock
Executing the dbt Model
Need help? Contact our support team

Here you can find the instructions for this specific Lab Step.

If you are ready for a real environment experience please start the Lab. Keep in mind that you'll need to start from the first step.

Introduction

In this lab step, you will create a dbt model that leverages both a native and a custom dbt macro.

 

Instructions

1. Create a file named d_orders.sql into the models folder by right-clicking on the folder name, and clicking on New File:

alt

 

2. Open the d_orders.sql file and insert the following snippet:

Copy code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
with orders as (
    select
        order_id,
        customer_id,
        employee_id,
        order_date,
        {{
            handle_null(
                check_field='shipped_date',
                then_value='not-shipped',
                else_value='shipped'
            ) 
        }} as shipping_status
    from {{ ref('stg_orders') }}
)

select *
from orders

The model will contain information about orders, and there is a calculated field that tells whether the order has been shipped or not that is calculated using the handle_null macro.

Furthermore, you used the ref native dbt macro to reference the stg_orders model.

As you can see, both native and custom macros can be invoked in the same way.

 

3. Save the content of the file by clicking CMD + S (macOS users) or CTRL + S (Windows users).

 

Summary

In this lab step, you created a dbt model that leveraged both a native and a custom dbt macro.