Forum Discussion
FORMAT function with standard string and 1 decimal digit
- 4 years ago
Yes that's the idea.
I would suggest you use variables to avoid evaluating the same measure(s) multiple times.
The code I posted earlier was what I was thinking as far as structure:
- Store the measure itself in one variable MeasureValue (T_01, T02 etc).
- Then store the chosen format string in a second variable
- Finally, format the measure using the format string
Hi kyrpav
I don't believe there is any solution involving a single format string that will show integers with no decimal point/places, but otherwise show a decimal point & one decimal place.
I would say the other options are either:
1. Add an additional column to your v_raw_data table that specifies format strings for integers, e.g. add a column v_raw_data[value_formatter_integer].
Then change your DAX expression so that it uses this new column for integers. Something like this (with a bit of re-jigging as well):
VAR MetricValue =
SWITCH (
SELECTEDVALUE( v_raw_data[metric_code] ),
"T_01", [T_01],
//...
BLANK ()
)
VAR FormatString =
IF (
MetricValue = TRUNC ( MetricValue ), -- integer
SELECTEDVALUE ( v_raw_data[value_formatter_integer] ),
SELECTEDVALUE ( v_raw_data[value_formatter] )
)
RETURN
FORMAT ( MetricValue, FormatString )
2. Alternatively, you could do something with calculation group format strings.
https://www.sqlbi.com/articles/controlling-format-strings-in-calculation-groups/
Regards,
Owen
I was thinking the same thing if i understand well to do it like
switch(selectedvalue(v_raw_data[metric_code]),
"T_02",if(TRUNC([T_02])=[T_02],FORMAT([T_02],SELECTEDVALUE(v_raw_data[value_formatter])),FORMAT([T_02],"##0.0"))
but i did not like to run the same function too many times. What you propose to split it also i think is better.
- OwenAuger4 years ago
Super User
Yes that's the idea.
I would suggest you use variables to avoid evaluating the same measure(s) multiple times.
The code I posted earlier was what I was thinking as far as structure:
- Store the measure itself in one variable MeasureValue (T_01, T02 etc).
- Then store the chosen format string in a second variable
- Finally, format the measure using the format string