Forum Discussion
Dynamic format string - format
Hi powerbiexpert22,
The easiest way to remember it: 0 means "always show a digit here", # means "show a digit only if needed", a comma between digits turns on the thousands separator, and each comma at the end (right before the decimal point or the end) divides the number by 1,000.
Format string 1234567.891 shows as Why ---------------- --------------------- ------------------------------- 0 1234568 whole number #,0 1,234,568 thousands separator #,0.00 1,234,567.89 2 decimals, always shown #,0.## 1,234,567.89 up to 2 decimals (hides trailing 0) #,0,"K" 1,235K one trailing comma = / 1,000 #,0.0,,"M" 1.2M two trailing commas = / 1,000,000 0.0% (0.256 -> 25.6%) % multiplies by 100 #,0;(#,0);"-" positive;negative;zero sectionsSo for K/M the only thing that changes is how many commas you put at the end, and the text in quotes is just a label. In a dynamic format string you pick the right one based on the size of the value:
-- Measure > Format: Dynamic, then use this as the format string expression
VAR v = ABS ( SELECTEDMEASURE () )
RETURN
SWITCH (
TRUE (),
v >= 1000000000, "#,0.0,,,""B""",
v >= 1000000, "#,0.0,,""M""",
v >= 1000, "#,0.0,""K""",
"#,0"
)Note that a quote inside a DAX string is written as two quotes (""M"" gives "M" in the format string), and that the format only changes how the value is displayed; the underlying number stays the same for sorting and totals.
So my overall approach would be:
0 = forced digit, # = optional digit -> comma between digits = separator, trailing comma = / 1,000 -> "K"/"M" are just quoted labels -> SWITCH on SELECTEDMEASURE() in the dynamic format stringAI-assisted drafting: AI was used to help structure and phrase this response. I reviewed and validated the technical content before posting.