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

发布于 2025-01-15 04:02:33 字数 4846 浏览 2 评论 0原文

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

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

计算所有房间中花费的总分钟数的表达式(有效)< /strong>

=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))))

数据表

ACCT#客房服务生效日期服务结束日期
1P14708-Dec-2021 20:1307-Feb-2022 11:44
2P2412022年1月28日 16:412022年2月6日 19:20
3P1472022年1月31日 13:512022年3月4日 10:15
4P2412022年3月6日 23:58

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

尝试#1 基于所有房间的总分钟数的工作表达式。我将 'AND Fields!Room.Value = "P241"' 添加到 IIF 条件中。结果:最终得出所有房间的总分钟数。我得出的结论(正确或错误)该问题可能是因为无法计算不是 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 我的第二次尝试是基于这样的想法:我需要一个“其他”选项才能选择房间。结果:我对逗号和括号进行了三重检查,但始终失败,并出现错误:“textrun 'Textbox275.Paragraphs[0].TextRuns[0]' 的值表达式包含错误:[BC30516] 重载解析失败,因为无法访问' 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,但它真的很讨厌那样。结果:错误:textrun '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)

I am using SSRS 2016 / Report Builder and I am trying to get a nested IIF expression to SUM number of minutes spent in one particular Room (P241). There are some other conditions to deal with as well, which complicate matters.

I am able to successfully sum total minutes for ALL Rooms using the following expression, but I can't seem to come up with an expression to separate out the rooms:

Expression That Calculates Total Minutes Spent in ALL Rooms (works)

=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))))

Data Table

ACCT#RoomService Effective DateService End Date
1P14708-Dec-2021 20:1307-Feb-2022 11:44
2P24128-Jan-2022 16:4106-Feb-2022 19:20
3P14731-Jan-2022 13:5104-Mar-2022 10:15
4P24106-Mar-2022 23:58

Useful info: I have two parameters @EffectiveDateTime and @EffectiveDateTime2 --> the user running the report uses these to specify a beginning and end dates from which to calculate the number of minutes.

Attempt #1
Based on the working expression that totals minutes for all rooms. I added 'AND Fields!Room.Value = "P241"' into the IIF conditions. Result: ended up with total minutes of ALL ROOMS. I concluded (right or wrong) that the issue might be because there was no way to account for rooms that were NOT P241

Expression that calculates total minutes for ALL ROOMS

=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))))

Attempt #2
My second attempt was based on the idea that I needed an "else" option for the room selection to work. RESULT: I triple-checked commas and brackets, but kept failing with ERROR: "The Value expression for the textrun ‘Textbox275.Paragraphs[0].TextRuns[0]’ contains an error: [BC30516] Overload resolution failed because no accessible 'IIf' accepts this number of arguments." The expression below has "" as the else part, but I also tried 0 and Nothing after scouring forums for suggestions

=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)))
            ),""

Attempt 3
Tried rearranging IIF/SUM and it really hated that. Result: ERROR:The Value expression for the textrun ‘Textbox275.Paragraphs[0].TextRuns[0]’ has a scope parameter that is not valid for an aggregate function. The scope parameter must be set to a string constant that is equal to either the name of a containing group, the name of a containing data region, or the name of a dataset.

=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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痕至 2025-01-22 04:02:33

我相信如果您只想对 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 ++)中。这将帮助您审查展示位置。

I believe #3 is the correct approach if you only want to sum values for Room.Value = "P241". Your problem is that your parenthesis are misplaced, starting with the one immediately after your room test.

Below is a slightly reformatted version with adjusted parenthesis placement.

=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

In short, I believe you want to change:

=SUM(<original calculation>)

to:

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

I suggest that you paste this into an editor (such as Notepad++) that supports highlighting of matching parenthesis. That will help you review the placement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文