tableau-apitableau-desktop

Rounding to three decimals in tableau


I have a calculated field within a tableau desktop workbook that is displaying an absolute difference value.

It used to be calculated like this: [delta_absolute (formatted with "+" when positive and "ppt" appended for proportion)]

However, this created a rounding error where values close to zero where automatically displayed as zero, leading to confusing results like: Absolute diff: -0.

I changed it to the following:

IF ABS([delta_absolute]) < 0.001 THEN
    "0.000 ppt"
ELSE
    IF [delta_absolute] >= 0 THEN
        "+" + STR(ROUND([delta_absolute], 3)) + " ppt"
    ELSE
        "-" + STR(ROUND(ABS([delta_absolute]), 3)) + " ppt"
    END
END

Which took care of it.

However, without having changed the calculated field (and the only change made is switching form a live data source to an extract), the field will now sometimes display values like e.g. -0.064000000000000001 ppt, which messes with my formatting.

Is there a way to make sure we're only rounding to three decimals?


Solution

  • I figured it out. The answer is:

    IF ABS([delta_absolute]) < 0.001 THEN
        "0.000 ppt"
    ELSEIF [delta_absolute] >= 0 THEN
        "+" + LEFT(STR([delta_absolute]), FIND(".", STR([delta_absolute])) + 4) + " ppt"
    ELSE
        "-" + LEFT(STR(ABS([delta_absolute])), FIND(".", STR(ABS([delta_absolute]))) + 4) + " ppt"
    END