
Loading...
Fact chonks create tables that capture business events and transactions, forming the core of your dimensional model. They contain the metrics your business cares about.
Fact tables store your measurable business events with foreign keys to dimensions for slicing and dicing.
The fact table SQL with measures and dimension keys
Foreign keys to all relevant dimensions
Common aggregations and boolean flags
One row per event. The most common type—captures discrete business events as they happen.
Examples: Orders, payments, clicks, logins, support tickets
One row per time period. Captures the state of something at regular intervals.
Examples: Daily inventory levels, monthly account balances, weekly active users
One row per process instance, updated as milestones occur. Tracks multi-step processes.
Examples: Order fulfillment pipeline, loan applications, support case lifecycle
Here's a transaction fact table for orders with dimension keys and pre-calculated measures.
with orders as (
select * from {{ ref('stg_ecommerce__orders') }}
),
order_items as (
select * from {{ ref('stg_ecommerce__order_items') }}
),
order_items_agg as (
select
order_id,
count(*) as item_count,
sum(quantity) as total_quantity,
sum(unit_price * quantity) as gross_amount
from order_items
group by 1
),
final as (
select
-- keys
orders.order_id,
orders.customer_id,
-- dimensions
orders.order_date,
orders.order_status,
-- measures
order_items_agg.item_count,
order_items_agg.total_quantity,
order_items_agg.gross_amount,
orders.discount_amount,
orders.order_total as net_amount,
-- flags
orders.discount_amount > 0 as has_discount,
orders.order_status = 'completed' as is_completed,
-- timestamps
orders.created_at,
orders.updated_at
from orders
left join order_items_agg using (order_id)
)
select * from finalGrain Matters
| Property | Type | Default | Description |
|---|---|---|---|
| fact_type | transaction | periodic | accumulating | transaction | Type of fact table to generate |
| grain | string | auto | Description of what one row represents |
| include_flags | boolean | true | Generate boolean flag columns |
| materialization | table | incremental | table | dbt materialization strategy |
| incremental_strategy | merge | delete+insert | merge | Strategy for incremental updates |
| unique_key | string | auto | Primary key for incremental models |