Nested iif issue in SSRS - reporting-services

Compilation error for nested iif
=IIf(Fields!no_of_employees.Value = max(Fields!no_of_employees.Value,"DataSet1"),"Lime","Gold",
IIf(Fields!no_of_employees.Value = min(Fields!no_of_employees.Value,"DataSet1"),"Red","Gold"
))
I was trying to implement nested iif.

If you look at your statement, you have passed 4 arguments to the first IIF
To write a normal IIF you would do something like
=IIF(
ConditionA=True,
TrueResult,
FalseResult
)
When writing a nested IIF you would do something like this
=IIF(
ConditionA=True,
TrueResult,
IIF(
ConditionB=True,
TrueResult,
FalseResult
)
)
To fix your code, remove the first instance of "Gold", .
I prefer to use SWITCH() instead of nested IIFs as I think it's easier to read.
If you want to use SWITCH() then you can rewrite your expression as follows.
=SWITCH(
Fields!no_of_employees.Value = max(Fields!no_of_employees.Value,"DataSet1"), "Lime",
Fields!no_of_employees.Value = min(Fields!no_of_employees.Value,"DataSet1"), "Red",
True, "Gold"
)
The final True just acts like an ELSE. You can read more about SWITCH() here .
https://learn.microsoft.com/en-us/sql/reporting-services/report-design/expression-examples-report-builder-and-ssrs?view=sql-server-ver16#DecisionFunctions

Related

IIF statements with Multiple conditions in SSRS Expression

My expression is different than the ones I looked at already. MY code looks like this and I am having a hard time logically reading it. The code works but is part of another larger IIF statement that is used to filter out certain values on a given table column on the report.
I want to be able to "learn" how to read this type of IIF statement as it really does not make sense to me as written.
=IIF(Fields!DMDeptARCFlag.Value=1,
IIF(Fields!CloseFlag.Value=1,"",
IIF(Fields!ARCPaymentRequested.Value=0,"Invalid ARC Payment","")),"")
This expression is called "InvalidPaymentLoaded" and is used in the following expression
=TRIM
(IIF(IsNothing(Reportitems!WTBTIClosure.Value)=0 AND Fields!SrMgmtStatus.Value = 1 AND Fields!DMDeptARCFlag.Value = 1, Reportitems!WTBTIClosure.Value, "") +
IIF(IsNothing(Reportitems!ARCPaymentNotLoaded.Value)=0 AND Fields!DMDeptARCFlag.Value <> 1, Reportitems!ARCPaymentNotLoaded.Value, "") +
IIF(IsNothing(Reportitems!InvalidARCClose.Value) =0, Reportitems!InvalidARCClose.Value , "") +
IIF(IsNothing(Reportitems!BCDPayementNotLoaded.Value)=0, Reportitems!BCDPayementNotLoaded.Value, "") +
IIF(IsNothing(Reportitems!InvalidPaymentLoaded.Value)=0, Reportitems!InvalidPaymentLoaded.Value, "") +
IIF(IsNothing(Reportitems!InvalidMISCClosure.Value)=0, Reportitems!InvalidMISCClosure.Value, "") +
IIF(IsNothing(Reportitems!InvalidMCFClose.Value) =0, Reportitems!InvalidMCFClose.Value , "") +
IIF(IsNothing(Reportitems!InvalidDeductionClose.Value)=0, Reportitems!InvalidDeductionClose.Value, ""))
Your IIF is not that complex if you break it down....
Remember basic syntax for IIF is...
IIF([This is true], [then this], [else this])
So in you expression you have
"If DMDeptARCFlag =1 then do some more work, else return ""
=IIF(
Fields!DMDeptARCFlag.Value=1,
IIF(Fields!CloseFlag.Value=1,"", IIF(Fields!ARCPaymentRequested.Value=0,"Invalid ARC Payment",""))
,"")
If DMDeptARCFlag does equal 1 then we look at the first nested IIF which reads
If CloseFlag = 1 return "", else do some more work
IIF(Fields!CloseFlag.Value=1,"", IIF(Fields!ARCPaymentRequested.Value=0,"Invalid ARC Payment",""))
So if CloseFlag does not equal 1 then we look at the final nested IIF which simple reads
If ARCPaymentRequested = 0 return "Invalid ARC Payment", if not return ""
I think you could simplify the expression though as really you only ever return 1 of 2 values so if you work out all the conditions for returning one value you should be able to get rid of all those nested IIFs
Something like this
=IIF(
Fields!DMDeptARCFlag.Value=1 AND Fields!CloseFlag.Value <> 1 AND Fields!ARCPaymentRequested.Value=0
, "Invalid ARC Payment"
, "")

SSRS Textbox expression modifying

i designed a tablix report, i have a text box called Student-Attendance which dispaly the information below.
Student_Attendance
Sick
Absence
Present
I have tried to use IIF statement in order to show it as S,A,P. Other than "IIF" is there anything i could use in order to get my result.
IIF (Fields!Student_Attendance.value = "Sick", "S" ) and
IIF(Fields!Student_Attendance.value = "Absence" , "A")
IIF Takes 3 arguments
The condition (if field = value)
What to return if true (e.g. "S")
What to return if false - you are missing this
If you want to use IIF then you have to nest the IIFs
=IIF(Fields!Student_Attendance.value = "Sick", "S", IIF(Fields!Student_Attendance.value = "Absence" , "A") )
What might be simpler is SWITCH especially if you have more than a few options, something like this
=SWITCH (
Fields!Student_Attendance.value = "Sick", "S",
Fields!Student_Attendance.value = "Absence", "A",
Fields!Student_Attendance.value = "Present", "P"
)

Case statement equivalent as an SSRS expressions

How could I go about writing the case statement as an SSRS expression so I don't have to modify the SQL source code? I don't understand how to call the field if the conditions are not met,
case when [Location Type] = 'COMMER' and [Contract] = '2)FRA Components' then 'Commercial Units'
when [Location Type] != 'COMMER' and [Contract] = '2)FRA Components' then 'NOn Commercial Units' else [Component Description] end [Component Description]
Any guidance appreciated
SDC's answer should be good but my preference, especially when nesting a couple of IIFs is to use SWITCH() instead.
It looks more like a SQL CASE statement too. Then final True acts like an ELSE.
=SWITCH(
Fields!Location_Type.Value = 'COMMER' and Fields!ContractValue = '2)FRA Components' , 'Commercial Units' ,
Fields!Location_Type.Value <> 'COMMER' and Fields!ContractValue = '2)FRA Components' , 'Non Commercial Units' ,
True, Fields!Component_Description.Value
)
The syntax is basically
=SWITCH(
Test1, ReturnValue1,
Test2, ReturnValue2,
Test3, ReturnValue3,
....
Test999, ReturnValue999
)
SWITCH tests each condition and returns the first that evaluates to True.
So by setting the final expression evaluated to True, if all previous expressions return false then this one will return a value.
A nested if expression should do the trick. I haven't had a chance to test it, but it should look something like this:
=IIF((Fields!Location_Type.Value = "COMMER") AND (Fields!Contract.Value = "2)FRA Components"), "Commercial Units", IIF((Fields!Location_Type.Value <> "COMMER") AND (Fields!Contract.Value = "2)FRA Components"), "Non Commercial", Fields!Component_Description.Value))

SSRS error Index was outside the bounds of the array. (rsRuntimeErrorInExpression)

I am building a report in SSRS and i am getting an the error: Index was outside the bounds of the array (rsRuntimeErrorInExpression).
I have tried different expressions to fix this but I can't make it work. I currently have:
=iif(LEN(Fields!Comment.Value) - LEN(REPLACE(Fields!Comment.Value, ",","")) > 3, Split(Fields!Comment.Value, ",")(3), Nothing)
Could anyone advice me how to solve this?
The error has to do with SSRS trying to evaluate the split expression even if the Iif condition is false.
Instead of the static value of 3 as the array index you can use a nested Iif to return a valid index value like 0, to make split always work.
=iif(
LEN(Fields!Comment.Value) - LEN(REPLACE(Fields!Comment.Value, ",","")) >3
, Split(Fields!Comment.Value, ",")(
iif(
LEN(Fields!Comment.Value) - LEN(REPLACE(Fields!Comment.Value, ",","")) >3
,3
,0
)
)
, Nothing
)

SSRS complex IIf expression

I'm stuck on an expression. Below is programmatically what i want to do, but SSRS is not having it... How can I get this to work?
=IIf(Fields!TransTypeLabel.Value = "Hour", Fields!SalesAmount.Value, IIf(Fields!TransTypeLabel.Value = "Fee" AND Fields!CategoryId.Value = "TIME_FEE", Fields!SalesAmount.Value, 0), 0)
ake the final " , 0" out (but not the end brace). You also have missed .Value (or .Label depending on what you actually want) from the end of some field references. e.g. Fields!TransTypeLabel should. be Fields!TransTypeLabel.Value . Or maybe that was a type and should have read Fields!TransType.Label ?