Created
December 27, 2023 15:29
-
-
Save PBI-DataVizzle/36feccd55e36640e55c450df1c0796eb to your computer and use it in GitHub Desktop.
DAX dynamic number formatting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//------- DYNAMIC FORMATTING ---------\\ | |
------------------------------ | |
var _selectedMeasure = SELECTEDMEASURE() | |
var _digitCount = | |
IFERROR( | |
LEN( | |
CONVERT( | |
INT( _selectedMeasure ), | |
STRING ) | |
) | |
, 0) | |
// define abbreviations | |
var _trillion = "T" | |
var _billion = "B" | |
var _million = "M" | |
var _thousand = "K" | |
// derive suffix | |
var _suffix = | |
SWITCH( | |
TRUE(), | |
_digitCount >= 12, _trillion , // 13 to display 100 billions ( 0.9 Trillion vs 880 billion ) | |
_digitCount >= 9, _billion, // 10 to display 100 millions ( 0.9 Billion vs 880 million ) | |
_digitCount >= 6, _million, // 7 to display 100 thousands ( 0.9 Million vs 100 thousand ) | |
_digitCount >= 4, _thousand, | |
"" | |
) | |
// derive commas and decimals per abbreviation | |
var _dp = 0 | |
var _dps = "." & REPT ( 0, _dp ) | |
var commas = | |
REPT( "," , | |
SWITCH( _suffix, | |
_trillion, 4, // "#,#0,,,,.# T" | |
_billion, 3, // "#,#0,,,.# B" | |
_million, 2, // "#,#0,,.# M" | |
_thousand, 1 // "#,#0,.# K" | |
,0) // "#,##0" | |
) | |
// derive formatting | |
var _decimalvalue = "#,##0.00" | |
var _baseFormat = "#,#0" | |
var _formattedNumber = _baseFormat & commas & _dps & "# " & _suffix | |
var _result = | |
SWITCH( | |
TRUE(), | |
_digitCount >= 2, _formattedNumber | |
, _decimalvalue | |
) | |
RETURN | |
_result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment