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
Anonymous
Not applicable

Switch Restricts to evaluate all the conditions

Using SWITCH the first condition met defines the result.
In the following example, the second condition (<= 150) will never be met, because the first one is less restrictive. Is there any solution so that switch should go to each and every condition, Order of condition should not matter.
DEFINE MEASURE Sales[Discounted Sales] =
    SUMX (
        SUMMARIZE ( Sales, Sales[Net Price], Product[Category] ),
        VAR DiscountPct =
            SWITCH (
                TRUE,
                Sales[Net Price] <= 1000, 0.2,
                Sales[Net Price] <= 150, 0.15,
                Product[Category] = "Audio", 0.13,
                0
            )
        RETURN
            [Sales Amount] * (1 - DiscountPct )
    )
EVALUATE
SUMMARIZECOLUMNS (
    'Product'[Category],
    "Sales Amount", [Sales Amount],
    "Discounted sales", [Discounted Sales]
)
ORDER BY [Category]
#Power_BI #DAX_Community
4 REPLIES 4
Anonymous
Not applicable

SWITCH (
    TRUE,
    Sales[Net Price] <= 150, 0.15,
    Sales[Net Price] <= 1000, 0.2,
    Product[Category] = "Audio", 0.13,
    0
)

does what you want with regards to the examples you've given. The order of conditions ALWAYS MATTERS but if you order them correctly, you'll get what you want.

Anonymous
Not applicable

"Is there any solution so that switch should go to each and every condition, Order of condition should not matter."

 

What do you want to do? SWITCH will go to the first condition that is TRUE in the order of their appearance. There's no way around it. Whether you should use SWITCH or not depends on the logic you want to implement. But you have not revealed anything about the logic... so nothing can be advised.

Anonymous
Not applicable

I am creating a calculated column, So I am explaining, what output I want exactly:
See the example below:- 
VAR DiscountPct =
            SWITCH (
                TRUE,
                Sales[Net Price] <= 1000, 0.2,
                Sales[Net Price] <= 150, 0.15,
                Product[Category] = "Audio", 0.13,
                0
            )
With these 3 conditions if I input the number
"20" then the output should be "0.15"
"151" then the output should be "0.2"
I want every condition to execute and work properly, the order of conditions should not matter.
Can you advise something other than "Switch".

Probably replying to and old question no sure if relevant for the original person but at least would be for the community. 
The problem here is that you are not evaluating the full condition. 
Based on the logic you want to implement, what you are saying is that if the value is less or equal to 1000 AND is greater than 150 then it's 0.2 
and only if the value is less or equal to 150, then the result should be 0.15.
In order to resolve this you need to use an AND condition in your statement to resolve it, like these:

VAR DiscountPct =
            SWITCH (
                TRUE,
                AND(Sales[Net Price] <= 1000,Sales[Net Price] > 150) , 0.2,
                Sales[Net Price] <= 150, 0.15,
                Product[Category] = "Audio", 0.13,
                0
            )



Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

Top Solution Authors