The introduction of parameters in Looker allows folks the ability to swap between dimensions / measures in their visualizations, with added functionality that did not exist before.
Although similar functionality existed with templated filters, what makes parameters more robust is its label_from_parameter. Variable measures can now take the name of whatever parameter value is selected by the user. E.g., from a list of 3 measures, you pick a measure called “revenue”, and a measure called “revenue” will land in your table visualization, instead of “variable measure”.
Currently, Looker cannot “inherit” measure formatting of selected measures when flipping between different choices. However, there is a workaround, which I detail below. This includes
- Using liquid in a sql case statements to 1) Select a Measure 2) Inject the Measure in a visualization
- Using liquid in an html if statement, to select the desired formatting
- You must reference the parameter as if it were a filter with _filters[‘parameter_name’]
Use case of 3 measures, that includes whole numbers, measures with 1 decimal place, and $ values with 2 decimal places
- For whole numbers, use {{ rendered_value | round }}
- For measures with one decimal place, use {{ rendered_value | round: 1 }}
- For $s with two decimal places, use ${{ rendered_value | round: 2 }} or ${{ rendered_value }} if you’ve set the value_format of your variable_measure to “0.00”
Important: Make sure that you pick a value_format with the most amount of decimal places that any of the selectable measures can contain. The use of round in the liquid variable can only remove decimal places, but cannot add any.
Please see example below
Happy looking!
##@@@@@@@@@@
##PARAMETERS
##@@@@@@@@@@
parameter: measure_type {
allowed_value: {label: "Sales Revenue $" value: "Sales Revenue $" }
allowed_value: {label: "Activations" value: "Activations" }
}
##@@@@@@@@@@
##MEASURES
##@@@@@@@@@@
measure: variable_measure {
type: number
label_from_parameter: measure_type
sql: case
when {% parameter measure_type %} = 'Sales Revenue' then ${sales_rev}
when {% parameter measure_type %} = 'Activations' then ${activations}
end
;;
html: {% if _filters['measure_type'] == 'Activations' %} {{ rendered_value | round }}
{% else %} ${{ rendered_value }}
{% endif %}
;;
value_format: "0.00"
}