NaN and Infinity values in SSRS - reporting-services

I am using Visual Studio to build my report and I have tried to use those three expressions:
=IIF(Fields!A.Value = 0 or Fields!B.Value = 0, 0,SUM(Fields!A.Value)/SUM(Fields!A.Value)
=IIF(SUM(Fields!A.Value) = 0 or SUM(Fields!B.Value =0),0,SUM(Fields!A.Value)/SUM(Fields!A.Value)
=Replace(Round((((SUM(Fields!A.Value)/SUM(Fields!B.Value)))*100),0),"NaN","0")+"%"
The first 2 give me only 0 as % and the last one doesnt get rid of Infinity but does get rid of NaN.
Can someone please help where I can use both these expressions together and not get only 0% and Infinity when I use either the top 2 or the last one?

At first, you need to be sure that the fields you are using to calculate your value are really numbers.
Normally, you get NaN when trying to divide 0 / 0 or Infinity when you are dividing any number by 0.
So, a generic resolution for NaN, Infinity or even #Error (that happens when a null value is passed to a division) is to create a Function that will work on this values for you.
Right click on the background of your report and go to Report Properties as shown:
Then you can go on tab Code and add your custom code:
Public Function Divide(ByVal dividend As Double, ByVal divisor As Double) As Double
If IsNothing(divisor) Or divisor = 0 Or IsNothing(dividend) Or dividend = 0 Then
Return 0
Else
Return dividend / divisor
End If
End Function
We use IsNothing() to avoid #Error and check whether the divisor or the dividend are 0 to avoid NaN and Infinity.
And then you replace your expression with:
= Code.Divide(Sum(Fields!A.Value), Sum(Fields!B.Value))
To make this looks like a percentage, I strongly recommend you change your Textbox properties. So, right click on it, go on Textbox Properties and over tab Number. Select category Percentage.
By doing this, you make sure that de SSRS will consider this value as percentage and not a text even when it is exported to other platforms such as Excel.
You can also check the Microsoft documentation on adding code to a Report (SSRS)

If you're getting 0% and NaN I'd check check your field data types. You should have at least one non-integer, and both need to be numeric. I'd still expect to see some 1s, but this is a start at the 0% and NaN. In terms of your check, you only need to check the denominator to avoid a calculation error.
Try:
=IIF(Sum(Fields!B.Value) <> 0 , Sum(Fields!A.Value) / Sum(Fields!B.Value), 0 )
Control your presentation (decimal places, % sign etc.) with the textbox Format properties, or the equation number properties.

Related

Function that will not return 0

I am writing a formula which to use as a decay multiplier on a given value.
The problem is the following : I have a window of processing - days lets say 10, this window is computed every day anew. I need to decay a certain parameter with a factor reflecting the days that an id is present. Currently what I do is (previousWinSize-(start of the current window-start of the previous window))/previousWinSize
In this case if my previous window size is 10 and the difference in the days of processing is two (10-2)/10 which gives me 0.8 to multiply my variable by and respectively decay .2 of it.
However if I have a 3 day window and again 2 days of difference (3-2)/3 I get value close to 0 which cuts more than I would like to.
I am looking for a formula that would scale better when the numbers are small and would not produce a huge decay factor.
Thank you in advance.
I recommend making use of a sigmoid function e.g.
You can take the output of your function i.e. returns a number between 0 and 1 based on the difference of days of processing and feed it into the sigmoid. If you set up the a (slope) and b (inflection point) parameters properly you can for example, ensure that the lowest decay multiplier you get is ~0.5 when your original equation returns a number close to 0.
I've graphed the example I stated above here:
https://www.desmos.com/calculator/nqemuexjhg
(This is based on: https://www.desmos.com/calculator/rna4aqta0c)
I think you do have two edge cases with this method though. When your equation returns 0 the sigmoid isn't exactly going to give you 0.5 (which you may not even want to begin with), you'll end up getting something that's close to 0.5. In this scenario what you may start to see is your values drifting if you keep applying the sigmoid. The same is true for when your equation returns 1. After putting it through the sigmoid you won't get 1, you'll get something close to 1.
What I think I'd do in such a scenario is have some sort of check before the sigmoid gets applied
e.g.
if(x == 0)
y = 0;
else if(x == 1)
y = 1;
else
y = sigmoid(x);
Sources / Possible further reading:
https://en.wikipedia.org/wiki/Sigmoid_function

Strip decimals in SSRS expression without rounding

In my report I'm trying to remove the decimals without rounding. I'll be using this to set the minimum value in the vertical axis of the area chart.
So I tried =Format(98.56, "N0"), but this returns 99, which is incorrect. It should return 98.
I've been searching specifically for SSRS, but most of the results are for tsql.
My question: How can I remov decimals in SSRS without rounding?
Thanks
Try using "Floor". It effective rounds down to the nearest integer. You'll find this under the Math functions.
=Floor(Fields!Number.Value)
Note, there's also a Floor function in Transact-SQL that works the same way in case you need to do some of the processing in your SQL script.
Update based on request in comments
If you wanted to achieve the same result after the decimal point, all you need is a little algebra.
=Floor((Fields!Number.Value*10))/10
That should turn 99.46 into 99.4. Given that it shaves off the remainder, you could then tack on any additional zeroes you wanted.
I ended up converting to Int. The following expression in SSRS returns 98:
=Int(98.56)
I know the question is quite old, but as I ended up here having the same question I would like to share my answer:
While FLOOR and CEILING are fine if you take extra measures to handle numbers <0 or know they are always >=0, the easiest way to simply strip off the decimals is to use
=Fix(Fields!Number.Value)
FIX only returns the integer part of a number, without any rounding or transformation. For negative numbers Int rounds up.
Source: Examples for Fix, Floor and Ceiling
Source: Difference between Int and Fix

Microsoft Access - Decimal Scale stuck at 0

I have a calculated field in my table called C. its the result of A-B=C. A & B are number fields (single, fixed). I have having trouble setting up C as a calculated (Decimal Field).
The precision / decimal places seem to work perfectly, I can modify them freely. But no matter what I do to "SCALE". It always seems to return to "0". I need it to be 2 since all my data in my reports are rounding off at the wrong locations giving me hole numbers.
As you can see "scale = 0", no matter what I do to this number. it will always revert to "0". Why is that?
You can’t change the scale in a calculated field, because it takes the values and settings from the calculation.
So the fact of a scale of 0 should not matter. The resulting number if it needs decimal places will (should) have the decimal value. The setting is IGNORED
I mean, if the calculation is:
2 x 3 = 6
Then you get 6.
If you have 4 / 3 = 1.3333
Then, in your case you get:
1.33333333333333
And you WILL get the above EVEN if the scale = 0. So the scale setting is NOT used nor available in a calculated field.
You are certainly free to round, or format the above result. And in fact you could (should) consider using the round() function in the actual calculation. So use something like:
Round([Field1] / [Field2],4)
And you thus get:
1.3333

SSRS Issue: Rounding to nearest .25

I have an SSRS (2008) report that takes in a raw transactions, then groups and totals them. At the "Total" level, I would like to round the final numbers to the nearest .25, however I cannot find a method to do this. According to what I've read, the Round() function in SSRS only rounds to integers. I have found a couple ways to do it in SQL, but the problem is, I want to do all the calculations with the REAL numbers and just round the result so that I don't introduce a significant amount of error from the real numbers. Here's the best SQL solution I could find:
dec(round(number * 4, 0)/4,11,2) as Nearest_Qtr
Anyone know how I could do the equivalent in the actual SSRS report?
Thanks in advance for the help!
You can define a function on the Report code:
Function MyRound(dblFigure As Double, dblMultiplier As Double) As Double
MyRound = Round(dblFigure / dblMultiplier, 0) * dblMultiplier
End Function
And then call it on your report:
=Code.MyRound(value, 0.25)

SSRS: Trouble applying custom expression to Vertical Axis Chart Label

Using visual studio BI dev studio 2008.
I have a chart that has a Y axis of numbers ranging from 0 to about 1500 (Values), an x axis of dates (Category Group). The Y-axis numbers are integers representing minutes.
I want to convert the Y-Axis of minutes to hh:mm form and I thought it would be simple to write a custom function to do so. However, after going to Vertical Axis Properties -> Number -> Custom format, I am finding that the custom expression will not calculate most expressions that I give it.
For example, I have tried
=(Fields!RealRunTimeMin.Value) * 2
=(Fields!RealRunTimeMin.Value) + 1000
But when I go to Preview the report, the y-Axis is in the same range (0 to 1500) rather than displaying 0-3000.
I have also tried
=CInt(Fields!RealRunTimeMin.Value) + 1000
But the chart remains unchanged. The only thing I seem able to do is convert the number to a string.
Any idea on what I am doing wrong? Note: I'm not asking for the logic to format to hh:mm, but rather I am asking why all attempts to maniuplate numbers in SSRS's axis labels seem to be defeating me.
Thanks in advance,
T
Expressions are not supported with this set of functionality. I do realize that the UI suggests that it is, but that is a bug in the current product. You will need to perform the calculations at either the query or dataset level.