-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpop.view.lkml
292 lines (253 loc) · 7.73 KB
/
pop.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
include: "/views/pacing_block/multisource_ads.view.lkml"
view: +multisource_ads {
#fields_hidden_by_default: yes
dimension_group: created {
#hidden: no
type: time
view_label: "Period Over Period"
timeframes: [
raw,
time,
hour_of_day,
date,
day_of_week,
day_of_week_index,
day_of_month,
month,
month_name,
month_num,
quarter,
year]
sql: ${partition_date_date} ;;
convert_tz: no
datatype: date
}
filter: current_date_range {
#hidden: no
type: date
view_label: "Period Over Period"
label: "Current Date Range"
description: "Select the current date range you are interested in. Make sure any other filter on Event Date covers this period, or is removed."
sql: ${period} IS NOT NULL ;;
convert_tz: no
datatype: date
}
parameter: compare_to {
#hidden: no
view_label: "Period Over Period"
description: "Select the templated previous period you would like to compare to. Must be used with Current Date Range filter"
label: "Compare To:"
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"
}
## ------------------ HIDDEN HELPER DIMENSIONS ------------------ ##
dimension: days_in_period {
#hidden: no
# hidden: yes
view_label: "Period Over Period"
description: "Gives the number of days in the current period date range"
type: number
sql: DATE_DIFF( DATE({% date_start current_date_range %}), DATE({% date_end current_date_range %}), DAY) ;;
}
dimension: period_2_start {
#hidden: no
view_label: "Period Over Period"
description: "Calculates the start of the previous period"
type: date
sql:
{% if compare_to._parameter_value == "Period" %}
DATE_ADD(DATE({% date_start current_date_range %}), INTERVAL ${days_in_period} DAY)
{% else %}
DATE_SUB(DATE({% date_start current_date_range %}), INTERVAL 1 {% parameter compare_to %})
{% endif %};;
convert_tz: no
}
dimension: period_2_end {
#hidden: no
view_label: "Period Over Period"
description: "Calculates the end of the previous period"
type: date
sql:
{% if compare_to._parameter_value == "Period" %}
DATE_SUB(DATE({% date_start current_date_range %}), INTERVAL 1 DAY)
{% else %}
DATE_SUB(DATE_SUB(DATE({% date_end current_date_range %}), INTERVAL 1 DAY), INTERVAL 1 {% parameter compare_to %})
{% endif %};;
convert_tz: no
}
dimension: day_in_period {
hidden: yes
description: "Gives the number of days since the start of each period. 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 %} ${created_raw} {% endcondition %}
THEN DATE_DIFF( DATE({% date_start current_date_range %}), DATE(${created_date}), DAY) + 1
WHEN DATE(${created_date}) between ${period_2_start} and ${period_2_end}
THEN DATE_DIFF(${period_2_start}, DATE(${created_date}), DAY) + 1
END
{% else %} NULL
{% endif %}
;;
}
dimension: order_for_period {
hidden: yes
type: number
sql:
{% if current_date_range._is_filtered %}
CASE
WHEN {% condition current_date_range %} ${created_raw} {% endcondition %}
THEN 1
WHEN DATE(${created_date}) between ${period_2_start} and ${period_2_end}
THEN 2
END
{% else %}
NULL
{% endif %}
;;
}
## ------------------ DIMENSIONS TO PLOT ------------------ ##
dimension_group: date_in_period {
#hidden: no
description: "Use this as your grouping dimension when comparing periods. Aligns the previous periods onto the current period"
label: "Current Period"
type: time
# sql: DATE_ADD( ${day_in_period} - 1, DATE({% date_start current_date_range %}), DAY) ;;
sql: DATE_SUB(DATE({% date_start current_date_range %}), INTERVAL (${day_in_period} - 1) DAY) ;;
view_label: "Period Over Period"
timeframes: [
date,
hour_of_day,
day_of_week,
day_of_week_index,
day_of_month,
day_of_year,
week_of_year,
month,
month_name,
month_num,
year]
convert_tz: no
datatype: date
}
dimension: period {
#hidden: no
view_label: "Period Over Period"
label: "Period"
description: "Pivot me! Returns the period the metric covers, i.e. either the 'This Period' or 'Previous Period'"
type: string
order_by_field: order_for_period
sql:
{% if current_date_range._is_filtered %}
CASE
WHEN {% condition current_date_range %} ${created_raw} {% endcondition %}
THEN 'This {% parameter compare_to %}'
WHEN DATE(${created_date}) between ${period_2_start} and ${period_2_end}
THEN 'Last {% parameter compare_to %}'
END
{% else %}
NULL
{% endif %}
;;
}
## ---------------------- TO CREATE FILTERED MEASURES ---------------------------- ##
dimension: period_filtered_measures {
#hidden: yes
description: "We just use this for the filtered measures"
type: string
sql:
{% if current_date_range._is_filtered %}
CASE
WHEN {% condition current_date_range %} ${created_raw} {% endcondition %} THEN 'this'
WHEN DATE(${created_date}) between ${period_2_start} and ${period_2_end} THEN 'last' END
{% else %} NULL {% endif %} ;;
}
# Filtered measures
measure: current_period_clicks {
#hidden: no
view_label: "Period Over Period"
type: sum
sql:${total_clicks};;
filters: [period_filtered_measures: "this"]
}
measure: previous_period_clicks {
#hidden: no
view_label: "Period Over Period"
type: sum
sql: ${total_clicks};;
filters: [period_filtered_measures: "last"]
}
measure: current_period_impressions {
#hidden: no
view_label: "Period Over Period"
type: sum
sql:${total_impressions};;
filters: [period_filtered_measures: "this"]
}
measure: previous_period_impressions {
#hidden: no
view_label: "Period Over Period"
type: sum
sql: ${total_impressions};;
filters: [period_filtered_measures: "last"]
}
measure: current_period_spend {
#hidden: no
view_label: "Period Over Period"
type: sum
sql:${total_spent};;
filters: [period_filtered_measures: "this"]
}
measure: previous_period_spend {
#hidden: no
view_label: "Period Over Period"
type: sum
sql: ${total_spent};;
filters: [period_filtered_measures: "last"]
}
measure: current_period_revenue {
#hidden: no
view_label: "Period Over Period"
type: sum
sql:${total_revenue};;
filters: [period_filtered_measures: "this"]
}
measure: previous_period_revenue {
#hidden: no
view_label: "Period Over Period"
type: sum
sql: ${total_revenue};;
filters: [period_filtered_measures: "last"]
}
measure: clicks_pop_change {
#hidden: no
view_label: "Period Over Period"
label: "Total Clicks period-over-period % change"
type: number
sql: CASE WHEN ${current_period_clicks} = 0
THEN NULL
ELSE (1.0 * ${current_period_clicks} / NULLIF(${previous_period_clicks} ,0)) - 1 END ;;
value_format_name: percent_2
}
}