DBT Modeling Guide

This guide covers creating dimensional models using DBT, including fact tables, dimension tables, and star schema design for the AdventureWorks dataset.

Dimensional Model for Sale Processes

Overview

Fact Table
Dim Date
Dim Territory
Dim Product

fact_sale

x

x

x

Star Schema Design

@startuml
!theme plain

entity fact_sale {
  * sale_key : bigint <<PK>>
  --
  * date_key : int <<FK>>
  * territory_key : int <<FK>>
  * product_key : int <<FK>>
  --
  order_qty : decimal
  unit_price : decimal
  line_total : decimal
  sub_total : decimal
  tax_amt : decimal
  total_due : decimal
  sales_order_id : int
}

entity dim_date {
  * date_key : int <<PK>>
  --
  date : date
  year : int
  quarter : int
  month : int
  day : int
  day_of_week : int
  week_of_year : int
  is_weekend : boolean
  fiscal_year : int
  fiscal_quarter : int
}

entity dim_territory {
  * territory_key : int <<PK>>
  --
  territory_id : int
  territory_name : varchar(50)
  country_region_code : varchar(3)
  territory_group : varchar(50)
  region_name : varchar(50)
}

entity dim_product {
  * product_key : int <<PK>>
  --
  product_id : int
  product_name : varchar(50)
  product_number : varchar(25)
  product_category : varchar(50)
  product_subcategory : varchar(50)
  standard_cost : decimal
  list_price : decimal
  is_current : boolean
}

dim_date ||--o{ fact_sale
dim_territory ||--o{ fact_sale
dim_product ||--o{ fact_sale

@enduml

Building Dimensional Models

Fact Table Creation

Create models/marts/core/fact_sale.sql:

Dimension Tables

Date Dimension (models/marts/core/dim_date.sql):

Product Dimension (models/marts/core/dim_product.sql):

Testing and Documentation

Data Quality Tests

Create models/marts/schema.yml:

Macros and Utilities

Create helper macros in macros/ directory:

macros/get_date_key.sql:

Next Steps

Continue to Testing & Validation to ensure data quality.

Last updated