More than 3 conditions with Excel value formatting

Knowledge Drop

Last tested: Nov 10, 2020
 

When using the Excel-style value formatting in a visualization, we can create a multiple conditions by linking them with a ;. For instance, applying two or more conditions [>=300]0.00,\M; [<=299] 0.00 \K in the visualization directly.

This is possible to be able to format positive, negative, 0 values and text. From Microsoft's documentation:

The following example shows the four types of format code sections.f2b64157-2150-4d6d-850e-21663688f94c.gif


e7950f26-9de8-460b-ab4c-4a4140f0ad7d.gif
 Format for positive numbers
685bd419-b740-4e75-bd45-5911b044fafc.gif
 Format for negative numbers
60df03f3-4603-4443-b354-587538f7b1de.gif
  Format for zeros
1ba319b8-df6f-4044-89a7-60be57f34a94.gif
  Format for text

If you specify only one section of format code, the code in that section is used for all numbers. If you specify two sections of format code, the first section of code is used for positive numbers and zeros, and the second section of code is used for negative numbers. When you skip code sections in your number format, you must include a semicolon for each of the missing sections of code. You can use the ampersand (&) text operator to join, or concatenate, two values.

Workaround for this for the front end would be create a table calculation that uses if(), substring and concatenation to accomplish the formatting. From the LookML we have a lot of formatting options with the html: parameter.

An example for using HTML in our LookML could look like this:

 

html:

{% if {{value}} >= 0 %}

{{ rendered_value }}

{% elsif {{value}} <=-1000000 %}

-£{{ value | divided_by: 1000000 | round: 2 | times: -1 }}M

{% elsif {{value}} <=-1000 %}

-£{{ value | divided_by: 1000 | round: 2 | times: -1 }}K

{% elsif {{value}} <0 %}

-£{{ value | times: -1 }}

{% endif %}

One Example of a table calculation formatting for values in the millions to show as "0.0 M", in the thousands as "0.0 K". and similar formatting for negative millions and thousands:

if(${calculation_2}>=1000000,

concat(to_string(round(${calculation_2}/1000000,1)), " M"),

if(${calculation_2}>=1000,

concat(to_string(round(${calculation_2}/1000,1)), " K"),

if(${calculation_2}<=-1000000,

concat(to_string(round(${calculation_2}/1000000,1)), " M"),

if(${calculation_2}<=-1000, concat(to_string(round(${calculation_2}/1000,1)), " K"),

to_string(${calculation_2})

)

)

)

)

This content is subject to limited support.                

Version history
Last update:
‎07-07-2021 01:13 PM
Updated by: