83 lines
2.3 KiB
Markdown
83 lines
2.3 KiB
Markdown
---
|
|
name: dbt_create_mart
|
|
description: "Step-by-step instructions for creating dbt Mart models (facts and dimensions) as thin wrappers over transform models. Use for any dbt + Snowflake project."
|
|
---
|
|
|
|
# Create a dbt Mart Model (Fact or Dimension)
|
|
|
|
Mart models are **thin wrappers** over transform models. They expose clean, BI-ready tables without duplicating business logic.
|
|
|
|
---
|
|
|
|
## Fact Model Steps
|
|
|
|
**1. Name the file**
|
|
- Pattern: `fct_<domain>_<metric>.sql`
|
|
- Example: `fct_sales_orders.sql`, `fct_ar_open_items.sql`
|
|
- Lives in `models/marts/facts/`
|
|
|
|
**2. Write a thin SELECT wrapper**
|
|
|
|
Option A — expose all columns (most common):
|
|
```sql
|
|
select {{ dbt_utils.star(ref('<trf_model_name>')) }}
|
|
from {{ ref('<trf_model_name>') }}
|
|
```
|
|
|
|
Option B — add a light filter or derived column:
|
|
```sql
|
|
select
|
|
*,
|
|
<derived_column> as <alias>
|
|
from {{ ref('<trf_model_name>') }}
|
|
where <filter_condition>
|
|
```
|
|
|
|
**3. Materialization**
|
|
- Default: `view` (inherited from `dbt_project.yml`).
|
|
- Override to `table` only if the BI tool performs poorly on views.
|
|
|
|
---
|
|
|
|
## Dimension Model Steps
|
|
|
|
**1. Name the file**
|
|
- Pattern: `dim_<entity>.sql`
|
|
- Example: `dim_customers.sql`, `dim_materials.sql`
|
|
- Lives in `models/marts/dim/`
|
|
|
|
**2. Write the SELECT**
|
|
- Dimensions are usually deduped/distinct entity lists from staging or transform.
|
|
```sql
|
|
select distinct
|
|
<natural_key> as <entity>_key,
|
|
<descriptive_field_1>,
|
|
<descriptive_field_2>
|
|
from {{ ref('<stg_or_trf_model>') }}
|
|
where <natural_key> is not null
|
|
```
|
|
|
|
**3. Add schema entry**
|
|
- File: `models/marts/schema.yml`
|
|
- Every fact: test `unique` + `not_null` on the primary key.
|
|
- Every dimension: test `unique` + `not_null` on the surrogate/natural key.
|
|
- Add `relationships` tests on foreign keys in fact models.
|
|
|
|
---
|
|
|
|
## Rules
|
|
|
|
| Rule | Detail |
|
|
|------|--------|
|
|
| No business logic | Mart = wrapper only; logic belongs in transform |
|
|
| Materialization | `view` by default; `table` only if needed for BI performance |
|
|
| Column naming | `snake_case` for marts; inherit from the transform model |
|
|
| `ref()` only | Always reference the transform model with `{{ ref() }}` |
|
|
| BI-facing names | Rename columns here if the BI layer needs friendlier names |
|
|
|
|
---
|
|
|
|
## Templates
|
|
|
|
See `mart_fct_template.sql` and `mart_dim_template.sql` in this folder.
|