sql server - SSRS Expression Evaluation Issue Nested IIF/Switch -
long-time reader, first time poster.
i've got tricky situation ssrs expression want with.
i have, amongst others, 2 columns inside table named:
forecastmovementcurrentperiod actualmovementcurrentperiod
i have 5 criteria need applied in expression third column.
they follows:
if forecastmovementcurrentperiod = 0 , 'actualmovementcurrentperiod-forecastmovementcurrentperiod'>0, return "-100%"
if forecastmovementcurrentperiod = 0 , 'actualmovementcurrentperiod-forecastmovementcurrentperiod'<0, return "+100%"
if forecastmovementcurrentperiod <> 0 , if 'actualmovementcurrentperiod-forecastmovementcurrentperiod' not error, , abs((actualmovementcurrentperiod-forecastmovementcurrentperiod) / forecastmovementcurrentperiod) > 100%, return "large"
if forecastmovementcurrentperiod <> 0 , if 'actualmovementcurrentperiod-forecastmovementcurrentperiod' not error, , abs((actualmovementcurrentperiod-forecastmovementcurrentperiod) / forecastmovementcurrentperiod) < 20%, return "minor"
if forecastmovementcurrentperiod <> 0 , if 'actualmovementcurrentperiod-forecastmovementcurrentperiod' not error , abs((actualmovementcurrentperiod-forecastmovementcurrentperiod)/forecastmovementcurrentperiod) < 100% , > 20%, return (actualmovementcurrentperiod-forecastmovementcurrentperiod)/forecastmovementcurrentperiod)
those criteria expression needs return. i've tried combination of switch statements , iif statements, can't work.
below code:
=switch ( sum(fields!forecastmovementcurrentperiod.value) = 0.00 , sum(fields!actualmovementcurrentperiod.value) - sum(fields!forecastmovementcurrentperiod.value) > 0.00, "-100%", sum(fields!forecastmovementcurrentperiod.value) = 0.00 , sum(fields!actualmovementcurrentperiod.value) - sum(fields!forecastmovementcurrentperiod.value) < 0.00, "+100%", sum(fields!forecastmovementcurrentperiod.value) <> 0 , (sum(fields!actualmovementcurrentperiod.value) - sum(fields!forecastmovementcurrentperiod.value) <> 0.99123131414) , (abs(sum(fields!actualmovementcurrentperiod.value)) - abs(sum(fields!forecastmovementcurrentperiod.value))) / abs(sum(fields!forecastmovementcurrentperiod.value)) > 1.00, "large", sum(fields!forecastmovementcurrentperiod.value) <> 0 , (sum(fields!actualmovementcurrentperiod.value) - sum(fields!forecastmovementcurrentperiod.value) <> 0.9912313141) , (abs(sum(fields!actualmovementcurrentperiod.value)) - abs(sum(fields!forecastmovementcurrentperiod.value))) / abs(sum(fields!forecastmovementcurrentperiod.value)) < 0.20, "minor", sum(fields!forecastmovementcurrentperiod.value) <> 0 , (sum(fields!actualmovementcurrentperiod.value) - sum(fields!forecastmovementcurrentperiod.value) <> 0.9912313141) , ((abs(sum(fields!actualmovementcurrentperiod.value)) - abs(sum(fields!forecastmovementcurrentperiod.value))) / abs(sum(fields!forecastmovementcurrentperiod.value)) > 0.20 , (abs(sum(fields!actualmovementcurrentperiod.value)) - abs(sum(fields!forecastmovementcurrentperiod.value))) / abs(sum(fields!forecastmovementcurrentperiod.value))) < 1.00, (sum(fields!actualmovementcurrentperiod.value) - sum(fields!forecastmovementcurrentperiod.value)) / sum(fields!forecastmovementcurrentperiod.value) )
this returns correctly "large", "minor" , final criterion % value of 2 columns. problem i'm having it's returning error when first 2 criteria met, e.g, forecastmovementcurrentperiod = 0 , actualmovementcurrentperiod-forecastmovementcurrentperiod results in "5" - should return "-100%" instead returns #error.
the interesting thing if remove last 3 lines in switch statement, have, say:
=switch ( sum(fields!forecastmovementcurrentperiod.value) = 0.00 , sum(fields!actualmovementcurrentperiod.value) - sum(fields!forecastmovementcurrentperiod.value) > 0.00, "-100%", sum(fields!forecastmovementcurrentperiod.value) = 0.00 , sum(fields!actualmovementcurrentperiod.value) - sum(fields!forecastmovementcurrentperiod.value) < 0.00, "+100%" )
it returns correct values same rows return #error when other 3 lines in there! can't life of me figure out. i've tried nested iif statements no avail.
can please tell me doing wrong? far knew, switch statement return first value true based on order in statements there. why won't return "-100%" or "+100%" when criteria met , last 3 lines of switch statement there?
any awesome!
edit:
i've figured out issue.
i managed figure out in end. though switch statement return true value first expression, looks switch statement won't return value until after it's evaluated every expression in statement. when trying evaluate last 3 statements, there divide 0 occurring , that's why return #error though first expression evaluated true.
strange behaviour because would've thought once switch statement found true value, wouldn't bother evaluate other ones.
my solution wrap iif statements around final 3 expressions avoid divide zero.
code below:
=switch ( sum(fields!forecastmovementcurrentperiod.value) = 0.00 , sum(fields!actualmovementcurrentperiod.value) - sum(fields!forecastmovementcurrentperiod.value) > 0.00, "-100%", sum(fields!forecastmovementcurrentperiod.value) = 0.00 , sum(fields!actualmovementcurrentperiod.value) - sum(fields!forecastmovementcurrentperiod.value) < 0.00, "+100%", sum(fields!forecastmovementcurrentperiod.value) <> 0 , (sum(fields!actualmovementcurrentperiod.value) - sum(fields!forecastmovementcurrentperiod.value) <> 0.99123131414) , iif(abs(sum(fields!forecastmovementcurrentperiod.value)) = 0, 0, abs(sum(fields!actualmovementcurrentperiod.value)) - abs(sum(fields!forecastmovementcurrentperiod.value))) / iif(abs(sum(fields!forecastmovementcurrentperiod.value)) = 0, 1, abs(sum(fields!forecastmovementcurrentperiod.value))) > 1.00, "large", sum(fields!forecastmovementcurrentperiod.value) <> 0 , (sum(fields!actualmovementcurrentperiod.value) - sum(fields!forecastmovementcurrentperiod.value) <> 0.9912313141) , iif(abs(sum(fields!forecastmovementcurrentperiod.value)) = 0, 0, abs(sum(fields!actualmovementcurrentperiod.value)) - abs(sum(fields!forecastmovementcurrentperiod.value))) / iif(abs(sum(fields!forecastmovementcurrentperiod.value)) = 0, 1, abs(sum(fields!forecastmovementcurrentperiod.value))) < 0.20, "minor", sum(fields!forecastmovementcurrentperiod.value) <> 0 , (sum(fields!actualmovementcurrentperiod.value) - sum(fields!forecastmovementcurrentperiod.value) <> 0.9912313141) , (iif(abs(sum(fields!forecastmovementcurrentperiod.value)) = 0, 0, abs(sum(fields!actualmovementcurrentperiod.value)) - abs(sum(fields!forecastmovementcurrentperiod.value))) / iif(abs(sum(fields!forecastmovementcurrentperiod.value)) = 0, 1, abs(sum(fields!forecastmovementcurrentperiod.value))) > 0.20 , iif(abs(sum(fields!forecastmovementcurrentperiod.value)) = 0, 0, abs(sum(fields!actualmovementcurrentperiod.value)) - abs(sum(fields!forecastmovementcurrentperiod.value))) / iif(abs(sum(fields!forecastmovementcurrentperiod.value)) = 0, 1, abs(sum(fields!forecastmovementcurrentperiod.value)))) < 1.00, iif(abs(sum(fields!forecastmovementcurrentperiod.value)) = 0, 0, abs(sum(fields!actualmovementcurrentperiod.value)) - abs(sum(fields!forecastmovementcurrentperiod.value))) / iif(abs(sum(fields!forecastmovementcurrentperiod.value)) = 0, 1, abs(sum(fields!forecastmovementcurrentperiod.value))), true, "error" )
i still find strange switch won't terminate evaluation of entire block once finds first true value - seems waste of computation me.
anyway guys!
i think last switch statement has parenthesis in wrong place. see if break down:
sum(cpv) <> 0 , (sum(mcp) - sum(cpv) <> 0.9912313141) , ( (abs(sum(mcp)) - abs(sum(cpv))) / abs(sum(cpv)) > 0.20 , (abs(sum(mcp)) - abs(sum(cpv))) / abs(sum(cpv)) -- not valid calculation ) < 1.00, (sum(mcp) - sum(cpv)) / sum(cpv)
that sixth line not valid calculation, because closed parens early. need put last parens after "< 1.00". this:
sum(cpv) <> 0 , (sum(mcp) - sum(cpv) <> 0.9912313141) , ( (abs(sum(mcp)) - abs(sum(cpv))) / abs(sum(cpv)) > 0.20 , (abs(sum(mcp)) - abs(sum(cpv))) / abs(sum(cpv)) < 1.00 ), -- close parens after (sum(mcp) - sum(cpv)) / sum(cpv)
to further simplify, try using between statement instead, eliminates duplication:
sum(cpv) <> 0 , (sum(mcp) - sum(cpv) <> 0.9912313141) , ( ((abs(sum(mcp)) - abs(sum(cpv))) / abs(sum(cpv))) between 0.20 , 1.00 ), (sum(mcp) - sum(cpv)) / sum(cpv)
Comments
Post a Comment