Looker: Comparing Dynamic Dates with Liquid

29 Views Asked by At

I am trying to utilize dynamic dates with liquid logic in a Looker derived table view to conditionally select the table that the query uses.

Specifically I'm trying to utilize the start date from a date filter {% date_start date_filter %} but struggling to assign the date_start to a liquid variable, or wondering if this is even possible. I'm using Snowflake, which appears to materialize {% date_start date_filter %} as something like date_add('days', -6, current_date()), if the date_filter is set to something like "last 7 days".

My desired code would is something like:

view: test_view {

  derived_table: {
    sql:
      SELECT
        
        test.id AS test_id,
        test.name AS test_name
      
    >>> {% assign start_date = {% date_start date_filter %} | date: "%d" %}      
        {% assign nowDate = "now" | date: "%d" %}
        {% assign diffDays = nowDate | minus: dateStart %}
        {% if diffDays < 14 %}
      FROM `dataset.table01` AS test
      
      {% else %}
      
      FROM `dataset.table02` AS test
      
      {% endif %}
     ;;


filter: date_filter {
  type: date
}

It makes sense to me that this variable assignment is not working ({% assign start_date = {% date_start date_filter %} | date: '%d' %} ), but I'm curious if there are any workarounds to achieve my desired result, or if my liquid syntax is wrong. Since this is a derived table, I'm unable to reference a LookML object in lieu of {% date_start date_filter %} and Looker does not support "output" syntax for date_start ( e.g. {{ date_start date_filter }} ).

Additional context in my reply comment in this Looker thread.

0

There are 0 best solutions below