Complex usage of jinja with dbt

Case Study: Building E-Commerce Data Models with dbt

Susan Sun

Freelance Data Scientist

Jinja statement: loop

Jinja statements are enclosed in {% %}:

  • Looping through items
    • Template:
      {% for ... %} ... {% endfor %}
      
    • Example:
      {% for order_status in order_statuses %}
        SUM(
            CASE WHEN status = '{{ order_status }}' 
              THEN order_id 
            END)
      {% endfor %}
      
Case Study: Building E-Commerce Data Models with dbt

Jinja statement: loop

Repeating SQL:

SELECT 
    user_id,
    SUM(CASE WHEN status = 'Shipped' THEN 1 ELSE 0 END) 
        AS num_orders_Shipped,
    SUM(CASE WHEN status = 'Complete' THEN 1 ELSE 0 END) 
        AS num_orders_Complete,
    SUM(CASE WHEN status = 'Processing' THEN 1 ELSE 0 END) 
        AS num_orders_Processing,        
FROM {{ ref('stg_looker__orders') }}
GROUP BY 1
Case Study: Building E-Commerce Data Models with dbt

Jinja statement: loop

{% set order_statuses = ['Shipped', 'Complete', 'Processing'] %}

SELECT 
    user_id,
    -- Jinja loop
    {% for order_status in order_statuses %}
        SUM(CASE WHEN status = '{{ order_status }}' THEN 1 ELSE 0 END) 
        -- Parametrized column name
        AS num_orders_{{ order_status }}
    {% endfor %}
FROM {{ ref('stg_looker__orders') }}
GROUP BY 1
Case Study: Building E-Commerce Data Models with dbt

Jinja statement: loop

dbt compile output:

SELECT 
    user_id,
    SUM(CASE WHEN status = 'Shipped' THEN 1 ELSE 0 END) 
        AS num_orders_Shipped,
    SUM(CASE WHEN status = 'Complete' THEN 1 ELSE 0 END) 
        AS num_orders_Complete,
    SUM(CASE WHEN status = 'Processing' THEN 1 ELSE 0 END) 
        AS num_orders_Processing,        
FROM "dbt"."main"."stg_looker__orders"
GROUP BY 1
Case Study: Building E-Commerce Data Models with dbt

Jinja statement: macros

A Jinja macro:

  • Is a function, written in Jinja
  • Is reusable
  • Is stored in the /macros folder

COALESCE():

  • returns the first non-null value
  • syntax: COALESCE(value1, value2, ...)
  • e.g. COALESCE(nickname, first_name)

An example of a macro function:

A screenshot of the IDE, with the `coalesce_and_round.sql` file open in the IDE editor showing its macro logic.

Case Study: Building E-Commerce Data Models with dbt

Jinja statement: macros

Jinja statements are enclosed in {% %}:

  • A reusable macro function:
    • Template:
      {% macro ... %} ... {% endmacro %}
      
    • Example:
      {% macro coalesce_and_round(column_name, decimal_places = 2) %}
            ROUND(COALESCE({{ column_name }}, 0), {{ decimal_places }})
      {% endmacro %}
      
Case Study: Building E-Commerce Data Models with dbt

Jinja statement: macros

  • Repeated SQL
    ROUND(COALESCE(sales_amount, 0), 2) AS sales_amount,
    ROUND(COALESCE(cost_of_goods_sold, 0), 2) AS cost_of_goods_sold,
    
  • DRY (Don't Repeat Yourself)
    • Create macro called coalesce_and_round.sql
    • Store in /macros
    • Invoke:
      {{ coalesce_and_round('sales_amount', 2) }} AS sales_amount,
      {{ coalesce_and_round('cost_of_goods_sold', 2) }} AS cost_of_goods_sold,
      
Case Study: Building E-Commerce Data Models with dbt

Managing Jinja whitespace

As written:

{% set traffic_source_values = ['Adwords', 'Email', 'Facebook'] %}
{% set browser_values = ['Chrome', 'Firefox', 'Safari', 'IE', 'Other'] %}

SELECT 
    user_id,
    COUNT(DISTINCT session_id) AS num_web_sessions,
...
Case Study: Building E-Commerce Data Models with dbt

Managing Jinja whitespace

Compiled:




SELECT 
    user_id,
    COUNT(DISTINCT session_id) AS num_web_sessions,
... 
Case Study: Building E-Commerce Data Models with dbt

Managing Jinja whitespace

  • {%- ... %} strips the whitespace before.
  • {% ... -%} strips the whitespace after.
  • {%- ... -%} strips both.

  • An example:

{%- set traffic_source_values = ['Adwords', 'Email', 'Facebook'] -%}
{%- set browser_values = ['Chrome', 'Firefox', 'Safari', 'IE', 'Other'] -%}

SELECT 
    user_id,
    COUNT(DISTINCT session_id) AS num_web_sessions,
...
Case Study: Building E-Commerce Data Models with dbt

Let's practice!

Case Study: Building E-Commerce Data Models with dbt

Preparing Video For Download...