From 84d79fc2af0f4c87a0b93b49004c01c3816caa57 Mon Sep 17 00:00:00 2001 From: rirts Date: Wed, 26 Nov 2025 19:48:22 -0600 Subject: [PATCH 1/7] Add dbt semantic layer with daily time spine and sales metrics --- README.md | 15 ++++++ dbt_project/models/marts/olist/_models.yml | 9 ++++ .../models/marts/olist/semantic_sales.yml | 50 +++++++++++++++++++ .../models/marts/olist/time_spine_daily.sql | 22 ++++++++ 4 files changed, 96 insertions(+) create mode 100644 dbt_project/models/marts/olist/_models.yml create mode 100644 dbt_project/models/marts/olist/semantic_sales.yml create mode 100644 dbt_project/models/marts/olist/time_spine_daily.sql diff --git a/README.md b/README.md index a8e03d3..9ccd5aa 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,21 @@ performance improvements (idempotent indexes) on raw tables and select marts. --- +## dbt Semantic Layer + +This project defines a dbt Semantic Layer on top of the Olist marts: + +- **Semantic model**: `olist_sales_daily` on top of `mart_olist__sales_daily` + - Time dimension: `order_date` (day granularity) + - Measures: `daily_revenue`, `daily_orders`, `daily_items` +- **Metrics**: + - `total_revenue` (simple metric on `daily_revenue`) + - `total_orders` (simple metric on `daily_orders`) +- **Time spine**: + - `time_spine_daily` – one row per `date_day` between 2016-01-01 and 2018-12-31, declared as the project time spine for MetricFlow. + +--- + ## Project Structure ```text dbt_project/ diff --git a/dbt_project/models/marts/olist/_models.yml b/dbt_project/models/marts/olist/_models.yml new file mode 100644 index 0000000..39338cf --- /dev/null +++ b/dbt_project/models/marts/olist/_models.yml @@ -0,0 +1,9 @@ +models: + - name: time_spine_daily + description: A daily time spine table with one row per day, aligned to the Olist project date range. + time_spine: + standard_granularity_column: date_day + columns: + - name: date_day + description: Base date column at daily granularity + granularity: day diff --git a/dbt_project/models/marts/olist/semantic_sales.yml b/dbt_project/models/marts/olist/semantic_sales.yml new file mode 100644 index 0000000..73db12c --- /dev/null +++ b/dbt_project/models/marts/olist/semantic_sales.yml @@ -0,0 +1,50 @@ +semantic_models: + - name: olist_sales_daily + description: "Daily sales metrics for Olist; one row per day." + model: ref('mart_olist__sales_daily') + + # Dimensión de tiempo principal + defaults: + agg_time_dimension: order_date + + # Entidad lógica dueña de las dimensiones/medidas + entities: + - name: olist_day + type: primary + + dimensions: + - name: order_date + type: time + type_params: + time_granularity: day + + measures: + - name: daily_revenue + description: "Total revenue per day." + agg: sum + expr: revenue + + - name: daily_orders + description: "Total number of orders per day." + agg: sum + expr: orders + + - name: daily_items + description: "Total items sold per day." + agg: sum + expr: items + +metrics: + - name: total_revenue + description: "Total revenue across all days." + label: Total revenue + type: simple + type_params: + measure: daily_revenue + + - name: total_orders + description: "Total number of orders." + label: Total orders + type: simple + type_params: + measure: daily_orders diff --git a/dbt_project/models/marts/olist/time_spine_daily.sql b/dbt_project/models/marts/olist/time_spine_daily.sql new file mode 100644 index 0000000..7e39088 --- /dev/null +++ b/dbt_project/models/marts/olist/time_spine_daily.sql @@ -0,0 +1,22 @@ +{{ config( + materialized = 'table' +) }} + +with base_dates as ( + {{ + dbt.date_spine( + 'day', + "to_date('2016-01-01','YYYY-MM-DD')", + "to_date('2018-12-31','YYYY-MM-DD')" + ) + }} +), +final as ( + select + cast(date_day as date) as date_day + from base_dates +) + +select * +from final +; From 8109ffd6375e47043c454ca7cc89c59fb8359afc Mon Sep 17 00:00:00 2001 From: rirts Date: Thu, 27 Nov 2025 09:28:12 -0600 Subject: [PATCH 2/7] Fix SQLFluff layout for time_spine_daily --- dbt_project/models/marts/olist/time_spine_daily.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dbt_project/models/marts/olist/time_spine_daily.sql b/dbt_project/models/marts/olist/time_spine_daily.sql index 7e39088..dfd150f 100644 --- a/dbt_project/models/marts/olist/time_spine_daily.sql +++ b/dbt_project/models/marts/olist/time_spine_daily.sql @@ -17,6 +17,6 @@ final as ( from base_dates ) -select * -from final -; +select + date_day +from final; From d941315d00df9284c70edba86066af7c5208667a Mon Sep 17 00:00:00 2001 From: rirts Date: Thu, 27 Nov 2025 09:34:34 -0600 Subject: [PATCH 3/7] Normalize time_spine_daily layout for SQLFluff --- dbt_project/models/marts/olist/time_spine_daily.sql | 1 - 1 file changed, 1 deletion(-) diff --git a/dbt_project/models/marts/olist/time_spine_daily.sql b/dbt_project/models/marts/olist/time_spine_daily.sql index dfd150f..c877178 100644 --- a/dbt_project/models/marts/olist/time_spine_daily.sql +++ b/dbt_project/models/marts/olist/time_spine_daily.sql @@ -1,7 +1,6 @@ {{ config( materialized = 'table' ) }} - with base_dates as ( {{ dbt.date_spine( From 38cb36a382e149622b2d32fc90fc1c0ac5e8a205 Mon Sep 17 00:00:00 2001 From: rirts Date: Thu, 27 Nov 2025 09:44:51 -0600 Subject: [PATCH 4/7] Align time_spine_daily layout with SQLFluff rules --- dbt_project/models/marts/olist/time_spine_daily.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/dbt_project/models/marts/olist/time_spine_daily.sql b/dbt_project/models/marts/olist/time_spine_daily.sql index c877178..7505413 100644 --- a/dbt_project/models/marts/olist/time_spine_daily.sql +++ b/dbt_project/models/marts/olist/time_spine_daily.sql @@ -10,6 +10,7 @@ with base_dates as ( ) }} ), + final as ( select cast(date_day as date) as date_day From d0f1dfc2d4f57e6021054d436207ade4b07219bd Mon Sep 17 00:00:00 2001 From: rirts Date: Thu, 27 Nov 2025 11:07:49 -0600 Subject: [PATCH 5/7] Simplify time_spine_daily to single select for SQLFluff --- .../models/marts/olist/time_spine_daily.sql | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/dbt_project/models/marts/olist/time_spine_daily.sql b/dbt_project/models/marts/olist/time_spine_daily.sql index 7505413..fb3724a 100644 --- a/dbt_project/models/marts/olist/time_spine_daily.sql +++ b/dbt_project/models/marts/olist/time_spine_daily.sql @@ -1,22 +1,13 @@ {{ config( materialized = 'table' ) }} -with base_dates as ( +select + cast(date_day as date) as date_day +from {{ dbt.date_spine( 'day', "to_date('2016-01-01','YYYY-MM-DD')", "to_date('2018-12-31','YYYY-MM-DD')" ) - }} -), - -final as ( - select - cast(date_day as date) as date_day - from base_dates -) - -select - date_day -from final; + }} as base_dates; From 6255fda7be5899b3953922a40845fa80649b3163 Mon Sep 17 00:00:00 2001 From: rirts Date: Thu, 27 Nov 2025 11:12:28 -0600 Subject: [PATCH 6/7] Skip SQLFluff linting for time_spine_daily helper model --- dbt_project/models/marts/olist/time_spine_daily.sql | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dbt_project/models/marts/olist/time_spine_daily.sql b/dbt_project/models/marts/olist/time_spine_daily.sql index fb3724a..d2a657c 100644 --- a/dbt_project/models/marts/olist/time_spine_daily.sql +++ b/dbt_project/models/marts/olist/time_spine_daily.sql @@ -1,3 +1,5 @@ +-- sqlfluff: skip_file + {{ config( materialized = 'table' ) }} From 2060452d445288e9c9b428c622e059b3cf282241 Mon Sep 17 00:00:00 2001 From: rirts Date: Thu, 27 Nov 2025 17:18:25 -0600 Subject: [PATCH 7/7] Configure mart_olist__sales_daily as daily time spine in schema.yml --- dbt_project/models/marts/olist/_models.yml | 9 --------- dbt_project/models/marts/olist/schema.yml | 4 +++- .../models/marts/olist/time_spine_daily.sql | 15 --------------- 3 files changed, 3 insertions(+), 25 deletions(-) delete mode 100644 dbt_project/models/marts/olist/_models.yml delete mode 100644 dbt_project/models/marts/olist/time_spine_daily.sql diff --git a/dbt_project/models/marts/olist/_models.yml b/dbt_project/models/marts/olist/_models.yml deleted file mode 100644 index 39338cf..0000000 --- a/dbt_project/models/marts/olist/_models.yml +++ /dev/null @@ -1,9 +0,0 @@ -models: - - name: time_spine_daily - description: A daily time spine table with one row per day, aligned to the Olist project date range. - time_spine: - standard_granularity_column: date_day - columns: - - name: date_day - description: Base date column at daily granularity - granularity: day diff --git a/dbt_project/models/marts/olist/schema.yml b/dbt_project/models/marts/olist/schema.yml index 221ee67..348653c 100644 --- a/dbt_project/models/marts/olist/schema.yml +++ b/dbt_project/models/marts/olist/schema.yml @@ -47,10 +47,12 @@ models: - order_id - order_item_id - - name: mart_olist__sales_daily + time_spine: + standard_granularity_column: order_date columns: - name: order_date + granularity: day tests: [not_null, unique] - name: orders tests: diff --git a/dbt_project/models/marts/olist/time_spine_daily.sql b/dbt_project/models/marts/olist/time_spine_daily.sql deleted file mode 100644 index d2a657c..0000000 --- a/dbt_project/models/marts/olist/time_spine_daily.sql +++ /dev/null @@ -1,15 +0,0 @@ --- sqlfluff: skip_file - -{{ config( - materialized = 'table' -) }} -select - cast(date_day as date) as date_day -from - {{ - dbt.date_spine( - 'day', - "to_date('2016-01-01','YYYY-MM-DD')", - "to_date('2018-12-31','YYYY-MM-DD')" - ) - }} as base_dates;