Changing color through expression SSRS - reporting-services

I am trying to change the background color of a row where the value is more than 1 with an expression. I have copied the following code from an earlier thread related to this issue but is not working for me.
= IIF(fields!OpenstaandeTijdInUren.value = >1, "Orange")
When I try to save it I get the following error statement:

instead of "fields" and "value" you need to capitalise the v and f, so the correct expression would be =IIF(Fields!OpenstaandeTijdInUren.Value >=1, "Orange"), although from the wording of your question, you might want to use > instead of >=. SSRS expressions are case sensitive. You should probably also put something in the false side of the check, maybe "Transparent".
=IIF(Fields!OpenstaandeTijdInUren.Value >=1, "Orange","Transparent")

Can you try this -
=IIF(Fields!OpenstaandeTijdInUren.Value > 1, "Orange", "No Color")

Related

How to get around '#Error' when a function in MS Report Builder is evaluating a null value?

I hope my title is clear enough. I'm working in MS Report Builder, using a function that applies a regular expression to a queried value in order to get back a certain substring. The regex works fine, so I'll demonstrate a simpler version here to make this less wordy. Here's the gist of my equation:
=IIF(Len(Fields!CourtLocation.Value) < 1, "none",System.Text.RegularExpressions.Regex.Match(Fields!CourtLocation.Value, "(?:[A-Z]{2,4})").Value))
The main purpose is to get that substring, but I added the IIF so that on those occasions when the CourtLocation.Value is empty (I tried Is Nothing in my expression as well), the function returns "none" rather than "#Error"
I've been looking around for a solution, but nothing has worked; it seems like most other people who talk about this are using a mathematical equation rather than trying to get a string. Can anyone help me get rid of that stupid "#Error" value?
You could try this (untested)
=System.Text.RegularExpressions.Regex.Match(
IIF(
Len(Fields!CourtLocation.Value) < 1,
"none",
Fields!CourtLocation.Value
)
, "(?:[A-Z]{2,4})"
).Value
This way the IIF is performed on the string that you want to pass to the regex function, so it always gets a valid value to process
Iif evaluates both sides, so you can nest two Iif statements to avoid the error.
Did you already read this one?
https://sqldusty.com/2011/08/01/ssrs-expression-iif-statement-divide-by-zero-error/
I'll copy the text into the answer if that solves it for you.

Switch function not returning correct value when referencing "nothing"

having a small issue here.
I have a Switch function in a report as shown below:
=Switch(
Fields!Duration.Value > 0, Round(Fields!Duration.Value / 60),
Fields!LastTime.Value = nothing, "Still Occupied",
Fields!LastTime.Value = Fields!FirstTime.Value, "Passing By"
)
This is for a column that is showing the total "Fields!Duration.Value" in minutes (rounded up), and it is working other than the second line:
If the Last Time value is the same as the First Time value, then it is assumed the object was just passing by and outputs "Passing By" and it does this correctly.
However, if the Last Time value is equal to nothing (it is defined in the column for "Last Time" that if it IsNothing, it is 'nothing', and it should output in this report with "Still Occupied" - which it's not doing. The cell is left blank, as if I have it written as Fields!LastTime.Value = nothing, nothing,
Why is this line of code not working?
Fields!LastTime.Value = nothing, "Still Occupied",
Thank you
You cannot test for Nothing using the = operator. There are two ways for which Nothing can be tested; using the IsNothing inspection function like this IsNothing(Fields!LastTime.Value) = True, or by using the Is operator like this Fields!LastTime.Value Is Nothing.
If these tests do not produce the expected result you may be dealing with a field that is set to something other than NULL, like empty '' or an arbitrary value. You can open the Query Designer on your Dataset properties to run your query and check the results.
You could also be looking at a mismapping with your dataset. Use the Refresh Fields button on your Dataset properties to verify your Field Mappings and then double-check that the name being used in your expression matches.
I found that using an IIF statement instead of a Switch statement worked for me, at least for this specific case:
=IIF(Fields!Duration.Value > 0, Round(Fields!Duration.Value / 60),
IIF(Fields!LastTime.Value = Fields!FirstTime.Value, "Passing by", "Still Occupied"))
Going to try to explain it for future people that might not understand (like me when I eventually forget it - I am very new to this sort of stuff) -
Rather than using the Switch statement I had in place to see if Fields!LastTime.Value is a number, equal to Fields!FirstTime.Value, or nothing, it now just asks if it is a number or if it is equal to Fields!FirstTime.Value, and if neither of those are true, it marks it as "Still Occupied" thus removing the need to reference nothing entirely. Like I said, this is pretty case specific, but you never know.
Thank you for the help #JamieSee and #SuperSimmer 44. Cheers!
Could be that nothing should be surrounded by " "

Conditional concatenation breaks if value is nothing

I'm trying to concatenate a string on the end of a sum, but if the sum is nothing, it breaks. It seems like this is due to SSRS evaluating both conditions of the IIf statement, but I can't figure out how to get around this.
I've got....
=IIf(IsNothing(Sum(Fields!Work.Value)), "", Sum(Fields!Work.Value).ToString + " J")
Which will print out the work summary + " J" if there is one, and #Error if not. What's the SSRS workaround?
Update / Clarification
The report in question is grouping on dates and then summing up Work, so it's not the case that Work is null, per se, but that for this particular date for this particular user, there are no rows in the group.. So, there are no rows to sum up in the error causing instance.
Sample Data Set
Name Date Work
Andy 12/1/15 511.30
Andy 12/1/15 549.70
Drew 12/2/15 484.80
Drew 12/2/15 322.36
Sample Report (current)
Name 12/1/15 12/2/15
Andy 1061 J #Error
Drew #Error 807.16 J
Sample Report (expected)
Name 12/1/15 12/2/15
Andy 1061 J
Drew 807.16 J
Have you considered doing the two parts of your desired output in two different expressions in the same Cell?
I assume for the current structure you have used a matrix, with Rows for the Name, and Columns for the date. You can the set the Data to be Sum of Work, as shown here, and in the red text in the image below.
=Sum(Fields!Work.Value)
You then then right click the cell and select "Create Placeholder" to insert a second expression in the same cell.
Set the value of this expression to be as shown here, and in the blue text below
=iif(Sum(Fields!Work.Value) > 0, " J", "")
Then when the report is run, it will always show the Sum if there is one, and if the value of the Sum is greater than zero, it will also display the J as required.
Hopefully this is the behaviour you require. Please let me know if you require further assistance with this solution
Try this:
=IIF(SUM(IIF(Isnothing(Fields!Total.Value),0,Fields!Total.Value)) = 0,
"",
SUM(IIF(Isnothing(Fields!Total.Value),0,Fields!Total.Value)).ToString + " J"
)
Let me know if this can help you
IIF does not short-circuit so this #Error is from SSRS trying to use the ToString function on NULLs.
The workaround is to add another IsNothing check in the false section before using ToString:
=IIF(IsNothing(Sum(Fields!Work.Value))
, ""
, IIF(IsNothing(Sum(Fields!Work.Value)), "", Sum(Fields!Work.Value)).ToString & " J")
To solve this exact problem, because if there is no Work to display then the report should display nothing, instead of using the IIf statement with concatenation, it is simple and sufficient to set a conditional visibility on the cell.
In the cell, use the expression:
Sum(Fields!Work.Value).ToString + " J"
Then for the same cell, select:
Text Box Properties > Visibility > Show or hide based on an expression
and enter:
=IsNothing(Sum(Fields!Work.Value))
While this solves this specific problem, if the solution requires the empty cell to display anything other than blank, then the original IIf short-circuit issue is still an issue.

If field is NOT null, then hide an object (SSRS Expression)

I have a rectangle.
I want to hide it if a field (X) is NOT NULL.
I tried this but it is not working:
=IIF(NOT IsNothing(Fields!filepath.Value), 1, 0)
I get the error:
An error occurred during local report processing.
The Hidden expression used in rectangle 'ID2398' returned a data type that is not valid.
Anyone know why I'm having this issue?
Do I need to place the actual field onto the report? I tried it but I keep getting the same error.
If you're using that for the Visibility expression, I believe you need to explicitly use True/False rather than 1 or 0. So try:
=IIF(NOT IsNothing(Fields!filepath.Value), True, False)
I do prefer to use =iff(Fields!filepath.Value IsNot Nothing, Fields!filepath.Value, ) in Textbox/Properties/Visibility/Hide as it is more intuitive for me.

Change Case in SSRS 2013

I am using a parameter to change the background color of my field when the field string contains the parameter string .
I have used IndexOf, Contains, and instr. All three work, however they are all case sensitive. (i.e. when I search 'Dol' Dollar Tree and Doldrum are highlighted but not Sandolski etc.)
It is not the stored procedure, the correct records display, however the SSRS functions are what is my challenge.
I have tried toLowerInvariant but was receiving an error Help please.
As I was writing this I checked my error and learned the issue..
I assumed the syntax was to pass my string as a parameter:
toLowerInvariant(Parameters!Param1.Value)
However the correct toLowerInvariant syntax is (in ssrs):
Parameters!Param1.Value.toLowerInvariant()
An explaination on toLowerInvariant can be found here: string.ToLower() and string.ToLowerInvariant()
And also I have found that this comparison is best done with a Switch (if you are comparing multiple parameters to the field).
I have not noticed a performance impact between Field.IndexOf(#Param), Field.Contains(#Param), or Field.Instr(#Param)
My final code:
=switch(
instr(Fields!Client.Value.toLowerInvariant(),Parameters!Client_Firm.Value.toLowerInvariant()) >= 1, "Cornsilk",
instr(Fields!Client.Value.toLowerInvariant(),Parameters!KeyWord.Value.toLowerInvariant()) >= 1, "Cornsilk"
)