How to change NaN value with 0 in SSRS - sql-server

I've got the below expression and even though I thought I was handling 'NaN' values properly I still get them when I click preview. Does anybody know how to change it, so that NaN values get replaced with 0. Thanks.
=0 + Sum(IIF(IsNothing(Fields!value.Value) = True, 0, Fields!value.Value)) / IIF(Information.IsNothing(Fields!value.Value) = True, 0, CountDistinct(Fields!Introducer.Value))

NaN means that the SSRS couldn't calculate the value. Looking at your expression, looks like it's because you could very easily end up with a divide by 0. If I understand your expression correct, I would do:
IIf(IsNothing(Fields!value.Value) OR (CountDistinct(Fields!Introducer.Value) = 0), 0, Fields!value.Value / CountDistinct(Fields!Introducer.Value))

Related

SSRS: IIF statement to change fill color erroring out

In every example I could find to facilitate this, the logic is precisely what I'm using, but I can't get it to do what I need it to. It appears that I definitely can't use an AND condition inside the IIF expression, but even trying to account for that I don't get the correct results.
Attempt #1:
=iif(Fields!Days_to_Bill.Value >= 5, "Yellow", iif(Fields!Days_to_Bill.Value >= 10, "Red", "Transparent"))
This results in all numbers, including 10 and greater, being Yellow.
Attempt 2:
=iif(Fields!Days_to_Bill.Value >= 5 and < 10, "Yellow", iif(Fields!Days_to_Bill.Value >= 10, "Red", "Transparent"))
This results in the following error:
I really thought this was going to be a super simple expression, but I must be missing something.
For starters, your second IIF() needs to repeat Fields!Days_to_Bill.Value for each condition you're checking.
So you need =iif(Fields!Days_to_Bill.Value >= 5 and Fields!Days_to_Bill.Value < 10...
Second, the first equation is correct and you are getting expected results. In an IIF() statement it will stop at the first 'True' value. Everything in your result set is >= 5, therefore they will all be yellow.
You can avoid the and if you simply change the order of your expression. Simply check for values that are >= 10 first. Also I made a guess at the desired highlighting logic, as I suspect you made some mistakes in your example.
=iif(Fields!Days_to_Bill.Value >= 10, "Transparent", iif(Fields!Days_to_Bill.Value >= 5 "Yellow", "Red"))

Compare and divide in report builder

I have a condition I want to divide two values in report builder with same date but different sample name in the same table...
For example, in this image, I want to divide the CM2(2.85) and Raw Meal(0.58) value. their result should be 4.9.
And if these two parameters (CM2 and Raw Meal) are not on the same date then the value should be empty or nothing. Please help I am new to report builder expression.
I've tried this expression but it does not give me what I need
IIf(InStr(Fields!Sample_Code.Value,"CM2") > 0, Fields!So3.Value, nothing) / IIf(InStr(Fields!Sample_Code.Value,"Raw Meal") > 0, Fields!So3.Value, nothing)
The record will either be CM2 or Raw Meal, but it will never contain both at the same time. If Fields!Sample_Code.Value = "CM2" is true, the second half of the expression will be false, or vice-versa. Fields!Sample_Code.Value can't be two different values at the same time, it can only contain the data from a single record.
Your expression will result in either:
nothing/Fields!So3.Value
or
Fields!So3.Value/nothing.
As a simplified example:
IF( x=1 , 1 , null) / IF( x=2 , 1 , null)
X cannot be two different values at the same time so the expression will never return a non-null result.
You'll need to join CM2 and RAW MEAL records together to evaluate them at the same time. That would probably require a significant change to what you've posted so far.

Need help correcting a divide by zero error

In one of the reports we are getting a divide by zero error. What we want is if the Prior Year value is 0 then Percent Difference should be 100%. I’ve tried all kinds of ways to get this to work with no success.
Below is the script that is causing the problem. Do you know how to correct this?
=Iif(Sum(Fields!LYSUMAMOUNT.Value, "Group3") <> 0, ((Sum(Fields!SUMAMOUNT.Value, "Group3") - Sum(Fields!LYSUMAMOUNT.Value, "Group3")) / Sum(Fields!LYSUMAMOUNT.Value, "Group3")) * 100, 0)
Thank you in advance!
This is a bug in SSRS or the way Microsoft implemented IIF evaluation. It tries to evaluate both true and false expression irrespective of IIF condition. You can try as below to resolve your issue. Here I have added IIF condition to division as well and if it is zero use 1 as divisor.
=Iif(Sum(Fields!LYSUMAMOUNT.Value, "Group3") <> 0, ((Sum(Fields!SUMAMOUNT.Value, "Group3") - Sum(Fields!LYSUMAMOUNT.Value, "Group3")) / Iif(Sum(Fields!LYSUMAMOUNT.Value, "Group3") <> 0, Sum(Fields!LYSUMAMOUNT.Value, "Group3"),1)) * 100, 0)

#Error even after using IIF to avoid division by Zero in Expression

Ran into this issue in SSRS and am hoping for some assistance.
Simply want to return a 0 or 0.00 in a Total textbox when there is a zero in textbox 167 or 168. I have ISNULL covered in the dataset for both fields. Textbox is formatted Currency in Report. I'm getting results if 167 and 168 are both not 0, but #Error if they are. Any thoughts?
=IIF((ReportItems!Textbox167.Value > 0) AND (ReportItems!Textbox168.Value > 0)
,CDEC(ReportItems!Textbox173.Value / ReportItems!Textbox167.Value) -
CDEC(ReportItems!Textbox176.Value / ReportItems!Textbox168.Value), 0)
Thanks!
IIF evaluates both possibilities even IF it doesn't need to - resulting in your error.
The theory is that you want the expression to evaluate normally if the denominator is not zero, but use 0 / 1 if the denominator is zero.
Your expression would look like:
=IIF(ReportItems!Textbox167.Value = 0, 0, CDEC(ReportItems!Textbox173.Value))
/
IIF(ReportItems!Textbox167.Value = 0, 1, CDEC(ReportItems!Textbox167.Value))
-
IIF(ReportItems!Textbox168.Value = 0, 0, CDEC(ReportItems!Textbox176.Value))
/
IIF(ReportItems!Textbox168.Value = 0, 1, CDEC(ReportItems!Textbox168.Value))
The problem here is that IIF is not like the ternary operator in C# as an example, where the conditional expressions are only evaluated depending on the state of the boolean expression.
In other words, this:
=IIF(a, b, c)
Will first evaluate a, b, and c, before IIF is evaluated. The damage has already done by the time IIF gets to look at the values.
Instead you should bake the dividend into an IIF expression to ensure you will never get the 0 in the divide expression at all, instead of trying to avoid using the expression.

SSRS: How to count true rows

I have a report with a dataset that has a column with booleans. In the table footer I want to display x / y where x is how many rows that were true and y is how many rows there was total.
Currently I have this:
=Count(Fields!Transfered.Value).ToString() + " / " + CountRows().ToString()
But the first becomes same as the last part. I then tried this:
=Sum(Fields!Transfered.Value).ToString() + " / " + CountRows().ToString()
But that generates an error, which I guess I can understand. I thought that if I converted the booleans into numbers where true is 1 and false is 0, then it could work out nicely. But how can I do that? Or is it a smarter way to do this all together?
Update: Have now also tried
=Sum(CInt(Fields!Transfered.Value)).ToString() + " / " + CountRows().ToString()
And got a negative result... O.o
Also found a way that worked, which I posted as an answer. But I won't accept it as an answer yet incase someone has a better way to do this =)
I can tell you why things went wrong...
Count(Fields!Transfered.Value) is simply the number of rows. aka CountRows()
Sum(Fields!Transfered.Value) is trying to aggregate "true" or "false" = error
Sum(CInt(Fields!Transfered.Value)) will sum -1 and 0 because VB.NET true = -1
Sum(IIF(Fields!Transfered.Value, 1, 0)) fixes the sign issue = your solution
To avoid the extra IIF, you could use negate the sum of all the -1s
= -Sum(CInt(Fields!Transfered.Value)).ToString() + " / " + CountRows().ToString()
In the end, either solution would be OK and both are equally kludgy
Figured out a way to do it, although there is probably a better more clear and logical way...
=Sum(IIF(Fields!Transfered.Value, 1, 0)).ToString() + " / " + CountRows().ToString()
I came across this today and at least verified that I wasn't the only one that got a negative sum value for bools out of SSRS. Thanks.
My solution was to use ABS for absolute value. I like that better than just negating the expression solely because it's easier to see visually where the '-' might be missed if you're not careful in reading the expression.
but it's essentially the exact same thing
Here is a way where you count on a grouped value
=CountDistinct(IIF(Fields!TrueOrFalseField.Value,
Fields!GroupedField.Value,
"BadValueToBeRemovedFromCount"))
-IIF(CountDistinct(Fields!TrueOrFalseField.Value)=1
and First(Fields!TrueOrFalseField.Value)
, 0, 1)
The second part
-IIF(CountDistinct(Fields!TrueOrFalseField.Value)=1
and First(Fields!TrueOrFalseField.Value)
, 0, 1)
remove 1 from the count if it has True and False value or if there are only one distinct false value.

Resources