You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SSRS使用Switch配合Count按字段条件计数时Lower分支不生效问题

问题原因

你现有表达式的核心问题是行级判断与聚合函数的执行顺序逻辑错误:
Switch的分支判断条件取的是当前表达式作用域内首行的Fields!color.value值,只要首行color为Higher,就会永远执行Higher分支的计数逻辑,Lower分支永远不会被触发,这就是Lower分支不生效的根因。

正确实现方式

方案1:将所有判断逻辑放入聚合函数内部(推荐)

把color的行级判断放到Count的嵌套IIF中,每一行都会独立判断规则后再参与聚合:

=Count(
    IIF(
        Not IsNothing(Fields!ncdrqtrpercent.Value),
        IIF(
            (Fields!color.Value = "Higher" And Fields!ncdrqtrpercent.Value >= Fields!percentile.Value)
            Or (Fields!color.Value = "Lower" And Fields!ncdrqtrpercent.Value <= Fields!percentile.Value),
            1,
            Nothing
        ),
        Nothing
    )
)

方案2:用Sum函数实现(逻辑更直观)

符合条件的行累加1,不符合累加0,最终求和结果等价于计数:

=Sum(
    IIF(
        Not IsNothing(Fields!ncdrqtrpercent.Value),
        IIF(
            (Fields!color.Value = "Higher" And Fields!ncdrqtrpercent.Value >= Fields!percentile.Value)
            Or (Fields!color.Value = "Lower" And Fields!ncdrqtrpercent.Value <= Fields!percentile.Value),
            1,
            0
        ),
        0
    )
)

特殊场景优化

如果你是在按color字段分组的表格/矩阵中分别展示Higher、Lower的计数,可直接在对应分组内写简化表达式,不需要额外判断color值:

  • Higher分组内计数:=Count(IIF(Fields!ncdrqtrpercent.Value >= Fields!percentile.Value,1,Nothing))
  • Lower分组内计数:=Count(IIF(Fields!ncdrqtrpercent.Value <= Fields!percentile.Value,1,Nothing))

内容的提问来源于stack exchange,提问作者bld1777

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 11:00:05