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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
ratgdillon
Frequent Visitor

Formatting with a SWITCH function

Hi,

 

I am using a SWITCH function to select between cases and revenue as a measure. For cases, the format should be a whole number. For revenue, the format should be currency. This is my formula:

 

Measure Selection = IF(ISCROSSFILTERED('Measure'[Measure]), 
SWITCH(
    TRUE(), 
    VALUES('Measure'[Measure]) = "Gross Revenue", FORMAT([Total Sales], "Currency"), 
    VALUES('Measure'[Measure]) = "Cases", FORMAT([Total Quantity], "0"),
    BLANK()), BLANK())

 

 where [Total Sales] = CALCULATE(SUM([Sales])) and [Total Quantity] = CALCULATE(SUM([Cases]))

 

Here is my 'Measure' table:

Measures table.png

 

When I use FORMAT, my table shows blank values:

SWITCH formatted.png

 

When I don't use the FORMAT function, my values do not naturally format the way I would like them to:

SWITCH non-formatted.png

 

There are two possible solutions:

1. Find a way to get rid of the blank rows

2. Use a function other than FORMAT to format my values into whole numbers and currency.

 

Here's some sample data:

ProductSalesCases
E.Com - Cakes & Pies/Shells101
E.Com - Cakes & Pies/Shells303
Pound Cake40001000
Pound Cake1472368

 

Thanks!

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

In your SWITCH statement you need to also check that there is actually a value, or FORMAT will happily produce an empty string.

 

Measure Selection = IF(ISCROSSFILTERED('Measure'[Measure]), 
SWITCH(
    TRUE(), 
    VALUES('Measure'[Measure]) = "Gross Revenue" && [Total Sales]<>0, FORMAT([Total Sales], "Currency"), 
    VALUES('Measure'[Measure]) = "Cases" && [Total Quantity]>0, FORMAT([Total Quantity], "0"),
    BLANK()), BLANK())

View solution in original post

9 REPLIES 9
amitchandak
Super User
Super User

@ratgdillon , I think you have to use max here

Measure Selection = IF(ISCROSSFILTERED('Measure'[Measure]),
SWITCH(
TRUE(),
Max('Measure'[Measure]) = "Gross Revenue", FORMAT([Total Sales], "Currency"),
max('Measure'[Measure]) = "Cases", FORMAT([Total Quantity], "0"),
BLANK()), BLANK())

 

But this will make date type as text .

This example can also help you how to handle measure Slicer -https://radacad.com/change-the-column-or-measure-value-in-a-power-bi-visual-by-selection-of-the-slic...

I have Stacked Chart, but can't get this working. do you have any other solution?

lbendlin
Super User
Super User

In your SWITCH statement you need to also check that there is actually a value, or FORMAT will happily produce an empty string.

 

Measure Selection = IF(ISCROSSFILTERED('Measure'[Measure]), 
SWITCH(
    TRUE(), 
    VALUES('Measure'[Measure]) = "Gross Revenue" && [Total Sales]<>0, FORMAT([Total Sales], "Currency"), 
    VALUES('Measure'[Measure]) = "Cases" && [Total Quantity]>0, FORMAT([Total Quantity], "0"),
    BLANK()), BLANK())

Hello, I'm looking a measure that change the format the format of salesAmount dynamically based on currency selection slicer and you give me this code : Sales Amount Selected Currency = VAR SelectedCurrency = SELECTEDVALUE('SlicerTable'[Currency]) RETURN IF(SelectedCurrency = "Local", SUM('SalesTable'[SalesAmount]), IF(SelectedCurrency = "Euro", FORMAT(SUM('SalesTable'[SalesAmount_EUR]), "€#,##0.00"), IF(SelectedCurrency = "Dollar", FORMAT(SUM('SalesTable'[SalesAmount_EUR]), "$#,##0.00") ) ) ) the problem when i use this measure, it show me a blank values out of the range sales dates (my sales dates years 2022 and 2023 before using the measure , after using the measure i found year 2014 and 2015 with blank values). how we can solve the problem ?

@ahmadov_10 please create a new post. Include sample data.

Hi lbenlin,
I am new to DAX and I have tried to implement your solution into my query, but I am not sure where I am going wrong? Below is my selector measure and switch measure that I have created as measures. This "selector measure" is used as a toggle between the different metrics ($ and FTE). I then put on a "Card" visual the "Switch Measure:" which changes according to the toggle.
 
Problem:
Everytime i switch to the Currency metric, the Symbol does not display at all. Can you pleae help me modify my measures to allow the Currency symbol to display when only the $ metric is selected?
 
Selector Measure:  
Selector = UNION(ROW("Type","Hrs."),ROW("Type","Wks."),ROW("Type","FTE"), Row("Type","$."))
Switch Measure:
Cost Selection = var selectitem=IF(HASONEVALUE(Selector[Type]),VALUES(Selector[Type]),BLANK())
return
SWITCH(selectitem,
"$", [Costing ($)],
"FTE", [Costing (FTE)],
BLANK())

@Deevo_ please create a new post with your question

Why this is not working for stacked Chart?

Hi,

 

I added "ISCROSSFILTERED" and checking actually a value exists in my switch funtion but format option didn't work out.

My final measure is dependant on multiple measures.

Are there any steps needs to be taken.

 

Thank you in advance.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors