Forum Discussion

AdamWhittaker's avatar
3 years ago
Solved

Custom Visual - Display Units - Auto not working

Hello, I installed the formatting utils from here: https://github.com/microsoft/powerbi-visuals-utils-formattingutils I added ` (to the capabilities.json)     "displayUnit": { "displayNa...
  • dm-p's avatar
    3 years ago

    Hi AdamWhittaker,

    It's been a while since I've looked into this (I've been looking for some code where I've resolved it that I could copy/paste in here but don't have anything to hand), so I'm working from memory (and hopefully it might be close to correct).

    If you want auto formatting to work, I believe you'll need to substitute the measure value into the value property when displayUnit is 0, as this property represents the scale that Power BI needs to consider when formatting.

    Based on your example, something like the following should work:

     

    const formatter: IValueFormatter = valueFormatter.create({
        value: this.visualSettings.value.displayUnit || value,
        precision: this.visualSettings.value.measurePrecision,
        cultureSelector: "en-US"
    });
    
    formatter.format(value);
    

     

    This change (on line 2) will coalesce the displayUnit (which would be 0 if auto) to the measure value, which would then be used to derive whether the value should be formatted as none, thousands, millions etc.

    This would be equaivalent to:

     

    const formatter: IValueFormatter = valueFormatter.create({
        value: this.visualSettings.value.displayUnit !== 0 ? this.visualSettings.value.displayUnit : value,
        precision: this.visualSettings.value.measurePrecision,
        cultureSelector: "en-US"
    });
    
    formatter.format(value);
    

     

    Regards,

    Daniel