Recap: dbt case study

Case Study: Building E-Commerce Data Models with dbt

Susan Sun

Freelance Data Scientist

Chapter 1: Setting up dbt

  1. Set up:

    • Check installtion: dbt --versions, which dbt
    • Initialize dbt: dbt init
  2. Load data:

    • Load dbt seed files (e.g. distribution center): dbt seed
    • Load dbt source files. E.g. {{ source('looker_ecommerce', 'orders') }}
  3. Review dbt subcommands: dbt run, dbt test, dbt build

  4. Review dbt project file and folder structure

Case Study: Building E-Commerce Data Models with dbt

Chapter 2: Building dbt models

  1. Build dbt staging, mart, and snapshot models
  2. Add table/column documentation and dbt data tests

A directed acyclic graph diagram showing all dbt sources and models and their relationships to each other, Sources are shown in green. Models and marts are shown in blue.

Case Study: Building E-Commerce Data Models with dbt

Chapter 3: Jinja with dbt

  1. Reduce repeated code using Jinja expressions: set, loop, macros
  2. Use dbt compile and whitespace management for development
{%- set order_statuses = ['Shipped', 'Complete', 'Processing'] -%}

SELECT 
    user_id,
    {%- for order_status in order_statuses %}
        COUNT(DISTINCT CASE WHEN status = '{{ order_status }}' THEN order_id END) 
        AS num_orders_{{ order_status }}
    {%- endfor %}
FROM {{ ref('stg_looker__orders') }}
GROUP BY 1
Case Study: Building E-Commerce Data Models with dbt

Congratulations!

Case Study: Building E-Commerce Data Models with dbt

Preparing Video For Download...