Forum Discussion
User Defined Function - Dynamic Formatting
- 9 months ago
Hi christsup3r
In general, a format string expression should return a format string, rather than the result of formatting a value with a format string.
In your case, if you're wanting format strings to be set based on _FormatType and _Scale parameters, you could rewrite your function as follows (I've made some simplifications as well):
DEFINE FUNCTION Fn_Formatting = ( _FormatType, _Scale ) => VAR CurrencySymbol = SWITCH ( _FormatType, "Amounts", "$", "" ) VAR NumScaleCommas = SWITCH ( _Scale, "B", 3, "M", 2, "K", 1, 0 ) VAR ScaleCommas = REPT ( ",", NumScaleCommas ) VAR FinalFormatString = CurrencySymbol & "#,0" & ScaleCommas & ".0" & "\" & _Scale RETURN FinalFormatStringThen you could set the dynamic format string of a particular measure to be an expression like any of these:
Fn_Formatting ( "Amount", "K" )Fn_Formatting ( "Numbers", "M" )Does this work for you?
See this post for some further demonstrations of UDFs applied to format strings.
Hi christsup3r
In general, a format string expression should return a format string, rather than the result of formatting a value with a format string.
In your case, if you're wanting format strings to be set based on _FormatType and _Scale parameters, you could rewrite your function as follows (I've made some simplifications as well):
DEFINE
FUNCTION Fn_Formatting = ( _FormatType, _Scale ) =>
VAR CurrencySymbol =
SWITCH (
_FormatType,
"Amounts", "$",
""
)
VAR NumScaleCommas = SWITCH (
_Scale,
"B", 3,
"M", 2,
"K", 1,
0
)
VAR ScaleCommas = REPT ( ",", NumScaleCommas )
VAR FinalFormatString =
CurrencySymbol & "#,0" & ScaleCommas & ".0" & "\"
& _Scale
RETURN
FinalFormatString
Then you could set the dynamic format string of a particular measure to be an expression like any of these:
Fn_Formatting ( "Amount", "K" )
Fn_Formatting ( "Numbers", "M" )
Does this work for you?
See this post for some further demonstrations of UDFs applied to format strings.