简体   繁体   中英

SSRS expression multiple conditions

I'm trying to build a SSRS expression on a tablix. Its using two Parameters: Product which has {A,B,C,D} as products and Qtr as quarters 1,2,3,4 and Amounts as 1000,2000,3000,4000,5000,6000,7000. I'm trying to add an extra condition to Product A by saying if the Product is A and qtr is 4 then the Amt is 2000 if not the Amt is 3000. The following expression is giving me #Error in the tablix. I don't know what is wrong.

=iif(Parameters!Product.count=2,1000,iif(Parameters!Product.Value(0)="A" AND Parameters!Qtr.Value(0)=4,2000,iif(Parameters!Product.Value(0)="A",3000,iif(Parameters!Product.Value(0)="B",4000,iif(Parameters!Product.Value(0)="C",5000,iif(Parameters!Product.Value(0)="D",6000,7000)))))) 

This may not be the most elegant solution, but it worked in my test case. I'm not exactly sure why, but the references to Parameters!Product.Value(0) are causing the issue. Doesn't make sense to me, since based on your expression checking for count = 2 it must be a multi-value parameter, which means it cannot be null, so there will always be a Value(0). You can put =Parameters!Product.Value(0) in a text box by itself, but putting it in a conditional causes a problem apparently.

First of all, Switch is much better suited to this task than nested IIf, so that is what I used instead. That doesn't have anything to do with the error, but it is much more readable and easier to understand.

To get around the issue with referencing Parameters!Product.Value(0) within the conditional, I created a comma separated string of the parameter values, and then took just the first character from that string with Left(Join(Parameters!Product.Value, ","), 1) . If the actual values for Product are a variable amount of characters, you would need to modify that to parse the value before the first comma, but since your values are A, B, C, and D this will work. So the full expression would look like this:

=switch(
    Parameters!Product.Count = 2, 1000,
    Left(Join(Parameters!Product.Value, ","), 1) = "A" and Parameters!Qtr.Value = 4, 2000,
    Left(Join(Parameters!Product.Value, ","), 1) = "A", 3000,
    Left(Join(Parameters!Product.Value, ","), 1) = "B", 4000,
    Left(Join(Parameters!Product.Value, ","), 1) = "C", 5000,
    Left(Join(Parameters!Product.Value, ","), 1) = "D", 6000,
    true, 7000
)

instead of implementing with IIF try with the switch statement. which make coding simple and manageable in long-term

example: =switch ( Fields!filestatus.Value="P","lightGreen" ,isnothing(Fields!filestatus.Value),"IndianRed" ,true,"No Color" )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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