Date Comparison Block
What is this block?
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. The code can be found in this repo in the Datatonic GitHub.
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.
How to use
The process for using this in your explore is as follows:
- Add the filter
Date Rangeto choose your initial date range
- Add the filter
Compare To (Templated)to choose a templated comparison range OR Add the filter
Compare 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
- 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
- Hit run
How to implement
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.lkmlinto your project
- In the view file where the date dimension you would like to be able to compare is, extend the
_date_comparisonview by adding the parameter
- In the same view file, add two new dimensions,
event_raw.These are simply
<your_date_dimension>_rawrespectively. This step is just so that naming convention used in the
_date_comparisonview works correctly
- In the relevant explore LookML, add in the
sql_always_whereclause defined in the model file here. Replace all instances of
<your_view_name>with your view name.