Question

Label from conditional liquid parameter

  • 10 February 2019
  • 5 replies
  • 155 views

I have multiple tables that hold the same data source, aggregated with different breakdowns. Instead of building different Explores for each table, I want to provide a parameter for users to select their preferred breakdown and have the data returned reflect this choice. I have a table with columns breakdown_1, breakdown_2 and a bunch of aggregated columns.


If someone selects “Platform and Placement” from breakdown_selected below, the label of breakdown_1 should read “Platform”. Likewise, if someone selects “Age and Gender” from breakdown_selected below, the label of breakdown_1 should read “Age”.


I have tried multiple ways of inserting the liquid into the label of breakdown_1 and nothing is working. “Other” appears in this example.


Is it possible to achieve this?


    parameter: breakdown_selected {
label: "What dimensions do you want to break the data down by?"
allowed_value: {
label: "Platform and Placement"
value: "platform_placement"
}
allowed_value: {
label: "Age and Gender"
value: "age_gender"
}
}

dimension: breakdown_1 {
sql: ${TABLE}.breakdown_1 ;;
label:
"{% if breakdown_selected._parameter_value == 'platform_placement' %} Platform
{% elsif breakdown_selected._parameter_value == 'age_gender' %} Age
{% else %} Other {% endif %}"
}

5 replies

Hi!


To get the label set by a parameter, add this to your dimension definition:


label_from_parameter: parameter_name


See this example from the docs https://docs.looker.com/reference/field-params/parameter#parameters_of_type_string.


Hope this helps,

Andy

I am trying to the same thing and facing the same problem as Charlotte. I want the label to appear in the field picker and not the visualization and hence label_from_parameter won’t work for me. I am not sure why the liquid is only picking up the else condition even though the if condition is true.


Any suggestions are appreciated.

So I was having the same trouble you guys are and found this in documentation:


"In addition, if you want to include the value of a parameter with type: string in a label , you must precede the double quotes with the \ character:


label: "{% if test._parameter_value == \"'foo'\" %} 'SUCCESS' {% else %} 'FAIL' {% endif %}" "


So you basically have to place \" before the ' and it’s going to work.


Here is the link if you want to read some more about it

Userlevel 4
Badge

Bear in mind that the label in the field picker is rendered whenever the page loads, so it won’t dynamically change when you modify the parameter value. You’ll need to refresh the page for that.

Hi,

This is still not working for me, even if I refresh the page.

parameter: date_granularity {
type: string
allowed_value: {
label: "Day"
value: "day"
}
allowed_value: {
label: "Week"
value: "week"
}
allowed_value: {
label: "Month"
value: "month"
}
}

dimension: time_since_account_creation {
label: "{% if date_granularity._parameter_value == \"'day'\" %} Days Since Account Creation
{% elsif date_granularity._parameter_value == \"'week'\" %} Weeks Since Account Creation
{% else %} Months Since Account Creation
{% endif %}"
type: number
sql: ${TABLE}.time_since_account_creation ;;
}

The label when I choose “Day” in the filter in my explore, shows as “Months Since Account Creation” even after refreshing the page. I have tried clearing the cache too but it still doesn’t work.

Reply