-
Notifications
You must be signed in to change notification settings - Fork 17
/
_date_comparison.view.lkml
251 lines (229 loc) · 8.44 KB
/
_date_comparison.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# this is the code for the date comparison tool, which mimics what google 360 does in the browser in comparing two different date ranges. use with _date_dim.view.lkml
view: _date_comparison {
# label: "Timeline Comparison Fields"
extension: required
filter: current_date_range {
view_label: "Timeline Comparison Fields"
label: "1. Date Range"
description: "Select the date range you are interested in using this filter, can be used by itself. Make sure any filter on Event Date covers this period, or is removed."
type: date
convert_tz: yes
}
filter: previous_date_range {
view_label: "Timeline Comparison Fields"
label: "2b. Compare To (Custom):"
group_label: "Compare to:"
description: "Use this if you want to specify a custom date range to compare to (limited to 2 comparison periods). Always use with '1. Date Range' filter (or it will error). Make sure any filter on Event Date covers this period, or is removed."
type: date
convert_tz: yes
}
dimension_group: in_period {
type: duration
intervals: [day]
description: "Gives the number of days in the current period date range"
sql_start: {% date_start current_date_range %} ;;
sql_end: {% date_end current_date_range %} ;;
hidden: yes
}
dimension: period_2_start {
view_label: "Timeline Comparison Fields"
description: "Calculates the start of the previous period"
type: date_raw
sql:
{% if compare_to._in_query %}
{% if compare_to._parameter_value == "Period" %}
TIMESTAMP_SUB({% date_start current_date_range %} , INTERVAL ${days_in_period} DAY)
{% else %}
TIMESTAMP(DATETIME_SUB(DATETIME({% date_start current_date_range %}) , INTERVAL 1 {% parameter compare_to %}))
{% endif %}
{% else %}
{% date_start previous_date_range %}
{% endif %};;
hidden: yes
}
dimension: period_2_end {
view_label: "Timeline Comparison Fields"
description: "Calculates the end of the previous period"
type: date_raw
sql:
{% if compare_to._in_query %}
{% if compare_to._parameter_value == "Period" %}
TIMESTAMP_SUB({% date_start current_date_range %}, INTERVAL 1 DAY)
{% else %}
TIMESTAMP(DATETIME_SUB(DATETIME_SUB(DATETIME({% date_end current_date_range %}), INTERVAL 1 DAY), INTERVAL 1 {% parameter compare_to %}))
{% endif %}
{% else %}
{% date_end previous_date_range %}
{% endif %};;
hidden: yes
}
dimension: period_3_start {
view_label: "Timeline Comparison Fields"
description: "Calculates the start of 2 periods ago"
type: date_raw
sql:
{% if compare_to._parameter_value == "Period" %}
TIMESTAMP_SUB({% date_start current_date_range %}, INTERVAL 2*${days_in_period} DAY)
{% else %}
TIMESTAMP(DATETIME_SUB(DATETIME({% date_start current_date_range %}), INTERVAL 2 {% parameter compare_to %}))
{% endif %};;
hidden: yes
}
dimension: period_3_end {
view_label: "Timeline Comparison Fields"
description: "Calculates the end of 2 periods ago"
type: date_raw
sql:
{% if compare_to._parameter_value == "Period" %}
TIMESTAMP_SUB(${period_2_start}, INTERVAL 1 DAY)
{% else %}
TIMESTAMP(DATETIME_SUB(DATETIME_SUB(DATETIME({% date_end current_date_range %}), INTERVAL 1 DAY), INTERVAL 2 {% parameter compare_to %}))
{% endif %};;
hidden: yes
}
dimension: period_4_start {
view_label: "Timeline Comparison Fields"
description: "Calculates the start of 4 periods ago"
type: date_raw
sql:
{% if compare_to._parameter_value == "Period" %}
TIMESTAMP_SUB({% date_start current_date_range %}, INTERVAL 3*${days_in_period} DAY)
{% else %}
TIMESTAMP(DATETIME_SUB(DATETIME({% date_start current_date_range %}), INTERVAL 3 {% parameter compare_to %}))
{% endif %};;
hidden: yes
}
dimension: period_4_end {
view_label: "Timeline Comparison Fields"
description: "Calculates the end of 4 periods ago"
type: date_raw
sql:
{% if compare_to._parameter_value == "Period" %}
TIMESTAMP_SUB(${period_2_start}, INTERVAL 1 DAY)
{% else %}
TIMESTAMP(DATETIME_SUB(DATETIME_SUB(DATETIME({% date_end current_date_range %}), INTERVAL 1 DAY), INTERVAL 3 {% parameter compare_to %}))
{% endif %};;
hidden: yes
}
parameter: compare_to {
description: "Choose the period you would like to compare to. Must be used with Current Date Range filter"
label: "2a. Compare To (Templated):"
type: unquoted
allowed_value: {
label: "Previous Period"
value: "Period"
}
allowed_value: {
label: "Previous Week"
value: "Week"
}
allowed_value: {
label: "Previous Month"
value: "Month"
}
allowed_value: {
label: "Previous Quarter"
value: "Quarter"
}
allowed_value: {
label: "Previous Year"
value: "Year"
}
default_value: "Period"
view_label: "Timeline Comparison Fields"
}
parameter: comparison_periods {
label: "3. Number of Periods"
description: "Choose the number of periods you would like to compare - defaults to 2. Only works with templated periods from step 2."
type: unquoted
allowed_value: {
label: "2"
value: "2"
}
allowed_value: {
label: "3"
value: "3"
}
allowed_value: {
label: "4"
value: "4"
}
default_value: "2"
view_label: "Timeline Comparison Fields"
}
dimension: period {
view_label: "Timeline Comparison Fields"
label: "Period"
description: "Pivot me! Returns the period the metric covers, i.e. either the 'This Period', 'Previous Period' or '3 Periods Ago'"
type: string
order_by_field: order_for_period
sql:
{% if current_date_range._is_filtered %}
CASE
WHEN {% condition current_date_range %} ${event_raw} {% endcondition %}
THEN "This {% parameter compare_to %}"
WHEN ${event_raw} between ${period_2_start} and ${period_2_end}
THEN "Last {% parameter compare_to %}"
WHEN ${event_raw} between ${period_3_start} and ${period_3_end}
THEN "2 {% parameter compare_to %}s Ago"
WHEN ${event_raw} between ${period_4_start} and ${period_4_end}
THEN "3 {% parameter compare_to %}s Ago"
END
{% else %}
NULL
{% endif %}
;;
}
dimension: order_for_period {
hidden: yes
view_label: "Timeline Comparison Fields"
label: "Period"
description: "Pivot me! Returns the period the metric covers, i.e. either the 'This Period', 'Previous Period' or '3 Periods Ago'"
type: string
sql:
{% if current_date_range._is_filtered %}
CASE
WHEN {% condition current_date_range %} ${event_raw} /*findme6*/{% endcondition %}
THEN 1
WHEN ${event_raw} between ${period_2_start} and ${period_2_end}
THEN 2
WHEN ${event_raw} between ${period_3_start} and ${period_3_end}
THEN 3
WHEN ${event_raw} between ${period_4_start} and ${period_4_end}
THEN 4
END
{% else %}
NULL
{% endif %}
;;
}
dimension_group: date_in_period {
description: "Use this as your date dimension when comparing periods. Aligns the all previous periods onto the current period"
label: "Current Period"
type: time
sql: TIMESTAMP_ADD({% date_start current_date_range %},INTERVAL (${day_in_period}-1) DAY) ;;
view_label: "Timeline Comparison Fields"
timeframes: [date, week, month, quarter, year]
}
dimension: day_in_period {
view_label: "Timeline Comparison Fields"
description: "Gives the number of days since the start of each periods. Use this to align the event dates onto the same axis, the axes will read 1,2,3, etc."
type: number
sql:
{% if current_date_range._is_filtered %}
CASE
WHEN {% condition current_date_range %} ${event_raw} {% endcondition %}
THEN TIMESTAMP_DIFF(${event_raw},{% date_start current_date_range %},DAY)+1
WHEN ${event_raw} between ${period_2_start} and ${period_2_end}
THEN TIMESTAMP_DIFF(${event_raw}, ${period_2_start},DAY)+1
WHEN ${event_raw} between ${period_3_start} and ${period_3_end}
THEN TIMESTAMP_DIFF(${event_raw}, ${period_3_start},DAY)+1
WHEN ${event_raw} between ${period_4_start} and ${period_4_end}
THEN TIMESTAMP_DIFF(${event_raw}, ${period_4_start},DAY)+1
END
{% else %} NULL
{% endif %}
;;
hidden: no
}
}