Search code examples
powerbidaxpowerbi-desktopvega-litedeneb

vega lite format zeros and negative values as blank and have a K or M suffix depending on the number size


Within a text mark, I want to format quantitative values (currency) according to these rules:

  1. zeros as blanks
  2. if value is over 1M as M, under 1M as K
  3. negative values without a minus sign

(1) and (3) can be done with pbiFormat.

"format": "$#0,,.M;;;@",
"formatType"

The default D3 formatter can do (2).

"format": "$~s"

How can I do all three at the same time?


Solution

  • You need to use PowerBI dynamic format strings for this. For any value, you feed into Deneb two columns - the raw numeric value (for quantitative data allowing you to encode in Vega) and a formatted text value for displaying labels. The dynamic format string for the text value would follow the usual format of:

    SWITCH(TRUE(), 
    val <=1e3, "#,0.00",
    val <=1e6, "#,0,.00 K",
    val <=1e9, "#,0,,.00 M"
    )