SSRS表达式评估问题嵌套IIF / Switch

时间:2015-05-14 05:45:37

标签: sql-server reporting-services switch-statement iif ssrs-expression

长期读者,第一次海报。

我有一个SSRS表达式的棘手情况,我想得到帮助。

我有一个名为:

的表中的2列

ForecastMovementCurrentPeriod ActualMovementCurrentPeriod

我有5个标准需要在表达式中应用到第三列。

他们如下:

  

如果ForecastMovementCurrentPeriod = 0 AND   ' ActualMovementCurrentPeriod-ForecastMovementCurrentPeriod'> 0,返回   " -100%"

     

如果ForecastMovementCurrentPeriod = 0 AND   ' ActualMovementCurrentPeriod-ForecastMovementCurrentPeriod'< 0,返回   " 100%"

     

如果ForecastMovementCurrentPeriod<> 0如果   ' ActualMovementCurrentPeriod-ForecastMovementCurrentPeriod'不是   错误,和   ABS((ActualMovementCurrentPeriod-ForecastMovementCurrentPeriod)/   ForecastMovementCurrentPeriod)> 100%,返回"大"

     

如果ForecastMovementCurrentPeriod<> 0如果   ' ActualMovementCurrentPeriod-ForecastMovementCurrentPeriod'不是   错误,和   ABS((ActualMovementCurrentPeriod-ForecastMovementCurrentPeriod)/   ForecastMovementCurrentPeriod)< 20%,返回"轻微"

     

如果ForecastMovementCurrentPeriod<> 0如果   ' ActualMovementCurrentPeriod-ForecastMovementCurrentPeriod'不是   错误和   ABS((ActualMovementCurrentPeriod-ForecastMovementCurrentPeriod)/ ForecastMovementCurrentPeriod)   < 100%和> 20%,回报   (ActualMovementCurrentPeriod-ForecastMovementCurrentPeriod)/ ForecastMovementCurrentPeriod)

这些是表达式需要返回的标准。我已尝试过SWITCH语句和IIF语句的组合,但我无法使其发挥作用。

以下是我的代码:

=SWITCH
    (
        SUM(Fields!ForecastMovementCurrentPeriod.Value) = 0.00 AND SUM(Fields!ActualMovementCurrentPeriod.Value) - SUM(Fields!ForecastMovementCurrentPeriod.Value) > 0.00, "-100%",
        SUM(Fields!ForecastMovementCurrentPeriod.Value) = 0.00 AND SUM(Fields!ActualMovementCurrentPeriod.Value) - SUM(Fields!ForecastMovementCurrentPeriod.Value) < 0.00, "+100%",
        SUM(Fields!ForecastMovementCurrentPeriod.Value) <> 0 AND (SUM(Fields!ActualMovementCurrentPeriod.Value) - SUM(Fields!ForecastMovementCurrentPeriod.Value) <> 0.99123131414) AND (ABS(SUM(Fields!ActualMovementCurrentPeriod.Value)) - ABS(SUM(Fields!ForecastMovementCurrentPeriod.Value))) / ABS(SUM(Fields!ForecastMovementCurrentPeriod.Value)) > 1.00, "Large",
        SUM(Fields!ForecastMovementCurrentPeriod.Value) <> 0 AND (SUM(Fields!ActualMovementCurrentPeriod.Value) - SUM(Fields!ForecastMovementCurrentPeriod.Value) <> 0.9912313141) AND (ABS(SUM(Fields!ActualMovementCurrentPeriod.Value)) - ABS(SUM(Fields!ForecastMovementCurrentPeriod.Value))) / ABS(SUM(Fields!ForecastMovementCurrentPeriod.Value)) < 0.20, "Minor",  
        SUM(Fields!ForecastMovementCurrentPeriod.Value) <> 0 AND (SUM(Fields!ActualMovementCurrentPeriod.Value) - SUM(Fields!ForecastMovementCurrentPeriod.Value) <> 0.9912313141) AND ((ABS(SUM(Fields!ActualMovementCurrentPeriod.Value)) - ABS(SUM(Fields!ForecastMovementCurrentPeriod.Value))) / ABS(SUM(Fields!ForecastMovementCurrentPeriod.Value)) > 0.20 AND (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)
    )

对于&#34; Large&#34;,&#34; Minor&#34;最终标准是2列的%值。我遇到的问题是,当满足前2个条件时,它会返回错误,例如,ForecastMovementCurrentPeriod = 0和ActualMovementCurrentPeriod-ForecastMovementCurrentPeriod会导致说&#34; 5&#34; - 它应该返回&#34; -100%&#34;但是返回#Error。

有趣的是,如果我删除SWITCH语句中的最后3行,那么我只会说:

=SWITCH
(
    SUM(Fields!ForecastMovementCurrentPeriod.Value) = 0.00 AND SUM(Fields!ActualMovementCurrentPeriod.Value) - SUM(Fields!ForecastMovementCurrentPeriod.Value) > 0.00, "-100%",
    SUM(Fields!ForecastMovementCurrentPeriod.Value) = 0.00 AND SUM(Fields!ActualMovementCurrentPeriod.Value) - SUM(Fields!ForecastMovementCurrentPeriod.Value) < 0.00, "+100%"
    )

它返回相同行的正确值,当其他3行在那里时返回#Error!我不能为我的生活弄清楚这一点。我试过嵌套的IIF语句无济于事。

任何人都可以告诉我我可能做错了什么吗?据我所知,SWITCH语句将根据语句的顺序返回第一个值为TRUE的值。为什么不会回来&#34; -100%&#34;或&#34; + 100%&#34;什么时候满足标准,我的SWITCH声明的最后3行也在那里?

任何帮助都会很棒!

编辑:

我已经找到了问题。

我最终设法搞清楚了。尽管SWITCH语句将为第一个表达式返回一个真值,但看起来SWITCH语句实际上不会返回该值,直到它在语句中计算了EVERY表达式为止。当它试图评估最后3个语句时,出现了零除以及即使第一个表达式求值为true也会返回#ERROR的原因。

奇怪的行为,因为我会想到,一旦SWITCH语句找到了真值,就不会费心去评估其他的。

我的解决方案是围绕最后3个表达式包装IIF语句,以避免被零除。

代码如下:

=SWITCH
    (
        SUM(Fields!ForecastMovementCurrentPeriod.Value) = 0.00 AND SUM(Fields!ActualMovementCurrentPeriod.Value) - SUM(Fields!ForecastMovementCurrentPeriod.Value) > 0.00, "-100%",
        SUM(Fields!ForecastMovementCurrentPeriod.Value) = 0.00 AND SUM(Fields!ActualMovementCurrentPeriod.Value) - SUM(Fields!ForecastMovementCurrentPeriod.Value) < 0.00, "+100%",
        SUM(Fields!ForecastMovementCurrentPeriod.Value) <> 0 AND (SUM(Fields!ActualMovementCurrentPeriod.Value) - SUM(Fields!ForecastMovementCurrentPeriod.Value) <> 0.99123131414) AND 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 AND (SUM(Fields!ActualMovementCurrentPeriod.Value) - SUM(Fields!ForecastMovementCurrentPeriod.Value) <> 0.9912313141) AND 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 AND (SUM(Fields!ActualMovementCurrentPeriod.Value) - SUM(Fields!ForecastMovementCurrentPeriod.Value) <> 0.9912313141) AND (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 AND 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"
    )

我仍然觉得奇怪的是,一旦找到第一个真值,SWITCH就不会终止对整个块的评估 - 这对我来说似乎是浪费计算。

无论如何,谢谢你们!

1 个答案:

答案 0 :(得分:0)

我认为你的上一个switch语句在错误的地方有一个括号。如果我将其分解,我就会看到这一点:

    SUM(cpv) <> 0 
    AND (SUM(mcp) - SUM(cpv) <> 0.9912313141)
    AND 
    (
        (ABS(SUM(mcp)) - ABS(SUM(cpv))) / ABS(SUM(cpv)) > 0.20 
        AND (ABS(SUM(mcp)) - ABS(SUM(cpv))) / ABS(SUM(cpv))  -- This is not valid calculation
    ) < 1.00,
      (SUM(mcp) - SUM(cpv)) / SUM(cpv)

第六行不是有效的计算,因为你太早关闭了parens。你需要把之后的最后一个parens 放在&#34;&lt; 1.00&#34 ;.像这样:

    SUM(cpv) <> 0 
    AND (SUM(mcp) - SUM(cpv) <> 0.9912313141)
    AND 
    (
        (ABS(SUM(mcp)) - ABS(SUM(cpv))) / ABS(SUM(cpv)) > 0.20 
        AND (ABS(SUM(mcp)) - ABS(SUM(cpv))) / ABS(SUM(cpv)) < 1.00
    ), -- close the parens after 
      (SUM(mcp) - SUM(cpv)) / SUM(cpv)

为了进一步简化,我会尝试使用BETWEEN语句,因为它可以消除重复:

    SUM(cpv) <> 0 
    AND (SUM(mcp) - SUM(cpv) <> 0.9912313141)
    AND 
    (
        ((ABS(SUM(mcp)) - ABS(SUM(cpv))) / ABS(SUM(cpv))) between 0.20 and 1.00 
    ),
      (SUM(mcp) - SUM(cpv)) / SUM(cpv)