Build with AnnE, or from your own code. The MCP server, the REST API and GitHub - every job is also files you can review.

Developers · dbt projects

dbt projects, from the Models tab to your warehouse

A dbt project’s Models tab holds its warehouse models in plain words: raw copies of the source tables, cleaned and joined models, report tables - every column an Excel-style formula. The engine writes them as a dbt project for each of 8 warehouses. Pick a dbt project to see its models, the formulas they use, and every file dbt reads; how it runs and where it lives in Git is below.

Harbor Lane OutfittersDaniel Reyes, analytics, on Snowflake

Order revenue

Revenue by day and region, paid orders only, with every phone and email in one spelling.

It readsShopify orders and customers, loaded into the warehouse
The models, layer by layer

Raw copies models/staging/

  1. stg_orders viewShopify orders as they arrive, money in dollars.Reads source table shop.orders
  2. stg_customers viewOne spelling per email and phone.Reads source table shop.customers

Cleaned models/intermediate/

  1. orders_enriched viewPaid orders with their customer, region and size.Reads stg_orders, joined to stg_customers

Report tables models/marts/

  1. daily_revenue tableRevenue by day and region.Reads orders_enriched
As written on the Models tab: each column a formula
stg_orders
order_id=orders.id
customer_id=orders.customer_id
order_day=orders.order_day
total=RoundMoney(CentsToDollars(orders.total_cents))
state=StateCode(orders.ship_state)
channel=orders.channel
status=orders.financial_status
stg_customers
customer_id=customers.id
email=NormalizeEmail(customers.email)
phone=PhoneDigits(customers.phone)
full_name=FullName(customers.first_name, customers.last_name)
orders_enriched
order_id=stg_orders.order_id
order_day=stg_orders.order_day
region=SalesRegion(stg_orders.state)
order_size=OrderSize(stg_orders.total)
is_web=IsWebOrder(stg_orders.channel)
total=stg_orders.total
sales_tax=SalesTax(stg_orders.total, stg_orders.state)
customer_email=stg_customers.email
Keep rows where=IsPaid(stg_orders.status)
daily_revenue
order_day=orders_enriched.order_day
region=orders_enriched.region
orders=orders_enriched.order_id counted
customers=orders_enriched.customer_email counted, each once
revenue=orders_enriched.total summed
sales_tax=orders_enriched.sales_tax summed
The dbt project, as the engine writes it for each warehouse
Snowflake 7 files
dbt_project.yml
name: order_revenue
version: '1.0'
config-version: 2
profile: dai
# Written by DuctileAI from this project's models; edit the models, not these files.
models/staging/stg_customers.sql
-- One spelling per email and phone.
{{ config(materialized='view') }}

select
    customers.id as customer_id,
    (LOWER(TRIM((customers.email)))) as email,
    (REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM((customers.phone)), '(', ''), ')', ''), '-', ''), ' ', ''), '.', '')) as phone,
    (CONCAT(COALESCE(CAST(TRIM((customers.first_name)) AS VARCHAR), ''), ' ', COALESCE(CAST(TRIM((customers.last_name)) AS VARCHAR), ''))) as full_name
from {{ source('shop', 'customers') }} as customers
models/staging/stg_orders.sql
-- Shopify orders as they arrive, money in dollars.
{{ config(materialized='view') }}

select
    orders.id as order_id,
    orders.customer_id as customer_id,
    orders.order_day as order_day,
    (ROUND((((orders.total_cents) / 100)), 2)) as total,
    (UPPER(LEFT(TRIM((orders.ship_state)), 2))) as state,
    orders.channel as channel,
    orders.financial_status as status
from {{ source('shop', 'orders') }} as orders
models/intermediate/orders_enriched.sql
-- Paid orders with their customer, region and size.
{{ config(materialized='view') }}

select
    stg_orders.order_id as order_id,
    stg_orders.order_day as order_day,
    (CASE WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('CA'), LOWER('OR'), LOWER('WA'), LOWER('NV'), LOWER('AZ')) THEN 'West' WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('NY'), LOWER('NJ'), LOWER('MA'), LOWER('PA'), LOWER('CT')) THEN 'Northeast' WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('TX'), LOWER('OK'), LOWER('LA'), LOWER('AR')) THEN 'South Central' ELSE 'Central' END) as region,
    (CASE WHEN (stg_orders.total) >= 500 THEN 'Large' WHEN (stg_orders.total) >= 100 THEN 'Medium' ELSE 'Small' END) as order_size,
    (LOWER(TRIM((stg_orders.channel))) = 'web') as is_web,
    stg_orders.total as total,
    (ROUND((stg_orders.total) * CASE WHEN UPPER((stg_orders.state)) = 'CA' THEN 0.0725 WHEN UPPER((stg_orders.state)) = 'TX' THEN 0.0625 WHEN UPPER((stg_orders.state)) = 'NY' THEN 0.04 WHEN LOWER(UPPER((stg_orders.state))) IN (LOWER('OR'), LOWER('MT'), LOWER('NH'), LOWER('DE')) THEN 0 ELSE 0.05 END, 2)) as sales_tax,
    stg_customers.email as customer_email
from {{ ref('stg_orders') }} as stg_orders
left join {{ ref('stg_customers') }} as stg_customers on stg_orders.customer_id = stg_customers.customer_id
where (LOWER(LOWER(TRIM((stg_orders.status)))) IN (LOWER('paid'), LOWER('settled'), LOWER('captured')))
models/marts/daily_revenue.sql
-- Revenue by day and region.
{{ config(materialized='table') }}

select
    orders_enriched.order_day as order_day,
    orders_enriched.region as region,
    COUNT(orders_enriched.order_id) as orders,
    COUNT(DISTINCT orders_enriched.customer_email) as customers,
    SUM(orders_enriched.total) as revenue,
    SUM(orders_enriched.sales_tax) as sales_tax
from {{ ref('orders_enriched') }} as orders_enriched
group by orders_enriched.order_day, orders_enriched.region
models/sources.yml
version: 2
sources:
  - name: shop
    schema: shop
    tables:
      - name: customers
      - name: orders
macros/dai_formulas.sql
{# The project's reusable formulas as dbt macros, for snowflake.
   Written by DuctileAI for this run; edit the formulas, not this file. #}

{% macro CentsToDollars(cents) -%}
{{ cents }} / 100
{%- endmacro %}

{% macro FullName(first, last) -%}
CONCAT(COALESCE(CAST(TRIM({{ first }}) AS VARCHAR), ''), ' ', COALESCE(CAST(TRIM({{ last }}) AS VARCHAR), ''))
{%- endmacro %}

{% macro IsPaid(status) -%}
LOWER(LOWER(TRIM({{ status }}))) IN (LOWER('paid'), LOWER('settled'), LOWER('captured'))
{%- endmacro %}

{% macro IsWebOrder(channel) -%}
LOWER(TRIM({{ channel }})) = 'web'
{%- endmacro %}

{% macro NormalizeEmail(email) -%}
LOWER(TRIM({{ email }}))
{%- endmacro %}

{% macro OrderSize(total) -%}
CASE WHEN {{ total }} >= 500 THEN 'Large' WHEN {{ total }} >= 100 THEN 'Medium' ELSE 'Small' END
{%- endmacro %}

{% macro PhoneDigits(phone) -%}
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM({{ phone }}), '(', ''), ')', ''), '-', ''), ' ', ''), '.', '')
{%- endmacro %}

{% macro RoundMoney(amount) -%}
ROUND({{ amount }}, 2)
{%- endmacro %}

{% macro SalesRegion(state) -%}
CASE WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('CA'), LOWER('OR'), LOWER('WA'), LOWER('NV'), LOWER('AZ')) THEN 'West' WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('NY'), LOWER('NJ'), LOWER('MA'), LOWER('PA'), LOWER('CT')) THEN 'Northeast' WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('TX'), LOWER('OK'), LOWER('LA'), LOWER('AR')) THEN 'South Central' ELSE 'Central' END
{%- endmacro %}

{% macro SalesTax(amount, state) -%}
ROUND({{ amount }} * CASE WHEN UPPER({{ state }}) = 'CA' THEN 0.0725 WHEN UPPER({{ state }}) = 'TX' THEN 0.0625 WHEN UPPER({{ state }}) = 'NY' THEN 0.04 WHEN LOWER(UPPER({{ state }})) IN (LOWER('OR'), LOWER('MT'), LOWER('NH'), LOWER('DE')) THEN 0 ELSE 0.05 END, 2)
{%- endmacro %}

{% macro StateCode(state) -%}
UPPER(LEFT(TRIM({{ state }}), 2))
{%- endmacro %}
BigQuery 7 files
dbt_project.yml
name: order_revenue
version: '1.0'
config-version: 2
profile: dai
# Written by DuctileAI from this project's models; edit the models, not these files.
models/staging/stg_customers.sql
-- One spelling per email and phone.
{{ config(materialized='view') }}

select
    customers.id as customer_id,
    (LOWER(TRIM((customers.email)))) as email,
    (REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM((customers.phone)), '(', ''), ')', ''), '-', ''), ' ', ''), '.', '')) as phone,
    (CONCAT(COALESCE(CAST(TRIM((customers.first_name)) AS STRING), ''), ' ', COALESCE(CAST(TRIM((customers.last_name)) AS STRING), ''))) as full_name
from {{ source('shop', 'customers') }} as customers
models/staging/stg_orders.sql
-- Shopify orders as they arrive, money in dollars.
{{ config(materialized='view') }}

select
    orders.id as order_id,
    orders.customer_id as customer_id,
    orders.order_day as order_day,
    (ROUND(((CAST((orders.total_cents) AS BIGNUMERIC) / 100)), 2)) as total,
    (UPPER(LEFT(TRIM((orders.ship_state)), 2))) as state,
    orders.channel as channel,
    orders.financial_status as status
from {{ source('shop', 'orders') }} as orders
models/intermediate/orders_enriched.sql
-- Paid orders with their customer, region and size.
{{ config(materialized='view') }}

select
    stg_orders.order_id as order_id,
    stg_orders.order_day as order_day,
    (CASE WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('CA'), LOWER('OR'), LOWER('WA'), LOWER('NV'), LOWER('AZ')) THEN 'West' WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('NY'), LOWER('NJ'), LOWER('MA'), LOWER('PA'), LOWER('CT')) THEN 'Northeast' WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('TX'), LOWER('OK'), LOWER('LA'), LOWER('AR')) THEN 'South Central' ELSE 'Central' END) as region,
    (CASE WHEN (stg_orders.total) >= 500 THEN 'Large' WHEN (stg_orders.total) >= 100 THEN 'Medium' ELSE 'Small' END) as order_size,
    (LOWER(TRIM((stg_orders.channel))) = 'web') as is_web,
    stg_orders.total as total,
    (ROUND((stg_orders.total) * CASE WHEN UPPER((stg_orders.state)) = 'CA' THEN BIGNUMERIC '0.0725' WHEN UPPER((stg_orders.state)) = 'TX' THEN BIGNUMERIC '0.0625' WHEN UPPER((stg_orders.state)) = 'NY' THEN BIGNUMERIC '0.04' WHEN LOWER(UPPER((stg_orders.state))) IN (LOWER('OR'), LOWER('MT'), LOWER('NH'), LOWER('DE')) THEN 0 ELSE BIGNUMERIC '0.05' END, 2)) as sales_tax,
    stg_customers.email as customer_email
from {{ ref('stg_orders') }} as stg_orders
left join {{ ref('stg_customers') }} as stg_customers on stg_orders.customer_id = stg_customers.customer_id
where (LOWER(LOWER(TRIM((stg_orders.status)))) IN (LOWER('paid'), LOWER('settled'), LOWER('captured')))
models/marts/daily_revenue.sql
-- Revenue by day and region.
{{ config(materialized='table') }}

select
    orders_enriched.order_day as order_day,
    orders_enriched.region as region,
    COUNT(orders_enriched.order_id) as orders,
    COUNT(DISTINCT orders_enriched.customer_email) as customers,
    SUM(orders_enriched.total) as revenue,
    SUM(orders_enriched.sales_tax) as sales_tax
from {{ ref('orders_enriched') }} as orders_enriched
group by orders_enriched.order_day, orders_enriched.region
models/sources.yml
version: 2
sources:
  - name: shop
    schema: shop
    tables:
      - name: customers
      - name: orders
macros/dai_formulas.sql
{# The project's reusable formulas as dbt macros, for bigquery.
   Written by DuctileAI for this run; edit the formulas, not this file. #}

{% macro CentsToDollars(cents) -%}
CAST({{ cents }} AS BIGNUMERIC) / 100
{%- endmacro %}

{% macro FullName(first, last) -%}
CONCAT(COALESCE(CAST(TRIM({{ first }}) AS STRING), ''), ' ', COALESCE(CAST(TRIM({{ last }}) AS STRING), ''))
{%- endmacro %}

{% macro IsPaid(status) -%}
LOWER(LOWER(TRIM({{ status }}))) IN (LOWER('paid'), LOWER('settled'), LOWER('captured'))
{%- endmacro %}

{% macro IsWebOrder(channel) -%}
LOWER(TRIM({{ channel }})) = 'web'
{%- endmacro %}

{% macro NormalizeEmail(email) -%}
LOWER(TRIM({{ email }}))
{%- endmacro %}

{% macro OrderSize(total) -%}
CASE WHEN {{ total }} >= 500 THEN 'Large' WHEN {{ total }} >= 100 THEN 'Medium' ELSE 'Small' END
{%- endmacro %}

{% macro PhoneDigits(phone) -%}
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM({{ phone }}), '(', ''), ')', ''), '-', ''), ' ', ''), '.', '')
{%- endmacro %}

{% macro RoundMoney(amount) -%}
ROUND({{ amount }}, 2)
{%- endmacro %}

{% macro SalesRegion(state) -%}
CASE WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('CA'), LOWER('OR'), LOWER('WA'), LOWER('NV'), LOWER('AZ')) THEN 'West' WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('NY'), LOWER('NJ'), LOWER('MA'), LOWER('PA'), LOWER('CT')) THEN 'Northeast' WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('TX'), LOWER('OK'), LOWER('LA'), LOWER('AR')) THEN 'South Central' ELSE 'Central' END
{%- endmacro %}

{% macro SalesTax(amount, state) -%}
ROUND({{ amount }} * CASE WHEN UPPER({{ state }}) = 'CA' THEN BIGNUMERIC '0.0725' WHEN UPPER({{ state }}) = 'TX' THEN BIGNUMERIC '0.0625' WHEN UPPER({{ state }}) = 'NY' THEN BIGNUMERIC '0.04' WHEN LOWER(UPPER({{ state }})) IN (LOWER('OR'), LOWER('MT'), LOWER('NH'), LOWER('DE')) THEN 0 ELSE BIGNUMERIC '0.05' END, 2)
{%- endmacro %}

{% macro StateCode(state) -%}
UPPER(LEFT(TRIM({{ state }}), 2))
{%- endmacro %}
Databricks 7 files
dbt_project.yml
name: order_revenue
version: '1.0'
config-version: 2
profile: dai
# Written by DuctileAI from this project's models; edit the models, not these files.
models/staging/stg_customers.sql
-- One spelling per email and phone.
{{ config(materialized='view') }}

select
    customers.id as customer_id,
    (LOWER(TRIM((customers.email)))) as email,
    (REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM((customers.phone)), '(', ''), ')', ''), '-', ''), ' ', ''), '.', '')) as phone,
    (CONCAT(COALESCE(CAST(TRIM((customers.first_name)) AS STRING), ''), ' ', COALESCE(CAST(TRIM((customers.last_name)) AS STRING), ''))) as full_name
from {{ source('shop', 'customers') }} as customers
models/staging/stg_orders.sql
-- Shopify orders as they arrive, money in dollars.
{{ config(materialized='view') }}

select
    orders.id as order_id,
    orders.customer_id as customer_id,
    orders.order_day as order_day,
    (ROUND(((CAST((orders.total_cents) AS DECIMAL(38, 10)) / 100)), 2)) as total,
    (UPPER(LEFT(TRIM((orders.ship_state)), 2))) as state,
    orders.channel as channel,
    orders.financial_status as status
from {{ source('shop', 'orders') }} as orders
models/intermediate/orders_enriched.sql
-- Paid orders with their customer, region and size.
{{ config(materialized='view') }}

select
    stg_orders.order_id as order_id,
    stg_orders.order_day as order_day,
    (CASE WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('CA'), LOWER('OR'), LOWER('WA'), LOWER('NV'), LOWER('AZ')) THEN 'West' WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('NY'), LOWER('NJ'), LOWER('MA'), LOWER('PA'), LOWER('CT')) THEN 'Northeast' WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('TX'), LOWER('OK'), LOWER('LA'), LOWER('AR')) THEN 'South Central' ELSE 'Central' END) as region,
    (CASE WHEN (stg_orders.total) >= 500 THEN 'Large' WHEN (stg_orders.total) >= 100 THEN 'Medium' ELSE 'Small' END) as order_size,
    (LOWER(TRIM((stg_orders.channel))) = 'web') as is_web,
    stg_orders.total as total,
    (ROUND((stg_orders.total) * CASE WHEN UPPER((stg_orders.state)) = 'CA' THEN 0.0725 WHEN UPPER((stg_orders.state)) = 'TX' THEN 0.0625 WHEN UPPER((stg_orders.state)) = 'NY' THEN 0.04 WHEN LOWER(UPPER((stg_orders.state))) IN (LOWER('OR'), LOWER('MT'), LOWER('NH'), LOWER('DE')) THEN 0 ELSE 0.05 END, 2)) as sales_tax,
    stg_customers.email as customer_email
from {{ ref('stg_orders') }} as stg_orders
left join {{ ref('stg_customers') }} as stg_customers on stg_orders.customer_id = stg_customers.customer_id
where (LOWER(LOWER(TRIM((stg_orders.status)))) IN (LOWER('paid'), LOWER('settled'), LOWER('captured')))
models/marts/daily_revenue.sql
-- Revenue by day and region.
{{ config(materialized='table') }}

select
    orders_enriched.order_day as order_day,
    orders_enriched.region as region,
    COUNT(orders_enriched.order_id) as orders,
    COUNT(DISTINCT orders_enriched.customer_email) as customers,
    SUM(orders_enriched.total) as revenue,
    SUM(orders_enriched.sales_tax) as sales_tax
from {{ ref('orders_enriched') }} as orders_enriched
group by orders_enriched.order_day, orders_enriched.region
models/sources.yml
version: 2
sources:
  - name: shop
    schema: shop
    tables:
      - name: customers
      - name: orders
macros/dai_formulas.sql
{# The project's reusable formulas as dbt macros, for databricks.
   Written by DuctileAI for this run; edit the formulas, not this file. #}

{% macro CentsToDollars(cents) -%}
CAST({{ cents }} AS DECIMAL(38, 10)) / 100
{%- endmacro %}

{% macro FullName(first, last) -%}
CONCAT(COALESCE(CAST(TRIM({{ first }}) AS STRING), ''), ' ', COALESCE(CAST(TRIM({{ last }}) AS STRING), ''))
{%- endmacro %}

{% macro IsPaid(status) -%}
LOWER(LOWER(TRIM({{ status }}))) IN (LOWER('paid'), LOWER('settled'), LOWER('captured'))
{%- endmacro %}

{% macro IsWebOrder(channel) -%}
LOWER(TRIM({{ channel }})) = 'web'
{%- endmacro %}

{% macro NormalizeEmail(email) -%}
LOWER(TRIM({{ email }}))
{%- endmacro %}

{% macro OrderSize(total) -%}
CASE WHEN {{ total }} >= 500 THEN 'Large' WHEN {{ total }} >= 100 THEN 'Medium' ELSE 'Small' END
{%- endmacro %}

{% macro PhoneDigits(phone) -%}
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM({{ phone }}), '(', ''), ')', ''), '-', ''), ' ', ''), '.', '')
{%- endmacro %}

{% macro RoundMoney(amount) -%}
ROUND({{ amount }}, 2)
{%- endmacro %}

{% macro SalesRegion(state) -%}
CASE WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('CA'), LOWER('OR'), LOWER('WA'), LOWER('NV'), LOWER('AZ')) THEN 'West' WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('NY'), LOWER('NJ'), LOWER('MA'), LOWER('PA'), LOWER('CT')) THEN 'Northeast' WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('TX'), LOWER('OK'), LOWER('LA'), LOWER('AR')) THEN 'South Central' ELSE 'Central' END
{%- endmacro %}

{% macro SalesTax(amount, state) -%}
ROUND({{ amount }} * CASE WHEN UPPER({{ state }}) = 'CA' THEN 0.0725 WHEN UPPER({{ state }}) = 'TX' THEN 0.0625 WHEN UPPER({{ state }}) = 'NY' THEN 0.04 WHEN LOWER(UPPER({{ state }})) IN (LOWER('OR'), LOWER('MT'), LOWER('NH'), LOWER('DE')) THEN 0 ELSE 0.05 END, 2)
{%- endmacro %}

{% macro StateCode(state) -%}
UPPER(LEFT(TRIM({{ state }}), 2))
{%- endmacro %}
Redshift 7 files
dbt_project.yml
name: order_revenue
version: '1.0'
config-version: 2
profile: dai
# Written by DuctileAI from this project's models; edit the models, not these files.
models/staging/stg_customers.sql
-- One spelling per email and phone.
{{ config(materialized='view') }}

select
    customers.id as customer_id,
    (LOWER(TRIM((customers.email)))) as email,
    (REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM((customers.phone)), '(', ''), ')', ''), '-', ''), ' ', ''), '.', '')) as phone,
    ((COALESCE(CAST(TRIM((customers.first_name)) AS VARCHAR), '') || ' ' || COALESCE(CAST(TRIM((customers.last_name)) AS VARCHAR), ''))) as full_name
from {{ source('shop', 'customers') }} as customers
models/staging/stg_orders.sql
-- Shopify orders as they arrive, money in dollars.
{{ config(materialized='view') }}

select
    orders.id as order_id,
    orders.customer_id as customer_id,
    orders.order_day as order_day,
    (ROUND(((CAST((orders.total_cents) AS DECIMAL(38, 10)) / 100)), 2)) as total,
    (UPPER(LEFT(TRIM((orders.ship_state)), 2))) as state,
    orders.channel as channel,
    orders.financial_status as status
from {{ source('shop', 'orders') }} as orders
models/intermediate/orders_enriched.sql
-- Paid orders with their customer, region and size.
{{ config(materialized='view') }}

select
    stg_orders.order_id as order_id,
    stg_orders.order_day as order_day,
    (CASE WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('CA'), LOWER('OR'), LOWER('WA'), LOWER('NV'), LOWER('AZ')) THEN 'West' WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('NY'), LOWER('NJ'), LOWER('MA'), LOWER('PA'), LOWER('CT')) THEN 'Northeast' WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('TX'), LOWER('OK'), LOWER('LA'), LOWER('AR')) THEN 'South Central' ELSE 'Central' END) as region,
    (CASE WHEN (stg_orders.total) >= 500 THEN 'Large' WHEN (stg_orders.total) >= 100 THEN 'Medium' ELSE 'Small' END) as order_size,
    (LOWER(TRIM((stg_orders.channel))) = 'web') as is_web,
    stg_orders.total as total,
    (ROUND((stg_orders.total) * CASE WHEN UPPER((stg_orders.state)) = 'CA' THEN 0.0725 WHEN UPPER((stg_orders.state)) = 'TX' THEN 0.0625 WHEN UPPER((stg_orders.state)) = 'NY' THEN 0.04 WHEN LOWER(UPPER((stg_orders.state))) IN (LOWER('OR'), LOWER('MT'), LOWER('NH'), LOWER('DE')) THEN 0 ELSE 0.05 END, 2)) as sales_tax,
    stg_customers.email as customer_email
from {{ ref('stg_orders') }} as stg_orders
left join {{ ref('stg_customers') }} as stg_customers on stg_orders.customer_id = stg_customers.customer_id
where (LOWER(LOWER(TRIM((stg_orders.status)))) IN (LOWER('paid'), LOWER('settled'), LOWER('captured')))
models/marts/daily_revenue.sql
-- Revenue by day and region.
{{ config(materialized='table') }}

select
    orders_enriched.order_day as order_day,
    orders_enriched.region as region,
    COUNT(orders_enriched.order_id) as orders,
    COUNT(DISTINCT orders_enriched.customer_email) as customers,
    SUM(orders_enriched.total) as revenue,
    SUM(orders_enriched.sales_tax) as sales_tax
from {{ ref('orders_enriched') }} as orders_enriched
group by orders_enriched.order_day, orders_enriched.region
models/sources.yml
version: 2
sources:
  - name: shop
    schema: shop
    tables:
      - name: customers
      - name: orders
macros/dai_formulas.sql
{# The project's reusable formulas as dbt macros, for redshift.
   Written by DuctileAI for this run; edit the formulas, not this file. #}

{% macro CentsToDollars(cents) -%}
CAST({{ cents }} AS DECIMAL(38, 10)) / 100
{%- endmacro %}

{% macro FullName(first, last) -%}
(COALESCE(CAST(TRIM({{ first }}) AS VARCHAR), '') || ' ' || COALESCE(CAST(TRIM({{ last }}) AS VARCHAR), ''))
{%- endmacro %}

{% macro IsPaid(status) -%}
LOWER(LOWER(TRIM({{ status }}))) IN (LOWER('paid'), LOWER('settled'), LOWER('captured'))
{%- endmacro %}

{% macro IsWebOrder(channel) -%}
LOWER(TRIM({{ channel }})) = 'web'
{%- endmacro %}

{% macro NormalizeEmail(email) -%}
LOWER(TRIM({{ email }}))
{%- endmacro %}

{% macro OrderSize(total) -%}
CASE WHEN {{ total }} >= 500 THEN 'Large' WHEN {{ total }} >= 100 THEN 'Medium' ELSE 'Small' END
{%- endmacro %}

{% macro PhoneDigits(phone) -%}
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM({{ phone }}), '(', ''), ')', ''), '-', ''), ' ', ''), '.', '')
{%- endmacro %}

{% macro RoundMoney(amount) -%}
ROUND({{ amount }}, 2)
{%- endmacro %}

{% macro SalesRegion(state) -%}
CASE WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('CA'), LOWER('OR'), LOWER('WA'), LOWER('NV'), LOWER('AZ')) THEN 'West' WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('NY'), LOWER('NJ'), LOWER('MA'), LOWER('PA'), LOWER('CT')) THEN 'Northeast' WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('TX'), LOWER('OK'), LOWER('LA'), LOWER('AR')) THEN 'South Central' ELSE 'Central' END
{%- endmacro %}

{% macro SalesTax(amount, state) -%}
ROUND({{ amount }} * CASE WHEN UPPER({{ state }}) = 'CA' THEN 0.0725 WHEN UPPER({{ state }}) = 'TX' THEN 0.0625 WHEN UPPER({{ state }}) = 'NY' THEN 0.04 WHEN LOWER(UPPER({{ state }})) IN (LOWER('OR'), LOWER('MT'), LOWER('NH'), LOWER('DE')) THEN 0 ELSE 0.05 END, 2)
{%- endmacro %}

{% macro StateCode(state) -%}
UPPER(LEFT(TRIM({{ state }}), 2))
{%- endmacro %}
Postgres 7 files
dbt_project.yml
name: order_revenue
version: '1.0'
config-version: 2
profile: dai
# Written by DuctileAI from this project's models; edit the models, not these files.
models/staging/stg_customers.sql
-- One spelling per email and phone.
{{ config(materialized='view') }}

select
    customers.id as customer_id,
    (LOWER(TRIM((customers.email)))) as email,
    (REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM((customers.phone)), '(', ''), ')', ''), '-', ''), ' ', ''), '.', '')) as phone,
    (CONCAT(TRIM((customers.first_name)), ' ', TRIM((customers.last_name)))) as full_name
from {{ source('shop', 'customers') }} as customers
models/staging/stg_orders.sql
-- Shopify orders as they arrive, money in dollars.
{{ config(materialized='view') }}

select
    orders.id as order_id,
    orders.customer_id as customer_id,
    orders.order_day as order_day,
    (ROUND(CAST(((CAST((orders.total_cents) AS NUMERIC) / 100)) AS NUMERIC), 2)) as total,
    (UPPER(LEFT(TRIM((orders.ship_state)), 2))) as state,
    orders.channel as channel,
    orders.financial_status as status
from {{ source('shop', 'orders') }} as orders
models/intermediate/orders_enriched.sql
-- Paid orders with their customer, region and size.
{{ config(materialized='view') }}

select
    stg_orders.order_id as order_id,
    stg_orders.order_day as order_day,
    (CASE WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('CA'), LOWER('OR'), LOWER('WA'), LOWER('NV'), LOWER('AZ')) THEN 'West' WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('NY'), LOWER('NJ'), LOWER('MA'), LOWER('PA'), LOWER('CT')) THEN 'Northeast' WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('TX'), LOWER('OK'), LOWER('LA'), LOWER('AR')) THEN 'South Central' ELSE 'Central' END) as region,
    (CASE WHEN (stg_orders.total) >= 500 THEN 'Large' WHEN (stg_orders.total) >= 100 THEN 'Medium' ELSE 'Small' END) as order_size,
    (LOWER(TRIM((stg_orders.channel))) = 'web') as is_web,
    stg_orders.total as total,
    (ROUND(CAST((stg_orders.total) * CASE WHEN UPPER((stg_orders.state)) = 'CA' THEN 0.0725 WHEN UPPER((stg_orders.state)) = 'TX' THEN 0.0625 WHEN UPPER((stg_orders.state)) = 'NY' THEN 0.04 WHEN LOWER(UPPER((stg_orders.state))) IN (LOWER('OR'), LOWER('MT'), LOWER('NH'), LOWER('DE')) THEN 0 ELSE 0.05 END AS NUMERIC), 2)) as sales_tax,
    stg_customers.email as customer_email
from {{ ref('stg_orders') }} as stg_orders
left join {{ ref('stg_customers') }} as stg_customers on stg_orders.customer_id = stg_customers.customer_id
where (LOWER(LOWER(TRIM((stg_orders.status)))) IN (LOWER('paid'), LOWER('settled'), LOWER('captured')))
models/marts/daily_revenue.sql
-- Revenue by day and region.
{{ config(materialized='table') }}

select
    orders_enriched.order_day as order_day,
    orders_enriched.region as region,
    COUNT(orders_enriched.order_id) as orders,
    COUNT(DISTINCT orders_enriched.customer_email) as customers,
    SUM(orders_enriched.total) as revenue,
    SUM(orders_enriched.sales_tax) as sales_tax
from {{ ref('orders_enriched') }} as orders_enriched
group by orders_enriched.order_day, orders_enriched.region
models/sources.yml
version: 2
sources:
  - name: shop
    schema: shop
    tables:
      - name: customers
      - name: orders
macros/dai_formulas.sql
{# The project's reusable formulas as dbt macros, for postgres.
   Written by DuctileAI for this run; edit the formulas, not this file. #}

{% macro CentsToDollars(cents) -%}
CAST({{ cents }} AS NUMERIC) / 100
{%- endmacro %}

{% macro FullName(first, last) -%}
CONCAT(TRIM({{ first }}), ' ', TRIM({{ last }}))
{%- endmacro %}

{% macro IsPaid(status) -%}
LOWER(LOWER(TRIM({{ status }}))) IN (LOWER('paid'), LOWER('settled'), LOWER('captured'))
{%- endmacro %}

{% macro IsWebOrder(channel) -%}
LOWER(TRIM({{ channel }})) = 'web'
{%- endmacro %}

{% macro NormalizeEmail(email) -%}
LOWER(TRIM({{ email }}))
{%- endmacro %}

{% macro OrderSize(total) -%}
CASE WHEN {{ total }} >= 500 THEN 'Large' WHEN {{ total }} >= 100 THEN 'Medium' ELSE 'Small' END
{%- endmacro %}

{% macro PhoneDigits(phone) -%}
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM({{ phone }}), '(', ''), ')', ''), '-', ''), ' ', ''), '.', '')
{%- endmacro %}

{% macro RoundMoney(amount) -%}
ROUND(CAST({{ amount }} AS NUMERIC), 2)
{%- endmacro %}

{% macro SalesRegion(state) -%}
CASE WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('CA'), LOWER('OR'), LOWER('WA'), LOWER('NV'), LOWER('AZ')) THEN 'West' WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('NY'), LOWER('NJ'), LOWER('MA'), LOWER('PA'), LOWER('CT')) THEN 'Northeast' WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('TX'), LOWER('OK'), LOWER('LA'), LOWER('AR')) THEN 'South Central' ELSE 'Central' END
{%- endmacro %}

{% macro SalesTax(amount, state) -%}
ROUND(CAST({{ amount }} * CASE WHEN UPPER({{ state }}) = 'CA' THEN 0.0725 WHEN UPPER({{ state }}) = 'TX' THEN 0.0625 WHEN UPPER({{ state }}) = 'NY' THEN 0.04 WHEN LOWER(UPPER({{ state }})) IN (LOWER('OR'), LOWER('MT'), LOWER('NH'), LOWER('DE')) THEN 0 ELSE 0.05 END AS NUMERIC), 2)
{%- endmacro %}

{% macro StateCode(state) -%}
UPPER(LEFT(TRIM({{ state }}), 2))
{%- endmacro %}
SQL Server, Azure SQL, Synapse, Fabric 7 files
dbt_project.yml
name: order_revenue
version: '1.0'
config-version: 2
profile: dai
# Written by DuctileAI from this project's models; edit the models, not these files.
models/staging/stg_customers.sql
-- One spelling per email and phone.
{{ config(materialized='view') }}

select
    customers.id as customer_id,
    (LOWER(TRIM((customers.email)))) as email,
    (REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM((customers.phone)), '(', ''), ')', ''), '-', ''), ' ', ''), '.', '')) as phone,
    (CONCAT(TRIM((customers.first_name)), ' ', TRIM((customers.last_name)))) as full_name
from {{ source('shop', 'customers') }} as customers
models/staging/stg_orders.sql
-- Shopify orders as they arrive, money in dollars.
{{ config(materialized='view') }}

select
    orders.id as order_id,
    orders.customer_id as customer_id,
    orders.order_day as order_day,
    (ROUND(((CAST((orders.total_cents) AS DECIMAL(38, 10)) / 100)), 2)) as total,
    (UPPER(LEFT(TRIM((orders.ship_state)), 2))) as state,
    orders.channel as channel,
    orders.financial_status as status
from {{ source('shop', 'orders') }} as orders
models/intermediate/orders_enriched.sql
-- Paid orders with their customer, region and size.
{{ config(materialized='view') }}

select
    stg_orders.order_id as order_id,
    stg_orders.order_day as order_day,
    (CASE WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('CA'), LOWER('OR'), LOWER('WA'), LOWER('NV'), LOWER('AZ')) THEN 'West' WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('NY'), LOWER('NJ'), LOWER('MA'), LOWER('PA'), LOWER('CT')) THEN 'Northeast' WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('TX'), LOWER('OK'), LOWER('LA'), LOWER('AR')) THEN 'South Central' ELSE 'Central' END) as region,
    (CASE WHEN (stg_orders.total) >= 500 THEN 'Large' WHEN (stg_orders.total) >= 100 THEN 'Medium' ELSE 'Small' END) as order_size,
    (LOWER(TRIM((stg_orders.channel))) = 'web') as is_web,
    stg_orders.total as total,
    (ROUND((stg_orders.total) * CASE WHEN UPPER((stg_orders.state)) = 'CA' THEN 0.0725 WHEN UPPER((stg_orders.state)) = 'TX' THEN 0.0625 WHEN UPPER((stg_orders.state)) = 'NY' THEN 0.04 WHEN LOWER(UPPER((stg_orders.state))) IN (LOWER('OR'), LOWER('MT'), LOWER('NH'), LOWER('DE')) THEN 0 ELSE 0.05 END, 2)) as sales_tax,
    stg_customers.email as customer_email
from {{ ref('stg_orders') }} as stg_orders
left join {{ ref('stg_customers') }} as stg_customers on stg_orders.customer_id = stg_customers.customer_id
where (LOWER(LOWER(TRIM((stg_orders.status)))) IN (LOWER('paid'), LOWER('settled'), LOWER('captured')))
models/marts/daily_revenue.sql
-- Revenue by day and region.
{{ config(materialized='table') }}

select
    orders_enriched.order_day as order_day,
    orders_enriched.region as region,
    COUNT(orders_enriched.order_id) as orders,
    COUNT(DISTINCT orders_enriched.customer_email) as customers,
    SUM(orders_enriched.total) as revenue,
    SUM(orders_enriched.sales_tax) as sales_tax
from {{ ref('orders_enriched') }} as orders_enriched
group by orders_enriched.order_day, orders_enriched.region
models/sources.yml
version: 2
sources:
  - name: shop
    schema: shop
    tables:
      - name: customers
      - name: orders
macros/dai_formulas.sql
{# The project's reusable formulas as dbt macros, for tsql.
   Written by DuctileAI for this run; edit the formulas, not this file. #}

{% macro CentsToDollars(cents) -%}
CAST({{ cents }} AS DECIMAL(38, 10)) / 100
{%- endmacro %}

{% macro FullName(first, last) -%}
CONCAT(TRIM({{ first }}), ' ', TRIM({{ last }}))
{%- endmacro %}

{% macro IsPaid(status) -%}
LOWER(LOWER(TRIM({{ status }}))) IN (LOWER('paid'), LOWER('settled'), LOWER('captured'))
{%- endmacro %}

{% macro IsWebOrder(channel) -%}
LOWER(TRIM({{ channel }})) = 'web'
{%- endmacro %}

{% macro NormalizeEmail(email) -%}
LOWER(TRIM({{ email }}))
{%- endmacro %}

{% macro OrderSize(total) -%}
CASE WHEN {{ total }} >= 500 THEN 'Large' WHEN {{ total }} >= 100 THEN 'Medium' ELSE 'Small' END
{%- endmacro %}

{% macro PhoneDigits(phone) -%}
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM({{ phone }}), '(', ''), ')', ''), '-', ''), ' ', ''), '.', '')
{%- endmacro %}

{% macro RoundMoney(amount) -%}
ROUND({{ amount }}, 2)
{%- endmacro %}

{% macro SalesRegion(state) -%}
CASE WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('CA'), LOWER('OR'), LOWER('WA'), LOWER('NV'), LOWER('AZ')) THEN 'West' WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('NY'), LOWER('NJ'), LOWER('MA'), LOWER('PA'), LOWER('CT')) THEN 'Northeast' WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('TX'), LOWER('OK'), LOWER('LA'), LOWER('AR')) THEN 'South Central' ELSE 'Central' END
{%- endmacro %}

{% macro SalesTax(amount, state) -%}
ROUND({{ amount }} * CASE WHEN UPPER({{ state }}) = 'CA' THEN 0.0725 WHEN UPPER({{ state }}) = 'TX' THEN 0.0625 WHEN UPPER({{ state }}) = 'NY' THEN 0.04 WHEN LOWER(UPPER({{ state }})) IN (LOWER('OR'), LOWER('MT'), LOWER('NH'), LOWER('DE')) THEN 0 ELSE 0.05 END, 2)
{%- endmacro %}

{% macro StateCode(state) -%}
UPPER(LEFT(TRIM({{ state }}), 2))
{%- endmacro %}
Oracle 7 files
dbt_project.yml
name: order_revenue
version: '1.0'
config-version: 2
profile: dai
# Written by DuctileAI from this project's models; edit the models, not these files.
models/staging/stg_customers.sql
-- One spelling per email and phone.
{{ config(materialized='view') }}

select
    customers.id as customer_id,
    (LOWER(TRIM((customers.email)))) as email,
    (REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM((customers.phone)), '(', ''), ')', ''), '-', ''), ' ', ''), '.', '')) as phone,
    ((TRIM((customers.first_name)) || ' ' || TRIM((customers.last_name)))) as full_name
from {{ source('shop', 'customers') }} as customers
models/staging/stg_orders.sql
-- Shopify orders as they arrive, money in dollars.
{{ config(materialized='view') }}

select
    orders.id as order_id,
    orders.customer_id as customer_id,
    orders.order_day as order_day,
    (ROUND((((orders.total_cents) / 100)), 2)) as total,
    (UPPER(SUBSTR(TRIM((orders.ship_state)), 1, 2))) as state,
    orders.channel as channel,
    orders.financial_status as status
from {{ source('shop', 'orders') }} as orders
models/intermediate/orders_enriched.sql
-- Paid orders with their customer, region and size.
{{ config(materialized='view') }}

select
    stg_orders.order_id as order_id,
    stg_orders.order_day as order_day,
    (CASE WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('CA'), LOWER('OR'), LOWER('WA'), LOWER('NV'), LOWER('AZ')) THEN 'West' WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('NY'), LOWER('NJ'), LOWER('MA'), LOWER('PA'), LOWER('CT')) THEN 'Northeast' WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('TX'), LOWER('OK'), LOWER('LA'), LOWER('AR')) THEN 'South Central' ELSE 'Central' END) as region,
    (CASE WHEN (stg_orders.total) >= 500 THEN 'Large' WHEN (stg_orders.total) >= 100 THEN 'Medium' ELSE 'Small' END) as order_size,
    (LOWER(TRIM((stg_orders.channel))) = 'web') as is_web,
    stg_orders.total as total,
    (ROUND((stg_orders.total) * CASE WHEN UPPER((stg_orders.state)) = 'CA' THEN 0.0725 WHEN UPPER((stg_orders.state)) = 'TX' THEN 0.0625 WHEN UPPER((stg_orders.state)) = 'NY' THEN 0.04 WHEN LOWER(UPPER((stg_orders.state))) IN (LOWER('OR'), LOWER('MT'), LOWER('NH'), LOWER('DE')) THEN 0 ELSE 0.05 END, 2)) as sales_tax,
    stg_customers.email as customer_email
from {{ ref('stg_orders') }} as stg_orders
left join {{ ref('stg_customers') }} as stg_customers on stg_orders.customer_id = stg_customers.customer_id
where (LOWER(LOWER(TRIM((stg_orders.status)))) IN (LOWER('paid'), LOWER('settled'), LOWER('captured')))
models/marts/daily_revenue.sql
-- Revenue by day and region.
{{ config(materialized='table') }}

select
    orders_enriched.order_day as order_day,
    orders_enriched.region as region,
    COUNT(orders_enriched.order_id) as orders,
    COUNT(DISTINCT orders_enriched.customer_email) as customers,
    SUM(orders_enriched.total) as revenue,
    SUM(orders_enriched.sales_tax) as sales_tax
from {{ ref('orders_enriched') }} as orders_enriched
group by orders_enriched.order_day, orders_enriched.region
models/sources.yml
version: 2
sources:
  - name: shop
    schema: shop
    tables:
      - name: customers
      - name: orders
macros/dai_formulas.sql
{# The project's reusable formulas as dbt macros, for oracle.
   Written by DuctileAI for this run; edit the formulas, not this file. #}

{% macro CentsToDollars(cents) -%}
{{ cents }} / 100
{%- endmacro %}

{% macro FullName(first, last) -%}
(TRIM({{ first }}) || ' ' || TRIM({{ last }}))
{%- endmacro %}

{% macro IsPaid(status) -%}
LOWER(LOWER(TRIM({{ status }}))) IN (LOWER('paid'), LOWER('settled'), LOWER('captured'))
{%- endmacro %}

{% macro IsWebOrder(channel) -%}
LOWER(TRIM({{ channel }})) = 'web'
{%- endmacro %}

{% macro NormalizeEmail(email) -%}
LOWER(TRIM({{ email }}))
{%- endmacro %}

{% macro OrderSize(total) -%}
CASE WHEN {{ total }} >= 500 THEN 'Large' WHEN {{ total }} >= 100 THEN 'Medium' ELSE 'Small' END
{%- endmacro %}

{% macro PhoneDigits(phone) -%}
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM({{ phone }}), '(', ''), ')', ''), '-', ''), ' ', ''), '.', '')
{%- endmacro %}

{% macro RoundMoney(amount) -%}
ROUND({{ amount }}, 2)
{%- endmacro %}

{% macro SalesRegion(state) -%}
CASE WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('CA'), LOWER('OR'), LOWER('WA'), LOWER('NV'), LOWER('AZ')) THEN 'West' WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('NY'), LOWER('NJ'), LOWER('MA'), LOWER('PA'), LOWER('CT')) THEN 'Northeast' WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('TX'), LOWER('OK'), LOWER('LA'), LOWER('AR')) THEN 'South Central' ELSE 'Central' END
{%- endmacro %}

{% macro SalesTax(amount, state) -%}
ROUND({{ amount }} * CASE WHEN UPPER({{ state }}) = 'CA' THEN 0.0725 WHEN UPPER({{ state }}) = 'TX' THEN 0.0625 WHEN UPPER({{ state }}) = 'NY' THEN 0.04 WHEN LOWER(UPPER({{ state }})) IN (LOWER('OR'), LOWER('MT'), LOWER('NH'), LOWER('DE')) THEN 0 ELSE 0.05 END, 2)
{%- endmacro %}

{% macro StateCode(state) -%}
UPPER(SUBSTR(TRIM({{ state }}), 1, 2))
{%- endmacro %}
MySQL 7 files
dbt_project.yml
name: order_revenue
version: '1.0'
config-version: 2
profile: dai
# Written by DuctileAI from this project's models; edit the models, not these files.
models/staging/stg_customers.sql
-- One spelling per email and phone.
{{ config(materialized='view') }}

select
    customers.id as customer_id,
    (LOWER(TRIM((customers.email)))) as email,
    (REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM((customers.phone)), '(', ''), ')', ''), '-', ''), ' ', ''), '.', '')) as phone,
    (CONCAT(COALESCE(CAST(TRIM((customers.first_name)) AS CHAR), ''), ' ', COALESCE(CAST(TRIM((customers.last_name)) AS CHAR), ''))) as full_name
from {{ source('shop', 'customers') }} as customers
models/staging/stg_orders.sql
-- Shopify orders as they arrive, money in dollars.
{{ config(materialized='view') }}

select
    orders.id as order_id,
    orders.customer_id as customer_id,
    orders.order_day as order_day,
    (ROUND((((orders.total_cents) / 100)), 2)) as total,
    (UPPER(LEFT(TRIM((orders.ship_state)), 2))) as state,
    orders.channel as channel,
    orders.financial_status as status
from {{ source('shop', 'orders') }} as orders
models/intermediate/orders_enriched.sql
-- Paid orders with their customer, region and size.
{{ config(materialized='view') }}

select
    stg_orders.order_id as order_id,
    stg_orders.order_day as order_day,
    (CASE WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('CA'), LOWER('OR'), LOWER('WA'), LOWER('NV'), LOWER('AZ')) THEN 'West' WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('NY'), LOWER('NJ'), LOWER('MA'), LOWER('PA'), LOWER('CT')) THEN 'Northeast' WHEN LOWER(UPPER(TRIM((stg_orders.state)))) IN (LOWER('TX'), LOWER('OK'), LOWER('LA'), LOWER('AR')) THEN 'South Central' ELSE 'Central' END) as region,
    (CASE WHEN (stg_orders.total) >= 500 THEN 'Large' WHEN (stg_orders.total) >= 100 THEN 'Medium' ELSE 'Small' END) as order_size,
    (LOWER(TRIM((stg_orders.channel))) = 'web') as is_web,
    stg_orders.total as total,
    (ROUND((stg_orders.total) * CASE WHEN UPPER((stg_orders.state)) = 'CA' THEN 0.0725 WHEN UPPER((stg_orders.state)) = 'TX' THEN 0.0625 WHEN UPPER((stg_orders.state)) = 'NY' THEN 0.04 WHEN LOWER(UPPER((stg_orders.state))) IN (LOWER('OR'), LOWER('MT'), LOWER('NH'), LOWER('DE')) THEN 0 ELSE 0.05 END, 2)) as sales_tax,
    stg_customers.email as customer_email
from {{ ref('stg_orders') }} as stg_orders
left join {{ ref('stg_customers') }} as stg_customers on stg_orders.customer_id = stg_customers.customer_id
where (LOWER(LOWER(TRIM((stg_orders.status)))) IN (LOWER('paid'), LOWER('settled'), LOWER('captured')))
models/marts/daily_revenue.sql
-- Revenue by day and region.
{{ config(materialized='table') }}

select
    orders_enriched.order_day as order_day,
    orders_enriched.region as region,
    COUNT(orders_enriched.order_id) as orders,
    COUNT(DISTINCT orders_enriched.customer_email) as customers,
    SUM(orders_enriched.total) as revenue,
    SUM(orders_enriched.sales_tax) as sales_tax
from {{ ref('orders_enriched') }} as orders_enriched
group by orders_enriched.order_day, orders_enriched.region
models/sources.yml
version: 2
sources:
  - name: shop
    schema: shop
    tables:
      - name: customers
      - name: orders
macros/dai_formulas.sql
{# The project's reusable formulas as dbt macros, for mysql.
   Written by DuctileAI for this run; edit the formulas, not this file. #}

{% macro CentsToDollars(cents) -%}
{{ cents }} / 100
{%- endmacro %}

{% macro FullName(first, last) -%}
CONCAT(COALESCE(CAST(TRIM({{ first }}) AS CHAR), ''), ' ', COALESCE(CAST(TRIM({{ last }}) AS CHAR), ''))
{%- endmacro %}

{% macro IsPaid(status) -%}
LOWER(LOWER(TRIM({{ status }}))) IN (LOWER('paid'), LOWER('settled'), LOWER('captured'))
{%- endmacro %}

{% macro IsWebOrder(channel) -%}
LOWER(TRIM({{ channel }})) = 'web'
{%- endmacro %}

{% macro NormalizeEmail(email) -%}
LOWER(TRIM({{ email }}))
{%- endmacro %}

{% macro OrderSize(total) -%}
CASE WHEN {{ total }} >= 500 THEN 'Large' WHEN {{ total }} >= 100 THEN 'Medium' ELSE 'Small' END
{%- endmacro %}

{% macro PhoneDigits(phone) -%}
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM({{ phone }}), '(', ''), ')', ''), '-', ''), ' ', ''), '.', '')
{%- endmacro %}

{% macro RoundMoney(amount) -%}
ROUND({{ amount }}, 2)
{%- endmacro %}

{% macro SalesRegion(state) -%}
CASE WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('CA'), LOWER('OR'), LOWER('WA'), LOWER('NV'), LOWER('AZ')) THEN 'West' WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('NY'), LOWER('NJ'), LOWER('MA'), LOWER('PA'), LOWER('CT')) THEN 'Northeast' WHEN LOWER(UPPER(TRIM({{ state }}))) IN (LOWER('TX'), LOWER('OK'), LOWER('LA'), LOWER('AR')) THEN 'South Central' ELSE 'Central' END
{%- endmacro %}

{% macro SalesTax(amount, state) -%}
ROUND({{ amount }} * CASE WHEN UPPER({{ state }}) = 'CA' THEN 0.0725 WHEN UPPER({{ state }}) = 'TX' THEN 0.0625 WHEN UPPER({{ state }}) = 'NY' THEN 0.04 WHEN LOWER(UPPER({{ state }})) IN (LOWER('OR'), LOWER('MT'), LOWER('NH'), LOWER('DE')) THEN 0 ELSE 0.05 END, 2)
{%- endmacro %}

{% macro StateCode(state) -%}
UPPER(LEFT(TRIM({{ state }}), 2))
{%- endmacro %}

From the Models tab to a dbt run

  1. Start a dbt project. New job, Starts as: dbt project. It comes with a Run dbt process, and opens on its Models tab - or points at a dbt project you already keep in Git.
  2. Write the models in plain words. Each model says what it reads, how it joins, which rows it keeps and what each column is, as a formula. A raw copy reads a source table; the rest read models, never a layer above their own. The engine checks every model as you save it.
  3. The engine writes the dbt project. When the Run dbt process is saved, or a model changes, the models become dbt_project.yml, models/staging, intermediate and marts, and sources.yml, in the SQL of the step’s warehouse. Formulas are written into each model’s SQL, and the job’s formulas also travel as macros/dai_formulas.sql, for models you write by hand.
  4. Run dbt runs dbt Core. The step runs dbt build (or run, test, seed, snapshot) with dbt Core in a runner beside your pods, against Postgres, Snowflake, Databricks, BigQuery, Redshift, a Microsoft Fabric Warehouse, SQL Server and Azure SQL and Oracle. The connection is written from the job’s credential for that run only, then deleted. dbt’s own results come back: each model, test, seed and snapshot, and a failed test fails the step - or carries on, marked, if you choose. The run’s lineage is drawn on the Runs tab: sources to marts, each model marked built, failed or skipped, its tests beside it.
  5. It is in Git. With GitHub connected, every model save commits the dbt project to dbt/<space>/<job>/ on the Development branch - a real dbt project anyone with dbt can read, review and run. GitHub integration

Checked, not typed

  • Every file on this page is what the formula engine writes for that warehouse - the models, the sources and the macros. A model or a formula it refuses would fail this page’s build.
  • Each formula is a published macro, run live in every warehouse: the formulas as dbt macros.
  • Run dbt runs on Postgres, Snowflake, Databricks, BigQuery, Redshift, a Microsoft Fabric Warehouse, SQL Server and Azure SQL and Oracle. MySQL gets the same dbt project for your own dbt: its community adapter stops at dbt 1.7, so Run dbt does not carry it.

Already have a dbt project?

Point Run dbt at it instead: the repository’s https address, a branch or tag, and the folder it sits in. For a private repository, a read-only token from your vault - never typed into the step. GitHub, GitLab, Bitbucket and self-hosted Git all work. Packages are not downloaded during a run: commit dbt_packages/ with the project.