This block allows for comparisons from one date period to another, or from one date period to a number of periods. It provides extensive flexibility in comparing date ranges, and presenting these back as a single visualisation.
There are already a few blocks that allow for comparison between periods in Looker. The motivation for this block was to combine all the elements from the other blocks in a way that is intuitive, flexible and fast to use.
The approach was to use the intuitive style of date comparison that is used in Google Analytics - where the basic idea is that you choose the current date range that you’re interested in, then choose the comparison date range in the past. There are some templated period filters to make this easy for the user, but also custom filters to allow flexibility in comparing dates. The features are:
-
Templated comparison periods - Once the initial period of interest is chosen, rather than manually choose the period before, there is a range of options presented such as “Previous Period”, “Previous Month”, “Previous Year” etc
-
Custom comparison periods - in the situation where any of the above don’t fit what you are trying to see, then a custom previous date range may be chosen
-
Any granularity - Choose how granular you want your results to be by picking the appropriate date dimension from the dimension group
- Multiple periods - Choose the number of periods you would like to compare, this is only available for templated periods, e.g January this year vs January last year vs January 2 years ago etc.
The process for using this in your explore is as follows:
- Add the filter
Date Range
to choose your initial date range - Add the filter
Compare To (Templated)
to choose a templated comparison range OR Add the filterCompare To (Custom)
to choose a custom comparison range - If you have chosen a templated range, you may choose to add more periods by using the
Comparison Periods
filter - Choose your date dimension - only pick from the dimension group
Current Period Date
, don’t use any date dimension from any other view - Choose your other dimensions and measures as usual
- Finally, pivot on
Period
- Hit run
Syntax assumes a BigQuery connection, you may need to adjust this for other database connections. To have this available to use in your explores and dashboards there are a few steps:
- Copy the view file
_date_comparison.view.lkml
into your project - In the view file where the date dimension you would like to be able to compare is, extend the
_date_comparison
view by adding the parameterextends: [_date_comparison]
- In the same view file, add one new dimensions,
event_raw
. This is simply<your_date_dimension>::datetime
. This step is just so that naming convention used in the_date_comparison
view works correctly - In the relevant explore LookML, add in the
sql_always_where
clause defined in the model file here. Replace all instances of<your_view_name>
with your view name.
If you have you connection set up to a timezone and time selected e.g. UTC, US Central Time, Looker will pass additional arguements into the SQL which will error with the old code block, this is due to the use of the Date_sub function.
The following error will occur:
No matching signature for function FORMAT_TIMESTAMP for argument types: STRING, DATE, STRING. Supported signature: FORMAT_TIMESTAMP(STRING, TIMESTAMP, [STRING]) at
Please ensure your code is up to date with our master branch, which now has implemented fixes for this.