DAX Workout 001 - Dynamic Format Strings

Hi There,

I completed this using the new Dynamic feature that was just released in Power BI. I’m late for the this workout.


under the measure component
!

Under the format component
image

Note: this option is in preview mode so becareful not to put in full production until Microsoft has full implelmented it.

Thanks for the workout
Keith

2 Likes

@Keith ,

Spot on perfect application of this new capability.

Great to have this in the thread for reference - thanks!

  • Brian
2 Likes

thanks @BrianJ

I thought I would give it a try using the new function after seeing a youtube video done by Curbal today :slight_smile:

Here is the video reference

thanks Keith

3 Likes

@Keith,

FYI – Here’s a different way of implementing this using a helper table, so that you can control the format selection from a slicer.

– Brian

2 Likes

thanks @BrianJ for the information

1 Like

@Brian Where did you go to review the custom formatting string rules?

1 Like

image

Bottom of the dropdown list is Dynamic

Here is my code:
Format Outcome =
if(
LEN(Data[Dollar Amount])<4,
FORMAT(Data[Dollar Amount],β€œΒ£0”),
if(
(LEN(Data[Dollar Amount])>3&&
LEN(Data[Dollar Amount])<7),
FORMAT(Data[Dollar Amount],β€œΒ£#,##0,.0 K”),
if(
(LEN(Data[Dollar Amount])>6&&
LEN(Data[Dollar Amount])<10),
FORMAT(Data[Dollar Amount],β€œΒ£#,##0,.0 M”),
FORMAT(Data[Dollar Amount],β€œΒ£#,##0,.0 B”)
)
)
)

Formatted =
var _formatted =
SWITCH(
TRUE(),
[Dollar Amount]>1000000000,FORMAT([Dollar Amount],β€œ$#,.0B”),
[Dollar Amount]>1000000,FORMAT([Dollar Amount],β€œ$#,.00M”),
[Dollar Amount]>100000,FORMAT([Dollar Amount],β€œ$#,.0K”),
FORMAT([Dollar Amount],β€œ$###”)

)

return _formatted

image

here is my solution
Formatted Result =
var amount =
SELECTEDVALUE(
Data[Dollar Amount]
)

var formatted =
SWITCH(
true(),
amount>1000000000, FORMAT(amount/1000000000,β€œ$#.0B”),
amount>1000000, FORMAT(amount/1000000,β€œ$#.00M”),
amount>1000, FORMAT(amount/1000,β€œ$#.0K”),
FORMAT(amount,β€œ$#”)
)

RETURN formatted

Here is my submission for Workout-001.
Its too late but good start for me :slightly_smiling_face:
@tweinzapfel really cool stuff, exploring each workout.
image
Workout 001.pbix (258.9 KB)

Here is my submission :slight_smile: Better late than never :smiley:

Total Dollar Amount CF = 
VAR __BaseMeasure = [Total Dollar Amount]
VAR __ResultCheck = LEN( __BaseMeasure )

VAR __Result =
    SWITCH(
        TRUE(),
        __ResultCheck < 4, FORMAT( __BaseMeasure, "$0" ),
        __ResultCheck <= 6, FORMAT( DIVIDE( __BaseMeasure, 1000 ), "$#,0.0k" ),
        __ResultCheck <= 9, FORMAT( DIVIDE( __BaseMeasure, 1000000 ), "$#,0.0M" ),
        __ResultCheck <= 12, FORMAT( DIVIDE( __BaseMeasure, 1000000000 ), "$#,0.0B" ),
        FORMAT( __BaseMeasure, "$#,0.0" )
    )
Return
    __Result

image

I didn’t use a new feature because it is still in Preview so I won’t use it in any project anyway.
Workout 001.pbix (260.8 KB)

Hi All,

I have attempted thes solution in Both ; Regular aswell as Dynamic Formatting option.

image

Dax for Both solutions is same, just used slightly differently.

Dax for Solution1:

Click to See Details

image

Dax for Solution2:

Click to See Details

image

Here is my solution. My β€œM” is rounded to 1, so it shows a bit different than what the results should look like.

workout001

1 Like

I suppose you would not need division in dynamic format strings.

DAX Workout No1 MB.pbix (292.1 KB)

Hello All,

the workout seems very tricky. There are a number of solutions to the problem, I am sharing one of them

Formatted Result =
SWITCH(
TRUE(),
Data[Dollar Amount] < 1000 , CONCATENATE( β€œ$”, Data[Dollar Amount] ),
Data[Dollar Amount] >= 1000 && Data[Dollar Amount] < 1000000, CONCATENATE(β€œ$ " , CONCATENATE ( ROUND( Data[Dollar Amount] / 1000, 2), " K” ) ),
Data[Dollar Amount] >= 1000000 && Data[Dollar Amount] < 1000000000 ,
CONCATENATE("$ β€œ, CONCATENATE( ROUND( Data[Dollar Amount] / 1000000,2 ) , " M” ) ),

CONCATENATE("$", CONCATENATE( ROUND( Data[Dollar Amount] / 1000000000,2 ) , " B") ) )

A) Solution using Nested IF function:

Formatted Result 1 =
VAR temp_amount = SELECTEDVALUE(Data[Dollar Amount]) //
VAR formatted_amount =
IF( temp_amount >= 1000000000, FORMAT( DIVIDE(temp_amount, 1000000000), β€œ$#0.0#B” ),
IF( temp_amount >= 1000000, FORMAT( DIVIDE(temp_amount,1000000), β€œ$#0.0#M” ),
IF( temp_amount >= 1000, FORMAT( DIVIDE(temp_amount,1000), β€œ$#0.0#k” ),
FORMAT(temp_amount, β€œ$#”) ) ) )
RETURN formatted_amount

B) Solution using SWITCH() function:

Formatted Result 2 =
VAR temp_amount = SELECTEDVALUE(Data[Dollar Amount])
VAR formatted_amount =

SWITCH( TRUE,
temp_amount >= 1000000000, FORMAT( DIVIDE(temp_amount, 1000000000), β€œ$#0.0#B” ),
temp_amount >= 1000000, FORMAT( DIVIDE(temp_amount,1000000), β€œ$#0.0#M”),
temp_amount >= 1000, FORMAT( DIVIDE(temp_amount,1000), β€œ$#0.0#k” ),
FORMAT(temp_amount, β€œ$#”)
)

RETURN formatted_amount

Snapshot of the visuals:
image

Here’s my workout .pbix file: Workout 001.pbix (257.6 KB)

Here is my solutions. A β€œbasic” one with which I got some problems however, a more complicated one with a parameter table containng factors and formatting.

Solution 1:

FormattedSales = 
VAR __Sales = SUM(Data[Dollar Amount])
VAR __Format = SWITCH(TRUE,
   __Sales < power(10, 3), "$0",
      __Sales < power(10, 6), "$0.0 K",
      __Sales < power(10, 9), "$0.0 M",
        "$0.0 B"
)
VAR __Ratio = SWITCH(TRUE,
   __Sales < power(10, 3), 1,
      __Sales < power(10, 6), power(10, 3),
      __Sales < power(10, 9), power(10, 6),
       power(10, 9)
)

RETURN FORMAT(__Sales/__Ratio, __Format)

Solution 2:
A table containing the parameters:

TableFormat = {

(-1, 1, "$0.0"),

(1000, 1000, "$0.0 K"),

(1000000, 1000000, "$0.0 M"),

(1000000000, 1000000000, "$0.0 B")

}

And the formula using it:

FormattedSales With Table = 
VAR __Sales = SUM(Data[Dollar Amount])

VAR __Table = TOPN(1, FILTER(TableFormat, TableFormat[Threshold] < __Sales), TableFormat[Factor], DESC)  // filtering the table to lines < sales, keeping the top 1
VAR __Ratio = MINX(__Table, TableFormat[Factor])   //getting the ratio
VAR __Format = MINX(__Table, TableFormat[FormatString])  //and the format string

RETURN  FORMAT(__Sales/__Ratio, __Format)

Hello, everyone,

here is the code for the first workout on the DAX code :slight_smile:

Formatted Value = 
var _number = SELECTEDVALUE(Data[Dollar Amount])
var _num_formatted = SWITCH(
                            TRUE(),
                            _number < 1000, FORMAT(_number,"$#"),
                            _number < 1000000, FORMAT(_number, "$###,K"),
                            _number < 1000000000, FORMAT(_number, "$#,##,,.00M"),
                            FORMAT(_number, "$#,0,,,.0B")
                    )
return
_num_formatted