简体   繁体   English

在SSRS 2008 R2中切换条件格式的表达式

[英]Switch expression for conditional formatting in SSRS 2008 R2

I have this conditional formatting expression (for background colour) which works fine: 我有这个条件格式表达式(用于背景色),它可以正常工作:

=SWITCH(
Fields!Fire_Safety.Value = "F", "LimeGreen",
Fields!Fire_Safety.Value = "i", "Red",
Fields!Fire_Safety.Value = "E", "Yellow",
TRUE,Nothing
)

I have now changed the data table (via SQL) so that where the “F” was – there are now dates. 我现在已经更改了数据表(通过SQL),以便“ F”所在的位置–现在有日期。

I changed the Switch expression (in SSRS) to this: 我将Switch表达式(在SSRS中)更改为:

=SWITCH(
Fields!Fire_Safety.Value <= dateadd("d",90,Today()),"Orange",
Fields!Fire_Safety.Value >= dateadd("d",91,Today()),"LimeGreen",
Fields!Fire_Safety.Value  = "i", "Red",
Fields!Fire_Safety.Value = "E", "Yellow",
TRUE,Nothing
)

And it fails. 它失败了。 The date fields seem to work, but not the “i” and the “E”. 日期字段似乎有效,但“ i”和“ E”无效。

I guess it must be the syntax, but I'm a bit lost... Any ideas? 我想它一定是语法,但是我有点迷失了...有什么想法吗?

In a comment you have stated: 在评论中,您说过:

So I guess this means Switch can do text or dates, but not both? 所以我想这意味着Switch可以输入文本或日期,但不能同时输入文本或日期?

No. In the predicate you can use any data type you like, consider this simple example: 否。在谓词中,您可以使用任何喜欢的数据类型,请考虑以下简单示例:

=SWITCH(
    "A" = "B", 1,
    1 = 0, 2,
    TRUE, 3)

The fact that the first expression compares strings, and the second compares integers is irrelevant, all that really matters to the switch function is the result of the predicate, ie the above example is no different to: 第一个表达式比较字符串,第二个表达式比较整数是无关紧要的,对switch函数真正重要的是谓词的结果,即,上面的示例与以下内容没有区别:

=SWITCH(
    FALSE, 1,
    FALSE, 2,
    TRUE, 3)

The actual problem comes from the following expression: 实际问题来自以下表达式:

Fields!Fire_Safety.Value  = "i"

And you would get an error whatever it was embedded in. 无论将其嵌入其中,您都会得到一个错误。

=IF(Fields!Fire_Safety.Value  = "i", 1, 0)

Would give the same error. 会给出同样的错误。

The problem is that since Fields!Fire_Safety.Value is a date, in order to compare a date to a string, the string must be first converted to a date, and "i" cannot be converted to a date, therefore you get an error. 问题在于,由于Fields!Fire_Safety.Value是日期,为了将日期与字符串进行比较,必须首先将字符串转换为日期,并且不能将“ i”转换为日期,因此会出现错误。 If your column only contains dates, the 3rd and 4th conditions can never be true anyway so they may as well be removed. 如果您的列仅包含日期,则第3个条件和第4个条件永远不会成立,因此也可以将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM