ELT warehouse with dbt, tested at every layer
Context
Raw e-commerce exports arrive as orders, order lines and products, which is enough to answer “what did we sell” and almost nothing else. Questions like average order value by category, or whether repeat customers spend more, need a grain that has been decided on deliberately and joins that hold. I built the warehouse that sits between the two: raw data in, a tested dimensional model out, and a dashboard on top of it.
Constraints
- Grain has to be decided before anything else. An order has several lines. One row per order and one row per order line give different revenue totals, and joining them without care double-counts.
- Raw exports only. Orders, order lines and products answer “what did we sell” and little else. Customer and date attributes have to be modelled.
- Joins that hold. A fact row that points to a missing product or customer silently drops out of a dashboard.
- Tests that fail the build. The model is only trusted if its checks stop the run when the data is wrong.
What I built
Python loads the raw order and product data into a PostgreSQL staging schema without transforming it. dbt does the rest: staging models clean and type the raw tables, and mart models build a star schema of two fact tables, fct_orders and fct_order_lines, against dim_customers, dim_products and dim_dates. Metabase reads only the marts and exposes revenue, average order value, revenue by category and period, order volume trends and customer frequency.
Tests run at every layer rather than only at the end: uniqueness on primary keys, not-null on required fields, and relationship tests that fail the build if a fact row points at a dimension key that does not exist. A referential integrity test is the cheapest way to find out that an export was incomplete, and it finds out before the dashboard quietly under-reports.
Design choices
Two fact tables. Order-level measures such as order value and order count live in fct_orders, and product-level measures live in fct_order_lines. A single line-grain fact would repeat order attributes on every line, and summing order-level values would then count them once per line.
ELT. Python lands the raw data in a staging schema as it arrives, and dbt does the cleaning and modelling in SQL. A change to a transform is then a model change and a re-run, not a re-ingestion, and the logic is versioned and tested alongside the rest of the models.
Metabase reads only the marts. The dashboard cannot depend on raw or staging tables, so what it shows is what the tests covered.
Next steps
- Incremental models for the largest fact table, so a run does not rebuild everything.
- History for dimensions, such as a changing product category, using snapshots instead of overwriting.
Stack
dbt, PostgreSQL, Python, Metabase, Docker Compose