Looker Community

Timeframes for Dimensions

Hey team,
I currently followed the instructions that allow me to change the date granularity on my looks (https://help.looker.com/hc/en-us/articles/360001288468-Dynamic-Timeframes-for-Dimension-Groups)

The issue however, is that this new dimension does not behave like a date.

As you can see, even if I change my datepicker to Month, I still have a fixed date instead of the month name. I even tried changing the dynamic_timeframe to a date type, but nothing worked.

Also, on top of that, if I order the dynamic_timeframe descending, it does not behave in the same way as the dates.

It’s been a few weeks but your issue is that you’re probably using a type: string parameter for your dynamic dimension.

Check out this article, it might help you.

Also, you should add label_from_parameter so that your viz shows the selected timeframe instead of the name of the dimension.

1 Like

That’s a really valuable tip. If actually helped me keep my code a little cleaner, and the name of the axis now refers to the parameter.

However when trying to use a bar chart , the x-axis is still not recognized as a date, but rather as a string. I tried setting a type: date but the grouping did not work at all (see below).

My dynamic_timeframe code was like:

dimension: dynamic_timeframe {
    label_from_parameter: timeframe_picker
    sql:
    {% if timeframe_picker._parameter_value == 'Day' %}
      ${segment_received_date}
    {% elsif timeframe_picker._parameter_value == 'Month' %}
      ${segment_received_month}
    {% else %}
      ${segment_received_week}
    {% endif %};;
    type: date
  }

I was actually thinking wether it would be possible to to apply a if statement to the type field, so that I would alternate between date, date_week, date_month

Your table screenshot shows “dimension filling”, which you can disable from the gear menu on the column header for the date field. With dimension filling turned off, is that the kind of result you’re looking for?

Edit: if so, you can permanently flag this to off for the field with allow_fill

1 Like

That’s a great solution! Thanks a lot
I found it very intriguing that I could only make it work with my weeky and daily aggreation. Anything other than that, is not being aggregated. See below:

LookML

   dimension_group: segment_received_date {
        type: time
        datatype: yyyymmdd
        timeframes: [date, month, week, year]
        sql:${TABLE}.ymd;;
        hidden: yes
      }

parameter: timeframe_picker {
        label: "Time Interval"
        type: unquoted
        allowed_value: {value: "Day"}
        allowed_value: {value: "Week"}
        allowed_value: {value: "Month"}
      }

      dimension: dynamic_timeframe {
        label_from_parameter: timeframe_picker
        sql:
        {% if timeframe_picker._parameter_value == 'Day' %}
          ${segment_received_date_date}
        {% elsif timeframe_picker._parameter_value == 'Month' %}
          ${segment_received_date_month}
        {% elsif timeframe_picker._parameter_value == 'Week' %}
          ${segment_received_date_week}
        {% endif %};;
        type: date
        allow_fill: no
      }

Do you know why this is happening? Is it maybe related to the type: date? If so, is it possible to dynamically modify the type to date, date_week, date month?

If I had to guess, I’d say because the _month frame might return a string like 2019-11 which then can’t be coerced to a date. I’d try manually inserting the “month” SQL with a date_trunc (and optional timezone conversion if needed)