带 SUM 的 SSRS 表达式嵌套 IIF 不起作用

SSRS Expressions Nested IIF with SUM not working

我正在使用 SSRS 2016/Report Builder,我正在尝试获取一个嵌套的 IIF 表达式来求和在一个特定房间 (P241) 中花费的分钟数。还有一些其他条件需要处理,这使事情变得复杂。

我可以使用以下表达式成功计算出所有房间的总分钟数,但我似乎想不出一个分隔房间的表达式:

计算在所有房间花费的总分钟数的表达式(有效)

=Sum(IIF(IsNothing(Fields!ServiceEndDate.Value),      
        IIF((Parameters!EffectiveDateTime.Value <= Fields!EffectiveDateTime.Value), 
              DATEDIFF("n", Fields!EffectiveDateTime.Value, DATEADD("d", 1, Parameters!EffectiveDateTime2.Value)), 
              DATEDIFF("n", Parameters!EffectiveDateTime.Value, DATEADD("d", 1, Parameters!EffectiveDateTime2.Value))), 
        IIF((Parameters!EffectiveDateTime.Value <= Fields!EffectiveDateTime.Value), 
              DATEDIFF("n", Fields!EffectiveDateTime.Value, Fields!ServiceEndDate.Value), 
              DATEDIFF("n", Parameters!EffectiveDateTime.Value, Fields!ServiceEndDate.Value))))

数据Table

ACCT# Room Service Effective Date Service End Date
1 P147 08-Dec-2021 20:13 07-Feb-2022 11:44
2 P241 28-Jan-2022 16:41 06-Feb-2022 19:20
3 P147 31-Jan-2022 13:51 04-Mar-2022 10:15
4 P241 06-Mar-2022 23:58

有用信息:我有两个参数@EffectiveDateTime 和@EffectiveDateTime2 --> 用户运行 报告使用这些参数来指定计算分钟数的开始日期和结束日期。

尝试 #1 基于计算所有房间分钟数的工作表达式。我在 IIF 条件中添加了 'AND Fields!Room.Value = "P241"'。结果:以 ALL ROOMS 的总分钟数结束。我得出结论(对或错)问题可能是因为无法解释不是 P241

的房间

计算所有房间总分钟数的表达式

=Sum(
    IIF(IsNothing(Fields!ServiceEndDate.Value),
        IIF((Parameters!EffectiveDateTime.Value <= Fields!EffectiveDateTime.Value AND Fields!Room.Value = "P241"), 
            DATEDIFF("n", Fields!EffectiveDateTime.Value, DATEADD("d", 1, Parameters!EffectiveDateTime2.Value)), 
            DATEDIFF("n", Parameters!EffectiveDateTime.Value, DATEADD("d", 1, Parameters!EffectiveDateTime2.Value))),
        IIF((Parameters!EffectiveDateTime.Value <= Fields!EffectiveDateTime.Value AND Fields!Room.Value = "P241"), 
            DATEDIFF("n", Fields!EffectiveDateTime.Value, Fields!ServiceEndDate.Value),
            DATEDIFF("n", Parameters!EffectiveDateTime.Value, Fields!ServiceEndDate.Value))))

尝试#2 我的第二次尝试是基于我需要一个“其他”选项来选择房间的想法。结果:我对逗号和括号进行了三重检查,但一直失败并显示错误:“文本运行的值表达式‘Textbox275.Paragraphs[0].TextRuns[0]’包含错误:[BC30516] 重载解析失败,因为没有accessible 'IIf' 接受这个数量的参数。”下面的表达式有 "" 作为 else 部分,但在搜索论坛寻求建议后我也尝试了 0 和 Nothing

=IIF(Fields!Room.Value = "P241"),
    SUM(
        IIF((IsNothing(Fields!ServiceEndDate.Value)),
            IIF((Parameters!EffectiveDateTime.Value <= Fields!EffectiveDateTime.Value),
                DATEDIFF("n", Fields!EffectiveDateTime.Value,DATEADD("d",1,Parameters!EffectiveDateTime2.Value)),
                DATEDIFF("n",Parameters!EffectiveDateTime.Value,DATEADD("d",1,Parameters!EffectiveDateTime2.Value))),
            IIF((Parameters!EffectiveDateTime.Value <= Fields!EffectiveDateTime.Value),
                DATEDIFF("n",Fields!EffectiveDateTime.Value,Fields!ServiceEndDate.Value),
                DATEDIFF("n",Parameters!EffectiveDateTime.Value,Fields!ServiceEndDate.Value)))
            ),""

尝试 3 尝试重新排列 IIF/SUM 但它真的很讨厌。结果:ERROR:The 文本运行“Textbox275.Paragraphs[0].TextRuns[0]”的值表达式具有对聚合函数无效的范围参数。范围参数必须设置为等于包含组名称、包含数据区域名称或数据集名称的字符串常量。

=SUM(
    IIF(Fields!Room.Value = "P241"),
        IIF((IsNothing(Fields!ServiceEndDate.Value)),
            IIF((Parameters!EffectiveDateTime.Value <= Fields!EffectiveDateTime.Value),
                DATEDIFF("n", Fields!EffectiveDateTime.Value,DATEADD("d",1,Parameters!EffectiveDateTime2.Value)),
                DATEDIFF("n",Parameters!EffectiveDateTime.Value,DATEADD("d",1,Parameters!EffectiveDateTime2.Value))),
            IIF((Parameters!EffectiveDateTime.Value <= Fields!EffectiveDateTime.Value),
                DATEDIFF("n",Fields!EffectiveDateTime.Value,Fields!ServiceEndDate.Value),
                DATEDIFF("n",Parameters!EffectiveDateTime.Value,Fields!ServiceEndDate.Value)))
            ),0)

如果您只想对 Room.Value = "P241" 的值求和,我相信 #3 是正确的方法。你的问题是你的括号放错了地方,从你房间测试后的那个开始。

以下是经过稍微调整后的括号位置的版本。

=SUM(
    IIF(Fields!Room.Value = "P241",
        IIF((IsNothing(Fields!ServiceEndDate.Value)),
            IIF((Parameters!EffectiveDateTime.Value <= Fields!EffectiveDateTime.Value),
                DATEDIFF("n", Fields!EffectiveDateTime.Value,DATEADD("d",1,Parameters!EffectiveDateTime2.Value)),
                DATEDIFF("n",Parameters!EffectiveDateTime.Value,DATEADD("d",1,Parameters!EffectiveDateTime2.Value))
            ), -- IIF #3
            IIF((Parameters!EffectiveDateTime.Value <= Fields!EffectiveDateTime.Value),
                DATEDIFF("n",Fields!EffectiveDateTime.Value,Fields!ServiceEndDate.Value),
                DATEDIFF("n",Parameters!EffectiveDateTime.Value,Fields!ServiceEndDate.Value)
            ) -- IIF #4
        ), -- IIF #2
        0
    ) -- IIF #1
) -- Sum

总之,相信你想改变:

=SUM(<original calculation>)

至:

=SUM(IIF(Fields!Room.Value = "P241", <original calculation>, 0))

我建议您将其粘贴到支持高亮显示匹配括号的编辑器(例如Notepad++)中。这将有助于您查看展示位置。