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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
cuohanele
Helper I
Helper I

DYNAMIC FORMAT USING DAX FOR SLICER

Hi everyone! In need of some assistance.

I created a dynamic format for a column in my table to be filtered by a specific slicer. Unfortunately, it is sorting the numbers like they are string. I used the code below:

 

Sales$ = IF(ISCROSSFILTERED('Parameter'[calculations]),
SWITCH( TRUE(),
    VALUES('Parameter'[calculations]) = "YoY%",
SWITCH( TRUE(),
ABS([Sales$ YoY%]) < 1, FORMAT([Sales$ YoY%],"0.0%"),
ABS([Sales$ YoY%]) >= 1, FORMAT([Sales$ YoY%],"0%"),
BLANK()
),
    VALUES('Parameter'[calculations]) = "YoY Gap", CONCATENATE("$", FORMAT([Sales$ YoY Gap],"0,0")),
    VALUES('Parameter'[calculations]) = "Current Year Actuals", CONCATENATE("$", FORMAT([Sales$ CY],"0,0")),
BLANK()), BLANK ())
 
When any of the options in the slicer is picked, it returns this column sorted out of order. See below for visual:
SlicerSlicerColumn it is filtered onColumn it is filtered on
So it is sorting something like this:
100
1
2
200
222
3
33
 
Thanks in advance!
2 ACCEPTED SOLUTIONS

GOT IT!!!

 

I took the initial code you sent that fixed the percentage values and switched it out with the new line of code but left the currency values as is. THIS IS THE CODE THAT WORKS. Thank you so much!!!

 

 

Sales$ (Table) =
IF (
ISCROSSFILTERED ( 'Parameter'[calculations] ),
SWITCH (
TRUE (),
VALUES('Parameter'[calculations]) = "YoY%",
SWITCH( TRUE(),
ABS([Sales$ YoY%]) < 0.1, FORMAT([Sales$ YoY%]," 0.0%"),
ABS([Sales$ YoY%]) < 1, FORMAT([Sales$ YoY%]," 0.0%"),
FORMAT([Sales$ YoY%],"0%")
),

VALUES ( 'Parameter'[calculations] ) = "YoY Gap", RIGHT ( " $" & FORMAT ( [Sales$ YoY Gap], "0,0" ), 12 ),
VALUES ( 'Parameter'[calculations] ) = "Current Year Actuals", RIGHT ( " $" & FORMAT ( [Sales$ CY], "0,0" ), 12 ),
BLANK ()
),
BLANK ()
)

View solution in original post

Add another space and change the 5 to 6.

View solution in original post

20 REPLIES 20
Anonymous
Not applicable

Instead of all these stunts in the code and workarounds that will soon become hardly readable... why not use the functionality of PBI that's called bookmarks? Instead of a slicer for parameters just use buttons+bookmarks and everything will be easy and simple. By the way, by doing what you're doing right now you're risking that one day there'll be a case you have not thought about and you'll get a messed-up display. Please, for your own good, use buttons, views and bookmarks with simple measures.

Stunts schmunts.  Try this:

 

 

 

Sales$ :=
IF (
    ISCROSSFILTERED ( 'Parameter'[calculations] ),
    SWITCH (
        TRUE (),
        VALUES ( 'Parameter'[calculations] ) = "YoY%", IF (
            ABS ( [Sales$ YoY%] ) < 1,
            RIGHT ( "  " & FORMAT ( [Sales$ YoY%], "0.0%" ), 5 ),
            FORMAT ( [Sales$ YoY%], "0%" )
        ),
        VALUES ( 'Parameter'[calculations] ) = "YoY Gap", RIGHT ( "         $" & FORMAT ( [Sales$ YoY Gap], "0,0" ), 12 ),
        VALUES ( 'Parameter'[calculations] ) = "Current Year Actuals", RIGHT ( "         $" & FORMAT ( [Sales$ CY], "0,0" ), 12 ),
        BLANK ()
    ),
    BLANK ()
)

 

 

GOT IT!!!

 

I took the initial code you sent that fixed the percentage values and switched it out with the new line of code but left the currency values as is. THIS IS THE CODE THAT WORKS. Thank you so much!!!

 

 

Sales$ (Table) =
IF (
ISCROSSFILTERED ( 'Parameter'[calculations] ),
SWITCH (
TRUE (),
VALUES('Parameter'[calculations]) = "YoY%",
SWITCH( TRUE(),
ABS([Sales$ YoY%]) < 0.1, FORMAT([Sales$ YoY%]," 0.0%"),
ABS([Sales$ YoY%]) < 1, FORMAT([Sales$ YoY%]," 0.0%"),
FORMAT([Sales$ YoY%],"0%")
),

VALUES ( 'Parameter'[calculations] ) = "YoY Gap", RIGHT ( " $" & FORMAT ( [Sales$ YoY Gap], "0,0" ), 12 ),
VALUES ( 'Parameter'[calculations] ) = "Current Year Actuals", RIGHT ( " $" & FORMAT ( [Sales$ CY], "0,0" ), 12 ),
BLANK ()
),
BLANK ()
)

You are on a roll! So the currency values have been FIXED and the percentages look good with only a very minor issue. See picture below:Screenshot (10)_LI.jpg

Add another space and change the 5 to 6.

What if there are negative numbers? I tried applying  the same logic of your previous DAX to other columns with negative percentages and negative dollar amounts. I ran into this:Screenshot (11)_LI.jpgScreenshot (13)_LI.jpg

Anonymous
Not applicable

Hi

Didn't I say already that you'll be having a pretty hard time to get this to work correctly? And there'll probably always be cases not accounted for... If I were you, I'd stay away from such BRITTLE CODE.

That works! Thanks a bunch. This is the final DAX:

 

Sales$ (Table) :=
IF (
ISCROSSFILTERED ( 'Parameter'[calculations] ),
SWITCH (
TRUE (),
VALUES ( 'Parameter'[calculations] ) = "YoY%", IF (
ABS ( [Sales$ YoY%] ) < 1,
RIGHT ( " " & FORMAT ( [Sales$ YoY%], "0.0%" ), 6 ),
FORMAT ( [Sales$ YoY%], "0%" )
),
VALUES ( 'Parameter'[calculations] ) = "YoY Gap", RIGHT ( " $" & FORMAT ( [Sales$ YoY Gap], "0,0" ), 12 ),
VALUES ( 'Parameter'[calculations] ) = "Current Year Actuals", RIGHT ( " $" & FORMAT ( [Sales$ CY], "0,0" ), 12 ),
BLANK ()
),
BLANK ()

If there were negative percentages, how will the code look? Ran into that issue

How can I go about doing that?

Anonymous
Not applicable

Greg_Deckler
Super User
Super User

@cuohanele - Correct, the minute you use FORMAT, everything is a string. You might have some luck with the technique described here: https://community.powerbi.com/t5/Quick-Measures-Gallery/Chelsie-Eiden-s-Duration/m-p/793639#M389

 

It allows you to keep things numeric but display them differently.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

You're returning STRINGS from your measure, not numbers. FORMAT returns STRINGS. Dynamic formatting of measures is only available through calculation groups which are only available when you use Tabular Editor to design measures. It's not possible (yet) through the Power BI Desktop GUI. Sorry.

Is there a way to create three separate slicers that filter on the same column? By doing that I can change the datatypes for each specifically instead of using one string datatype for all three slicer options

lbendlin
Super User
Super User

That's what you get when you use FORMAT() - it produces strings.

 

Try this

 

 

Sales$ := IF(ISCROSSFILTERED('Parameter'[calculations]),
SWITCH( TRUE(),
    VALUES('Parameter'[calculations]) = "YoY%",
SWITCH( TRUE(),
ABS([Sales$ YoY%]) < 0.1, FORMAT([Sales$ YoY%]," 0.0%"),
ABS([Sales$ YoY%]) < 1, FORMAT([Sales$ YoY%],"0.0%"),
FORMAT([Sales$ YoY%],"0%")
),
    VALUES('Parameter'[calculations]) = "YoY Gap", CONCATENATE("$", FORMAT([Sales$ YoY Gap],"0,0")),
    VALUES('Parameter'[calculations]) = "Current Year Actuals", CONCATENATE("$", FORMAT([Sales$ CY],"0,0")),
BLANK()), BLANK ())

 

Hi. This is the same DAX i used. It's returning the column as texts and sorting out of order still

@cuohanele No, it is not the same. There are subtle differences.  Try it out.

Wow! It definitely made a change but we still ran into an issue. It's sorting the 100's like they are the number 10. See below:Screenshot (7)_LI.jpg

try this

 

Sales$ := IF(ISCROSSFILTERED('Parameter'[calculations]),
SWITCH( TRUE(),
    VALUES('Parameter'[calculations]) = "YoY%",
SWITCH( TRUE(),
ABS([Sales$ YoY%]) < 0.1, FORMAT([Sales$ YoY%],"  0.0%"),
ABS([Sales$ YoY%]) < 1, FORMAT([Sales$ YoY%]," 0.0%"),
FORMAT([Sales$ YoY%],"0%")
),
    VALUES('Parameter'[calculations]) = "YoY Gap", CONCATENATE("$", FORMAT([Sales$ YoY Gap],"0,0")),
    VALUES('Parameter'[calculations]) = "Current Year Actuals", CONCATENATE("$", FORMAT([Sales$ CY],"0,0")),
BLANK()), BLANK ())

We're getting somewhere! So it sorted the percentages right! But now the currency values are out of order (YOY GAP and Current Year Actuals). See below:Screenshot (9)_LI.jpg

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.