Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
RichardJ
Responsive Resident
Responsive Resident

Dynamically changing the number of decimal points and applying conditional prefix/suffix

Hi,

In the linked PBIX file I have a table with metrics, target, actual and delta values.

I'm wondering if there is a way of dynamically applying a format using a measure where

 

1) the 'Percentage' would appear with the suffix of the percentage symbol (e.g. 22.00 % - two decimal places)

2) the 'Cost' would appear with a prefix of the currency symbol (e.g. $ 71472- whole number, no decimal places)

 

I have a measure for the 'delta' but am stuggling to understand if it's possible to apply conditions to it to enable conditions 1 and 2 above.

 

Dynamic_Formatting.JPG

Thanks for any advice,

 

PBIX full link : https://www.dropbox.com/s/b907wyhzrflugkp/Power_BI_Dynamic_Formatting_Question.pbix?dl=0

 

Richard

4 ACCEPTED SOLUTIONS
mahoneypat
Employee
Employee

You could create variables that have a FORMAT or CONVERT function to conditional return those, but the better way is to use a Calculation Group with dynamic format expressions.

(6) Are you still not using Calculation Groups?! - YouTube

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

VahidDM
Super User
Super User

Hi @RichardJ 

 

Try this measure with Format dax code:

Delta = 
VAR _Delta =
    MAX ( 'Table'[Target] ) - MAX ( 'Table'[Actual] )
RETURN
    IF (
        MAX ( 'Table'[Metric] ) = "Cost",
        FORMAT ( _Delta, "$#,##0" ),
        IF (
            MAX ( 'Table'[Metric] ) = "Percentage",
            FORMAT ( _Delta, "%##.00" ),
            _Delta
        )
    )

 

Output will be as below:

VahidDM_0-1630369321811.png

If you want to add specific condition for any metrics, try to use SWITCH rather that if in the mesure.

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Appreciate your Kudos VahidDM_1-1630369412418.png !!

 

 

View solution in original post

RichardJ
Responsive Resident
Responsive Resident

@Vahid Thank you very much for this solution.
It was really helpful.
Ended up using this

Delta = 
VAR _Delta =
    SUM ( 'Table'[Target] ) - SUM ( 'Table'[Actual] )
RETURN
    IF (
        MAX ( 'Table'[Metric] ) = "Cost",
        FORMAT ( _Delta, "$ #,##0" ),
        IF (
            MAX ( 'Table'[Metric] ) = "Percentage",
            FORMAT ( _Delta/100, "0%" ),
            _Delta
        )
    )

 

and got the results I was looking for

.Resolved.JPG

 

Appreciate you making the effort to respond.

Cheers,

Richard

View solution in original post

@mahoneypat The video was awesome. Another great solution and not one I would have considered.


Never looked at calculation groups in detail before.

 

Thanks for taking the time to reply and for making the video.

 

I've subscribed to your channel.

Cheers,

Richard

View solution in original post

4 REPLIES 4
VahidDM
Super User
Super User

Hi @RichardJ 

 

Try this measure with Format dax code:

Delta = 
VAR _Delta =
    MAX ( 'Table'[Target] ) - MAX ( 'Table'[Actual] )
RETURN
    IF (
        MAX ( 'Table'[Metric] ) = "Cost",
        FORMAT ( _Delta, "$#,##0" ),
        IF (
            MAX ( 'Table'[Metric] ) = "Percentage",
            FORMAT ( _Delta, "%##.00" ),
            _Delta
        )
    )

 

Output will be as below:

VahidDM_0-1630369321811.png

If you want to add specific condition for any metrics, try to use SWITCH rather that if in the mesure.

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Appreciate your Kudos VahidDM_1-1630369412418.png !!

 

 

RichardJ
Responsive Resident
Responsive Resident

@Vahid Thank you very much for this solution.
It was really helpful.
Ended up using this

Delta = 
VAR _Delta =
    SUM ( 'Table'[Target] ) - SUM ( 'Table'[Actual] )
RETURN
    IF (
        MAX ( 'Table'[Metric] ) = "Cost",
        FORMAT ( _Delta, "$ #,##0" ),
        IF (
            MAX ( 'Table'[Metric] ) = "Percentage",
            FORMAT ( _Delta/100, "0%" ),
            _Delta
        )
    )

 

and got the results I was looking for

.Resolved.JPG

 

Appreciate you making the effort to respond.

Cheers,

Richard

mahoneypat
Employee
Employee

You could create variables that have a FORMAT or CONVERT function to conditional return those, but the better way is to use a Calculation Group with dynamic format expressions.

(6) Are you still not using Calculation Groups?! - YouTube

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


@mahoneypat The video was awesome. Another great solution and not one I would have considered.


Never looked at calculation groups in detail before.

 

Thanks for taking the time to reply and for making the video.

 

I've subscribed to your channel.

Cheers,

Richard

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.