Dynamic Date Granularity using Parameter in Google Data Studio

I created this dynamic bar chart which change the granularity based on the date selected, so if you the date selected > year, it will group the data per year then you can drill down to Month, the same if it is Month, you can drill down to week etc. see Example here.

it was just for fun, but someone asked me how it works, I thought it is worth a Quick Blog.

As of 27 October 2020, the date parameter are only available if you connect to BigQuery.

ok, basically you need just create a custom Query that calculate the duration between the start date Parameter and end date parameter which are passed by the Calendar Control

This example is using Average Australian wholesale Electricity Market, Obviously it works with any dataset that contain date.

WITH
   xx AS (
   SELECT
     *EXCEPT(YEAR),
     CAST(DATE_TRUNC(DATE(SETTLEMENTDATE), YEAR) AS timestamp) AS YEAR,
     CAST(DATE_TRUNC(DATE(SETTLEMENTDATE), MONTH) AS timestamp) AS MONTH,
     CAST(DATE_TRUNC(DATE(SETTLEMENTDATE), WEEK) AS timestamp) AS WEEK,
     CAST(DATE(SETTLEMENTDATE) AS timestamp) AS date,
     DATE_DIFF(PARSE_DATE('%Y%m%d',
         @DS_END_DATE),PARSE_DATE('%Y%m%d',
         @DS_START_DATE),DAY) + 1 AS nbrdays
   FROM
     xxxxxxx.PRICEARCHIVE
   WHERE
     DATE(SETTLEMENTDATE) >= PARSE_DATE('%Y%m%d',
       @DS_START_DATE)
     AND DATE(SETTLEMENTDATE) <= PARSE_DATE('%Y%m%d',
       @DS_END_DATE)
     AND UNIT="DUNIT")
 SELECT
   REGIONID,
   RRP,
   nbrdays,
   CASE
     WHEN nbrdays <= 1 THEN SETTLEMENTDATE
     WHEN nbrdays <= 7 THEN date
     WHEN nbrdays <= 31 THEN WEEK
     WHEN nbrdays <= 365 THEN MONTH
   ELSE
   YEAR
 END
   AS newdate,
   CASE
     WHEN nbrdays <= 1 THEN SETTLEMENTDATE
     WHEN nbrdays <= 7 THEN SETTLEMENTDATE
     WHEN nbrdays <= 31 THEN date
     WHEN nbrdays <= 365 THEN WEEK
   ELSE
   MONTH
 END
   AS newdate2
 FROM
   xx

One thought on “Dynamic Date Granularity using Parameter in Google Data Studio”

Leave a comment