Question

Problems writing conditional expression in Liquid with string parameters

  • 18 December 2020
  • 1 reply
  • 64 views

I am trying to write a section of conditional logic in the sql: section of a derived_table using a parameter. My strong preference is to use a string parameter to avoid using arbitrary numbers to control the logic.

This is the non-working version. No matter what value of the parameter is selected, the Liquid always resolves to the ‘else’ case. I have tried every different variant of reference a parameter that I have found online.

          where status = status_lookup.id and 

            {% if period_week_filter._parameter_value == 'before_next' %}

              week_index < 6

            {% else %}

              week_index < 2

            {% endif %};;

the parameter definition is:

  parameter:period_week_filter {

    type: string

    allowed_value: {

      value: "before_next"

    }

    allowed_value: {

      value: "before_last"

      }

    default_value: "before_next"

  }

 

The same code works fine, however, if I switch over to a number type parameter.

          where status = status_lookup.id and 

            {% if period_week_filter._parameter_value == '1' %}

              week_index < 6

            {% else %}

              week_index < 2

            {% endif %};;

 

the corresponding parameter definition is:

  parameter:period_week_filter {

    type: number

    allowed_value: {

      label: "before_next"

      value: "1"

    }

    allowed_value: {

      label: "before_last"

      value: "2"

      }

    default_value: "before_next"

  }

 

Please help - Thanks!


1 reply

When you use a parameter with type: string, the parameter_name._parameter_value Liquid variable requires that you enclose the values of the parameter with both single and double quotes. This is so that the single quotes are transmitted to the SQL, identifying the value as a string value.

 

You can see an example of this in this article in the Looker docs.

Reply