Nested IIF or SWITCH Statement syntax needed corre

2020-02-11 10:15发布

问题:

Can somebody please tell me what I am missing in this formula in SSRS? Or better yet can somebody please write this same thing in a NESTED IIF syntax?

Switch(
    (Parameters!StartMonth.Value <= 1 And Parameters!EndMonth.Value >= 1), 
     (Code.CalculateFraction(
                             (Fields!retail1.Value -Fields!cost1.Value) , Fields!cost1.Value 
                            ) *100
     ), 
    (Parameters!StartMonth.Value <= 2 And Parameters!EndMonth.Value >= 2),
     (Code.CalculateFraction(
                               (
                                 (Fields!retail1.Value +Fields!retail2.Value)- 
                                 (Fields!cost1.Value + Fields!cost2.Value)
                               ) , 
                             (Fields!cost1.Value + Fields!cost2.Value)
                            ) *100
     )
   )

This is seriously driving me crazy. For simplicity I have just put 2 iterations here. I have 12 of these and every next step I have to sum up retail1 until retail12 and cost1 until cost12.

I cant get it right for these two in the first place.

EDIT:

I am trying this now and still returns the value in the first condition

=iif(
        Parameters!StartMonth.Value <= 1 AND Parameters!EndMonth.Value >= 1,
        ReportItems!txtTotal2.Value,
        iif(
        Parameters!StartMonth.Value <= 2 AND Parameters!EndMonth.Value >= 2,
        ReportItems!txtTotal3.Value,
            iif(
            Parameters!StartMonth.Value <= 3 AND Parameters!EndMonth.Value >= 3,
            ReportItems!txtTotal4.Value,
            Nothing
               )
           )
    )

EDIT 2:

FIGURED OUT WHAT WAS INCORRECT.

My entire logic was incorrect to get to the result that I was expecting. It was obvious in my case that whatever I use, be it IIF or switch only the first statement would execute because it was true.

But I had to change the logic to get to the result that I wanted.

iif(
        Parameters!EndMonth.Value = 1,
        ReportItems!txtTotal1.Value, 
                  Parameters!EndMonth.Value = 2,
              ReportItems!txtTotal2.Value,
             and so on
            )

This solved my problem. Thanks guys I appreciate it.

回答1:

Try using separated IIF Statements:

=iif(
    Parameters!StartMonth.Value <= 1 AND Parameters!EndMonth.Value >= 1,
    ReportItems!txtTotal2.Value, Nothing
    ) OR

 iif(
    Parameters!StartMonth.Value <= 2 AND Parameters!EndMonth.Value >= 2,
    ReportItems!txtTotal3.Value, Nothing
    ) OR

 iif(
    Parameters!StartMonth.Value <= 3 AND Parameters!EndMonth.Value >= 3,
    ReportItems!txtTotal4.Value, Nothing
    )


回答2:

Syntax looks correct at first glance. Most places in SSRS you are required to have = to start your statement. Your code above refers to embedded code for CalculateFraction() Does that exist and is it used successfully elsewhere?