-
Notifications
You must be signed in to change notification settings - Fork 4
/
24_inventory_snapshot.view.lkml
103 lines (83 loc) · 2.38 KB
/
24_inventory_snapshot.view.lkml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
view: inventory_snapshot {
derived_table: {
# sql_trigger_value: select current_date ;;
sql: with calendar as
(select distinct to_date(created_at) as snapshot_date
from inventory_items
-- where dateadd('day',90,created_at)>=current_date
)
select
inventory_items.product_id
,calendar.snapshot_date
,count(*) as number_in_stock
from inventory_items
left join calendar
on inventory_items.created_at <= calendar.snapshot_date
and (inventory_items.sold_at >= calendar.snapshot_date OR inventory_items.sold_at is null)
-- where dateadd('day',90,calendar.snapshot_date)>=current_date
group by 1,2
;;
}
dimension: product_id {
type: number
sql: ${TABLE}.product_id ;;
}
dimension: snapshot_date {
type: date
sql: ${TABLE}.snapshot_date ;;
}
dimension: number_in_stock {
type: number
hidden: yes
sql: ${TABLE}.number_in_stock ;;
}
measure: total_in_stock {
type: sum
sql: ${number_in_stock} ;;
}
measure: stock_coverage_ratio {
type: number
sql: 1.0 * ${total_in_stock} / nullif(${trailing_sales_snapshot.sum_trailing_28d_sales},0) ;;
value_format_name: decimal_2
}
measure: sum_stock_yesterday {
type: sum
hidden: yes
sql: ${number_in_stock} ;;
filters: {
field: snapshot_date
value: "yesterday"
}
}
measure: sum_stock_last_wk {
type: sum
hidden: yes
sql: ${number_in_stock} ;;
filters: {
field: snapshot_date
value: "8 days ago for 1 day"
}
}
measure: stock_coverage_ratio_yday {
type: number
view_label: "Stock Ratio Changes"
sql: 1.0 * ${sum_stock_yesterday} / nullif(${trailing_sales_snapshot.sum_trailing_28d_sales_yesterday},0) ;;
value_format_name: decimal_2
}
measure: stock_coverage_ratio_last_wk {
type: number
view_label: "Stock Ratio Changes"
sql: 1.0 * ${sum_stock_last_wk} / nullif(${trailing_sales_snapshot.sum_trailing_28d_sales_last_wk},0) ;;
value_format_name: decimal_2
}
measure: wk_to_wk_change_coverage {
label: "WoW Change - Coverage Ratio"
view_label: "Stock Ratio Changes"
sql: round(100*(${stock_coverage_ratio_yday}-${stock_coverage_ratio_last_wk}),1) ;;
value_format_name: decimal_1
# value_format: "# 'bp'"
}
set: detail {
fields: [product_id, snapshot_date, number_in_stock]
}
}