looker

Looker conditional formatting using another field


I'm trying to create a conditional format based on another field.

Concretely, if the score_label_column == "Green" then it give a green background to the score_value_column, but I'm not sure how to reference another column in the html.

Here is what I have thus far:

view: name_of_view {
  derived_table: {
    sql: SELECT * FROM  MY_TABLE
      ;;
  }

  measure: count {
    type: count
    drill_fields: [detail*]
  }


  dimension: total_score_colored_looker{
    
    sql: ${TABLE}."score_value_column" ;;
    
    html:
    
       {% if score_label_column._value  == "Green"%}
      
       [color green]     
      
      {% elsif score_label_column._value  == "Yellow" %}
      
      [color yellow]
      
      {% else %}
      
      [color red]
      
      {% endif %} ;;
    
  }

I tried doing it the way the documents reference which is by wrapping it around double brackets {{ }}, but when it rendered I got the following error

Liquid Syntax Error: Liquid parse exception: extraneous input '{{' (around the text "% if {{ CH") Note: you should use "{% if var %}" rather than "{% if {{ var }} %}" to reference variables.

I also tried prefixing it with the view name name_of_view.score_label_column._value with and without double brackets to no avail.

Any help appreciated.


Solution

  • You are almost right with the liquid syntax, it is also quite sensitive so make sure that there is spacing between the brackets:

    {% if value%} - would not render correctly

    {% if value %} - would render

    To come back to your issue, you are referring to score_label_column which is not a dimension nor a measure that exist. If we want to refer to the current dimension, which is here total_score_colored_looker you can write as follow:

       {% if total_score_colored_looker._value  == "Green" %}
      
       [color green]     
      
      {% elsif total_score_colored_looker._value  == "Yellow" %}
      
      [color yellow]
      
      {% else %}
      
      [color red]
      
      {% endif %} ;;
    

    or

       {% if value  == "Green" %}
      
       [color green]     
      
      {% elsif value  == "Yellow" %}
      
      [color yellow]
      
      {% else %}
      
      [color red]
      
      {% endif %} ;;