IIF statements with Multiple conditions in SSRS Expression - reporting-services

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"
, "")

Related

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"
)

Iff statement in Access report with possible more than 1 true condition

I want to make a report that could return several conditions. Negative, Positive, Cancelled, Negative/Dilute, or Positive/Dilute. I wrote a IIF statement with several conditions.
=IIf([Negative]="1","NEGATIVE RESULT",
IIf([Positive]="1","POSITIVE RESULT",
IIf([Cancelled]="1","CANCELLED TEST",
IIf([Negative]="1" And [Dilute]="1","NEGATIVE/DILUTE RESULT",
IIf([Positive]=”1” And [Dilute]=”1”, POSITIVE/DILUTE
)
)
)
)
)
The first part, the middle, and the end will all work alone as single statements, but it will not work when all together.
What am I doing wrong? Any suggestions?
You could use a mixture of Switch and IIf functions:
=Switch(
[Negative]="1", "NEGATIVE" & IIf([Dilute]="1", "/DILUTE", "") & " RESULT",
[Positive]="1", "POSITIVE" & IIf([Dilute]="1", "/DILUTE", "") & " RESULT",
[Cancelled]="1", "CANCELLED TEST"
)

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 ?

Report Designer - Nest IIF inside a Sum with multiple datasets

While there are many code samples for the individual pieces of this question, as a neophyte with Report Designer, I cannot get the syntax for this correct.
I have a textfield that I want to display a SUM of "Held Hours" from one of three DataSets, and this is determined by another field in that same DataSet having a value of "H."
So taking it in pieces, this works to sum ALL hours in ReportDataset.
=Sum((Fields!RegHrs.Value + Fields!OvtHrs.Value),"ReportDataset")
Now, somehow i need to nest that in an IIF. I need that SUM expression to be the "True" return from the IIF when the BillStatus = "H."
IIF(Fields!BillStatus.Value = "H",true,false)
I cannot seem to figure out how to combine the IIF and the SUM so that they pull from "ReportDataset" and are syntactically correct.
I've been trying various permutations of this:
=Sum(IIF(Fields!BillStatus.Value = "H",((Fields!RegHrs.Value + Fields!OvtHrs.Value),"ReportDataset"),0))
Any pointers?
New Info:
The following reports as syntactically correct, but gives me 0 for a return value.
=Sum(IIF(Fields!BillStatus.Value = "H", (Fields!RegHrs.Value + Fields!OvtHrs.Value),0),"ReportDataset")
There should be hundreds of hours returned.
Try a nested sum:
=Sum(IIF(Fields!BillStatus.Value = "H", Sum(Fields!RegHrs.Value + Fields!OvtHrs.Value,"ReportDataset"),0),"ReportDataset")
Or:
=IIF(Fields!BillStatus.Value = "H", Sum(Fields!RegHrs.Value + Fields!OvtHrs.Value,"ReportDataset"),0)
Can you do it in your dataset using SQL? If so, just use the following:
SELECT CASE WHEN BillStatus = 'H' THEN RegHrs + OvtHrs END AS HeldHours
FROM MyTable
Then all you need to do is sum this field:
=Sum(Fields!HeldHours.Value, "ReportDataSet")

Replace #Error or NAN with -(hyphen) in SSRS

When i get a value into the text box (say "NAN" or "#Err") i want to replace it with -(hyphen).
How to achieve this in SSRS reporting ?
Thanks in advance.
You'll need to write an expression to handle it. Ideally, you would have some custom code function in your report (ie. Code.SafeDivision())
=iif(Fields!denominator.Value = 0, "-", Numerator / Denominator)
To elaborate on cmotley's answer, add the following as custom code to your report (to do this go to report properties, then code and then paste this in):
Function SafeDivide(value1 As Decimal, value2 As Decimal) As Decimal
If (value1 <> 0.0 And value2 <> 0.0) Then
Return value1 / value2
Else
Return 0
End If
End Function
Then you can use this in your textboxes to divide without receiving not a number and/or error, for example, adding the following as an expression (changing textbox names as required):
=Code.SafeDivide(ReportItems!Textbox186.Value, ReportItems!Textbox184.Value)
Divides two textboxes using our function.
To show a dash you can simply change the formatting of your textbox to 'number' and tick the box indicating that you'd like to replace '0' with '-'.
Alternatively if for some reason 'NaN' is coming through explicitly from a datasource (rare but not impossible when referencing a loaded table in SharePoint) you could use the following:
Function evalutator(value as String) as String
Select Case value
Case "NaN"
Return "-"
Case "NAN"
Return "-"
End Select
Return value
End Function
What about:
=IIF(Fields!yourcolumnname.Value = 0, "-", Fields!yourcolumnname.Value*1)
Or if you wanted zero's where you get NAN/#ERROR then you could use:
=IIF(Fields!yourcolumnname.Value = 0, Fields!yourcolumnname.Value*1, Fields!yourcolumnname.Value)