Skip to content

Commit 8da3994

Browse files
danyelfclaude
andcommitted
Add 15 metrics and reporting models
10 metric models (daily/weekly/monthly revenue, orders, customer acquisition/retention, product sales, store, promotion, inventory) and 5 report dashboard models (executive, sales, customer, product, store) for analytics layer. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 49577cc commit 8da3994

15 files changed

Lines changed: 342 additions & 0 deletions
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
with cohorts as (
2+
select * from {{ ref('customer_cohorts') }}
3+
),
4+
5+
final as (
6+
select
7+
cohort_month,
8+
cohort_size as new_customers,
9+
avg_lifetime_orders,
10+
avg_tenure_days,
11+
sum(cohort_size) over (order by cohort_month) as cumulative_customers
12+
from cohorts
13+
)
14+
15+
select * from final
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
with retention as (
2+
select * from {{ ref('customer_retention') }}
3+
),
4+
5+
cohort_sizes as (
6+
select
7+
cohort_month,
8+
customers as cohort_size
9+
from retention
10+
where months_since_first = 0
11+
),
12+
13+
rates as (
14+
select
15+
r.cohort_month,
16+
r.months_since_first,
17+
r.customers as retained_customers,
18+
cs.cohort_size,
19+
round(cast(r.customers as decimal) / cs.cohort_size * 100, 1) as retention_rate
20+
from retention r
21+
inner join cohort_sizes cs on r.cohort_month = cs.cohort_month
22+
)
23+
24+
select * from rates
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
with orders as (
2+
select * from {{ ref('int_order_enriched') }}
3+
),
4+
5+
daily as (
6+
select
7+
order_date,
8+
count(*) as total_orders,
9+
sum(case when status in ('completed', 'Scompleted') then 1 else 0 end) as completed_orders,
10+
sum(case when status in ('returned', 'Sreturned', 'return_pending', 'Sreturn_pending') then 1 else 0 end) as returned_orders,
11+
sum(case when status in ('shipped', 'Sshipped') then 1 else 0 end) as shipped_orders,
12+
sum(case when status in ('placed', 'Splaced') then 1 else 0 end) as placed_orders,
13+
sum(case when has_promotion then 1 else 0 end) as promoted_orders,
14+
avg(item_count) as avg_items_per_order
15+
from orders
16+
group by order_date
17+
)
18+
19+
select * from daily
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
with daily as (
2+
select * from {{ ref('int_daily_order_summary') }}
3+
),
4+
5+
final as (
6+
select
7+
order_date,
8+
order_count,
9+
total_revenue,
10+
total_cost,
11+
total_margin,
12+
total_discounts,
13+
total_revenue - total_discounts as net_revenue,
14+
avg_order_value,
15+
unique_customers,
16+
total_items_sold
17+
from daily
18+
)
19+
20+
select * from final
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
with inventory as (
2+
select * from {{ ref('product_inventory') }}
3+
),
4+
5+
snapshot as (
6+
select
7+
current_date as snapshot_date,
8+
count(distinct product_id) as total_products_tracked,
9+
count(distinct store_id) as total_stores,
10+
sum(current_stock) as total_stock_units,
11+
sum(case when stock_status = 'out_of_stock' then 1 else 0 end) as out_of_stock_count,
12+
sum(case when stock_status = 'low_stock' then 1 else 0 end) as low_stock_count,
13+
sum(case when stock_status = 'adequate' then 1 else 0 end) as adequate_count,
14+
sum(case when stock_status = 'well_stocked' then 1 else 0 end) as well_stocked_count,
15+
round(avg(current_stock), 1) as avg_stock_per_product_store
16+
from inventory
17+
)
18+
19+
select * from snapshot
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
with daily as (
2+
select * from {{ ref('metric_daily_revenue') }}
3+
),
4+
5+
monthly as (
6+
select
7+
date_trunc('month', order_date) as month_start,
8+
sum(order_count) as total_orders,
9+
sum(total_revenue) as total_revenue,
10+
sum(net_revenue) as net_revenue,
11+
sum(total_margin) as total_margin,
12+
sum(total_discounts) as total_discounts,
13+
avg(avg_order_value) as avg_daily_order_value,
14+
sum(total_items_sold) as total_items_sold,
15+
count(distinct order_date) as active_days
16+
from daily
17+
group by date_trunc('month', order_date)
18+
)
19+
20+
select * from monthly
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
with items as (
2+
select * from {{ ref('order_items') }}
3+
),
4+
5+
orders as (
6+
select order_id, order_date from {{ ref('int_order_enriched') }}
7+
),
8+
9+
daily_product as (
10+
select
11+
o.order_date,
12+
i.product_id,
13+
i.product_name,
14+
i.category_name,
15+
sum(i.quantity) as units_sold,
16+
sum(i.line_total) as revenue,
17+
sum(i.line_margin) as margin,
18+
count(distinct o.order_id) as order_count
19+
from items i
20+
inner join orders o on i.order_id = o.order_id
21+
group by o.order_date, i.product_id, i.product_name, i.category_name
22+
)
23+
24+
select * from daily_product
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
with discounts as (
2+
select * from {{ ref('order_discounts') }}
3+
),
4+
5+
daily as (
6+
select
7+
order_date,
8+
promotion_name,
9+
discount_type,
10+
count(*) as usage_count,
11+
sum(discount_amount) as total_discount,
12+
sum(subtotal) as total_order_value,
13+
sum(net_revenue) as total_net_revenue,
14+
avg(discount_pct_of_order) as avg_discount_pct
15+
from discounts
16+
group by order_date, promotion_name, discount_type
17+
)
18+
19+
select * from daily
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
with revenue as (
2+
select * from {{ ref('revenue_summary') }}
3+
),
4+
5+
stores as (
6+
select * from {{ ref('stores') }}
7+
),
8+
9+
daily as (
10+
select
11+
r.order_date,
12+
r.store_id,
13+
s.store_name,
14+
s.city,
15+
r.order_count,
16+
r.revenue,
17+
r.cost,
18+
r.margin,
19+
r.discounts,
20+
r.net_revenue
21+
from revenue r
22+
left join stores s on r.store_id = s.store_id
23+
)
24+
25+
select * from daily
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
with daily as (
2+
select * from {{ ref('metric_daily_revenue') }}
3+
),
4+
5+
weekly as (
6+
select
7+
date_trunc('week', order_date) as week_start,
8+
sum(order_count) as total_orders,
9+
sum(total_revenue) as total_revenue,
10+
sum(net_revenue) as net_revenue,
11+
sum(total_margin) as total_margin,
12+
sum(total_discounts) as total_discounts,
13+
sum(unique_customers) as total_customer_visits,
14+
avg(avg_order_value) as avg_daily_order_value,
15+
sum(total_items_sold) as total_items_sold
16+
from daily
17+
group by date_trunc('week', order_date)
18+
)
19+
20+
select * from weekly

0 commit comments

Comments
 (0)