Skip to content

Commit f0ec894

Browse files
committed
Support dbt and metrics v1.3.0+
1 parent 02eed7c commit f0ec894

14 files changed

+302
-49
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
# dbt - Dimensional Modeling, Semantic Layer and Business Intelligence
1+
# dbt - Dimensional Modeling, Semantic Layer, Metrics and Business Intelligence
22

33
This dbt project is database agnostic, requires no data sources, and focuses on the following concepts:
44
1. **Dimensional Modeling** - dbt data transformations that highlight multi-dimensional (star schema) modeling concepts using the Kimball methodology
55
2. **Semantic Layer** - leverage dbt as a semantic layer to define metrics, dimensions, aggregations, calculations, data relationships, business-friendly names and descriptions, synonyms, formatting and other data catalog attributes
66
3. **Business Intelligence** - connect Business Intelligence tools to the dbt semantic layer for analysis, focusing on standards for ["Semantic-free" BI](https://towardsdatascience.com/semantic-free-is-the-future-of-business-intelligence-27aae1d11563) integration with dbt that enables BI tools to "speak the same language"
77

8-
> Tested on both **dbt Core** and **dbt Cloud** versions 1.0+. Database agnostic and has been tested with the **Postgres**, **BigQuery**, **Snowflake**, and **Redshift** [adapters](https://docs.getdbt.com/docs/available-adapters).
8+
> Tested on both **dbt Core** and **dbt Cloud** versions 1.0+. Database agnostic and has been tested with the **Postgres**, **BigQuery**, **Snowflake**, **SQL Server**, and **Redshift** [adapters](https://docs.getdbt.com/docs/available-adapters).
9+
10+
> **dbt 1.3+** - this project requires dbt and dbt_metrics versions 1.3+. If you'd like to try with dbt < 1.3, then use previous releases of this project.
911
1012
## Create dbt Project
1113

@@ -54,7 +56,7 @@ To run this project (assuming you have dbt installed):
5456

5557
3. Give the project a name, e.g. "Sales Project"
5658

57-
4. [Create a connection](https://docs.getdbt.com/docs/dbt-cloud/cloud-quickstart#create-a-connection) to either Snowflake, BigQuery, PostgreSQL or Redshift
59+
4. [Create a connection](https://docs.getdbt.com/docs/dbt-cloud/cloud-quickstart#create-a-connection) to either Snowflake, BigQuery, PostgreSQL, SQL Server, or Redshift
5860

5961
5. [Connect a repository](https://docs.getdbt.com/docs/dbt-cloud/cloud-quickstart#connect-a-repository) with either **Git Clone** or **Github**
6062

dbt_project.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
# and underscores. A good package name should reflect your organization's
44
# name or the intended use of these models
55
name: 'dbt_business_intelligence'
6-
version: '1.0.0'
6+
version: '1.3.0'
77
config-version: 2
88

9+
require-dbt-version: ">=1.3.0"
10+
911
# This setting configures which "profile" dbt uses for this project.
1012
profile: 'dbt_business_intelligence'
1113

@@ -43,6 +45,8 @@ models:
4345
staging:
4446
+schema: staging
4547
+materialized: view
48+
metrics:
49+
+enabled: "{{ false if target.type == 'sqlserver' else true }}" # sqlserver does not allow nested CTE's, which metrics require
4650
metrics:
4751
dbt_metrics_default_calendar:
4852
+schema: staging

macros/bigquery__create_constraints.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@
5050
{% macro bigquery__truncate_relation(relation) -%}
5151
{#- TODO -#}
5252
{% endmacro %}
53+
54+
{% macro bigquery__create_not_null() -%}
55+
{#- TODO -#}
56+
{% endmacro %}

macros/postgres_create_index.sql

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
{% macro sqlserver__date_part(datepart, date) -%}
2+
3+
{%- if datepart == "dayofweek" -%}
4+
datepart = "WEEKDAY"
5+
{%- elif datepart == "isoweek" -%}
6+
datepart = "ISO_WEEK"
7+
{%- endif -%}
8+
9+
datepart({{ datepart }}, {{ date }})
10+
{%- endmacro %}
11+
12+
{%- macro sqlserver__week_end(date) -%}
13+
{%- set dt = dbt_date.week_start(date) -%}
14+
{{ dbt_date.n_days_away(6, dt) }}
15+
{%- endmacro %}
16+
17+
{% macro sqlserver__date_spine_sql(datepart, start_date, end_date) %}
18+
19+
20+
with
21+
22+
l0 as (
23+
24+
select c
25+
from (select 1 union all select 1) as d(c)
26+
27+
),
28+
l1 as (
29+
30+
select
31+
1 as c
32+
from l0 as a
33+
cross join l0 as b
34+
35+
),
36+
37+
l2 as (
38+
39+
select 1 as c
40+
from l1 as a
41+
cross join l1 as b
42+
),
43+
44+
l3 as (
45+
46+
select 1 as c
47+
from l2 as a
48+
cross join l2 as b
49+
),
50+
51+
l4 as (
52+
53+
select 1 as c
54+
from l3 as a
55+
cross join l3 as b
56+
),
57+
58+
l5 as (
59+
60+
select 1 as c
61+
from l4 as a
62+
cross join l4 as b
63+
),
64+
65+
nums as (
66+
67+
select row_number() over (order by (select null)) as rownum
68+
from l5
69+
),
70+
71+
rawdata as (
72+
73+
select top ({{datediff(start_date, end_date, datepart)}}) rownum -1 as n
74+
from nums
75+
order by rownum
76+
),
77+
78+
all_periods as (
79+
80+
select (
81+
{{
82+
dateadd(
83+
datepart,
84+
'n',
85+
start_date
86+
)
87+
}}
88+
) as date_{{datepart}}
89+
from rawdata
90+
),
91+
92+
filtered as (
93+
94+
select *
95+
from all_periods
96+
where date_{{datepart}} <= {{ end_date }}
97+
98+
)
99+
100+
select * from filtered
101+
102+
{% endmacro %}
103+
104+
105+
{% macro sqlserver__date_spine(datepart, start_date, end_date) -%}
106+
107+
{% set date_spine_query %}
108+
109+
{{tsql_utils.sqlserver__date_spine_sql(datepart, start_date, end_date)}} order by 1
110+
111+
{% endset %}
112+
113+
114+
{% set results = run_query(date_spine_query) %}
115+
116+
{% if execute %}
117+
118+
{% set results_list = results.columns[0].values() %}
119+
120+
{% else %}
121+
122+
{% set results_list = [] %}
123+
124+
{% endif %}
125+
126+
{%- for date_field in results_list %}
127+
select '{{ date_field }}' as date_{{datepart}} {{ 'union all ' if not loop.last else '' }}
128+
{% endfor -%}
129+
130+
{% endmacro %}
131+
132+
{% macro sqlserver__get_date_dimension(start_date, end_date) %}
133+
134+
{%- set start_date="cast('" ~ start_date ~ "' as " ~ type_timestamp() ~ ")" -%}
135+
{%- set end_date="cast('" ~ end_date ~ "' as " ~ type_timestamp() ~ ")" -%}
136+
137+
with base_dates as (
138+
139+
{{ dbt_utils.date_spine(
140+
datepart="day",
141+
start_date=start_date,
142+
end_date=end_date
143+
)
144+
}}
145+
146+
),
147+
dates_with_prior_year_dates as (
148+
149+
select
150+
cast(d.date_day as date) as date_day,
151+
cast({{ dateadd('year', -1 , 'd.date_day') }} as date) as prior_year_date_day,
152+
cast({{ dateadd('day', -364 , 'd.date_day') }} as date) as prior_year_over_year_date_day
153+
from
154+
base_dates d
155+
156+
)
157+
select
158+
d.date_day,
159+
{{ dbt_date.yesterday('d.date_day') }} as prior_date_day,
160+
{{ dbt_date.tomorrow('d.date_day') }} as next_date_day,
161+
d.prior_year_date_day as prior_year_date_day,
162+
d.prior_year_over_year_date_day,
163+
164+
DATEPART(WEEKDAY, d.date_day) as day_of_week,
165+
DATEPART(WEEKDAY, d.date_day) as day_of_week_iso,
166+
DATENAME(WEEKDAY, d.date_day) as day_of_week_name,
167+
LEFT(DATENAME(WEEKDAY, d.date_day), 3) as day_of_week_name_short,
168+
/*
169+
{{ dbt_date.day_of_week('d.date_day', isoweek=false) }} as day_of_week,
170+
{{ dbt_date.day_of_week('d.date_day', isoweek=true) }} as day_of_week_iso,
171+
{{ dbt_date.day_name('d.date_day', short=false) }} as day_of_week_name,
172+
{{ dbt_date.day_name('d.date_day', short=true) }} as day_of_week_name_short,
173+
*/
174+
{{ dbt_date.day_of_month('d.date_day') }} as day_of_month,
175+
{{ dbt_date.day_of_year('d.date_day') }} as day_of_year,
176+
177+
{{ dbt_date.week_start('d.date_day') }} as week_start_date,
178+
{{ dbt_date.week_end('d.date_day') }} as week_end_date,
179+
{{ dbt_date.week_start('d.prior_year_over_year_date_day') }} as prior_year_week_start_date,
180+
{{ dbt_date.week_end('d.prior_year_over_year_date_day') }} as prior_year_week_end_date,
181+
{{ dbt_date.week_of_year('d.date_day') }} as week_of_year,
182+
183+
/*
184+
{{ dbt_date.iso_week_start('d.date_day') }} as iso_week_start_date,
185+
{{ dbt_date.iso_week_end('d.date_day') }} as iso_week_end_date,
186+
{{ dbt_date.iso_week_start('d.prior_year_over_year_date_day') }} as prior_year_iso_week_start_date,
187+
{{ dbt_date.iso_week_end('d.prior_year_over_year_date_day') }} as prior_year_iso_week_end_date,
188+
{{ dbt_date.iso_week_of_year('d.date_day') }} as iso_week_of_year,
189+
*/
190+
191+
{{ dbt_date.week_of_year('d.prior_year_over_year_date_day') }} as prior_year_week_of_year,
192+
/*
193+
{{ dbt_date.iso_week_of_year('d.prior_year_over_year_date_day') }} as prior_year_iso_week_of_year,
194+
*/
195+
cast({{ dbt_date.date_part('month', 'd.date_day') }} as {{ type_int() }}) as month_of_year,
196+
197+
DATENAME(MONTH, d.date_day) as month_name,
198+
LEFT(DATENAME(MONTH, d.date_day), 3) as month_name_short,
199+
/*
200+
{{ dbt_date.month_name('d.date_day', short=false) }} as month_name,
201+
{{ dbt_date.month_name('d.date_day', short=true) }} as month_name_short,
202+
*/
203+
204+
cast({{ date_trunc('month', 'd.date_day') }} as date) as month_start_date,
205+
cast({{ last_day('d.date_day', 'month') }} as date) as month_end_date,
206+
207+
cast({{ date_trunc('month', 'd.prior_year_date_day') }} as date) as prior_year_month_start_date,
208+
cast({{ last_day('d.prior_year_date_day', 'month') }} as date) as prior_year_month_end_date,
209+
210+
cast({{ dbt_date.date_part('quarter', 'd.date_day') }} as {{ type_int() }}) as quarter_of_year,
211+
cast({{ date_trunc('quarter', 'd.date_day') }} as date) as quarter_start_date,
212+
cast({{ last_day('d.date_day', 'quarter') }} as date) as quarter_end_date,
213+
214+
cast({{ dbt_date.date_part('year', 'd.date_day') }} as {{ type_int() }}) as year_number,
215+
cast({{ date_trunc('year', 'd.date_day') }} as date) as year_start_date,
216+
cast({{ last_day('d.date_day', 'year') }} as date) as year_end_date
217+
from
218+
dates_with_prior_year_dates d
219+
220+
{% endmacro %}

macros/typehelper.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% macro redshift__type_string() %}
2+
varchar(256)
3+
{% endmacro %}
4+
5+
{% macro redshift__type_json() %}
6+
varchar(max)
7+
{% endmacro %}
8+
9+
{% macro redshift__type_array() %}
10+
varchar(max)
11+
{% endmacro %}

0 commit comments

Comments
 (0)