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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Switch with multiple conditions in a variance

Hi All , 

I am trying to create a variance of shipment from budgeted. However , the customer allows permissible variance which I am trying to build in DAX function. the below function is returning zero for all. Here are the conditions I am trying to build.

If Variance (shipment-budgeted) is > 0 ; then report the actual varaianceEg : Variance = 15 ; result =15
If Variance is  less than 0 but greater than -5 , then report vairance as 0Eg : Variance = - 4; result  = 0
If variance is less than -5 , then add 5 to the actual varianceEg : Variance  = -25 , result = -20 

 

Here is the function I am using . I think >=0 <= -5 condition is incorrect , but really dont know how to write. really appreciate any help!

variance Mod =
var Forecast = SUM('Monthly Budget'[Order Budget])
var VarianceR =[Shipment_tons]-Forecast
var switchFnc =
SWITCH(TRUE(),
VarianceR>0,VarianceR,
VarianceR>=-5&&VarianceR<=0,0,
VarianceR<-5,VarianceR+5)
return
switchFnc
 
Thanks,
SV
1 ACCEPTED SOLUTION
Fowmy
Super User
Super User

@Anonymous 

Can you the values in the columns and the filter context on which you are running this measure?

Try the following:

varianceMod =
VAR Forecast =CALCULATE (  SUM ( 'Monthly Budget'[Order Budget] ) )
VAR VarianceR = [Shipment_tons] - Forecast
VAR switchFnc =
    SWITCH (
        TRUE (),
        VarianceR > 0, VarianceR,
        VarianceR >= -5
            && VarianceR <= 0, 0,
        VarianceR < -5, VarianceR + 5
    )
RETURN
    switchFnc
Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

5 REPLIES 5
Fowmy
Super User
Super User

@Anonymous 

Can you the values in the columns and the filter context on which you are running this measure?

Try the following:

varianceMod =
VAR Forecast =CALCULATE (  SUM ( 'Monthly Budget'[Order Budget] ) )
VAR VarianceR = [Shipment_tons] - Forecast
VAR switchFnc =
    SWITCH (
        TRUE (),
        VarianceR > 0, VarianceR,
        VarianceR >= -5
            && VarianceR <= 0, 0,
        VarianceR < -5, VarianceR + 5
    )
RETURN
    switchFnc
Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

What could be the reason , when the above variance is put in a matrix the row total gives incorrect results (Budget and shipmet totals are correct) ? I checked all the filters , but cannot figure out the reason.

2021-10-25_15-57-40.png

 

 

 

Anonymous
Not applicable

What could be the reason , when the above variance is put in a matrix the row total gives incorrect results (Budget and shipmet totals are correct) ? I checked all the filters , but cannot figure out the reason.

2021-10-25_15-57-40.png

 

 

 

smpa01
Super User
Super User

@Anonymous  can you reverse the switch order and try

SWITCH(TRUE(),
VarianceR<-5,VarianceR+5,
VarianceR>=-5&&VarianceR<=0,0,
VarianceR>0,VarianceR,)

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
mattww
Responsive Resident
Responsive Resident

Hi @Anonymous ,

 

That expression seems to be working on my end when I've built it in a simple model, could it be something with your relationships that's causing the issue?

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.