DateDiff negative results to 0 in SSRS - reporting-services

I'm trying to set my results for an expression in SSRS to return 0 if the datediff results is negative. my expression is:
=DateDiff(DateInterval.Minute, Fields!Check_In_Time.Value, Fields!Date___Time.value)
and tried:
=Max(0,(DateDiff(DateInterval.Minute, Fields!Check_In_Time.Value, Fields!Date___Time.value))) --this one errors and doesn't run
but some results are negative, I want it to return a 0 if it's negative. Can anyone help?

This actually worked for me:
=IIF(DateDiff(DateInterval.Minute, Fields!Check_In_Time.Value, Fields!Date___Time.Value) <= 0, nothing, DateDiff(DateInterval.Minute, Fields!Check_In_Time.Value, Fields!Date___Time.Value))

Unfortunately, there's no function to do this in SSRS.
You'd just need to use an IIF to check for it and use 0 if the Date_Time was before the Check_In_Time.
=IIF(Check_In_Time > Date_Time, 0, DateDiff(DateInterval.Minute, Fields!Check_In_Time.Value, Fields!Date___Time.value))

Related

Getting NaN and infinity value in ssrs

Here is my formula
Deviation=(Today- yesterday)/yesterday
Need the value in %
If today is 0, then the value should be -100%(negative deviation)
If yesterday is 0, then the value should be +100%(positive deviation)
Issue:
1.Getting NaN if both today and yesterday are 0. Expected -100%
2.And infinity if yesterday is zero. Expected +100%
Assuming your expression looks like that below
= (Fields!myvalue.Value-Previous(Fields!myvalue.Value)) / Previous(Fields!myvalue.Value)
You can use switch to customize the output based on conditions when current or previous value is 0
Adjust your expression to look like that below
= Switch(
Fields!myvalue.Value=0 AND Previous(Fields!myvalue.Value)=0, -1,
Previous(Fields!myvalue.Value)=0, 1,
True, (Fields!myvalue.Value-Previous(Fields!myvalue.Value)) / Previous(Fields!myvalue.Value)
)

SSRS expression divide by zero in rdl

How should I handle divide by zero error in rdl expression for SSRS 2017?
I have tried the below approaches but nothing works.
1.
=iif((iif((SUM(Fields!Consumption.Value)/
(iif((Fields!daysINDate.Value)<=0,1,Fields!daysINDate.Value))*30)<=0,0,
(SUM(Fields!Consumption.Value)/
(iif((Fields!daysINDate.Value)<=0,1,Fields!daysINDate.Value))*30)))=0,0,
SUM(Fields!ClosingStock.Value)/(iif((SUM(Fields!Consumption.Value)/
(iif((Fields!daysINDate.Value)<=0,1,Fields!daysINDate.Value))*30)<=0,0,
(SUM(Fields!Consumption.Value)/
(iif((Fields!daysINDate.Value)<=0,1,Fields!daysINDate.Value))*30))))*30)
(SUM(Fields!ClosingStock.Value)/
(iif((SUM(Fields!Consumption.Value)/
(iif(Fields!daysINDate.Value<=0,1,Fields!daysINDate.Value))*30)<=0,0,
(SUM(Fields!Consumption.Value)/
(iif(Fields!daysINDate.Value<=0,1,Fields!daysINDate.Value))*30)))*30)
You should only be checking the denominator for zero - not the division operation.
=IIF(Fields!daysINDate.Value = 0, 0, SUM(Fields!Consumption.Value))
/
IIF(Fields!daysINDate.Value = 0, 1, Fields!daysINDate.Value)
*30
SSRS checks both sides of the IIF statement... if either the THEN or ELSE part if the IIF is 0, it will throw up an error.
One way to get around it is to use VB code.
Right click on your report -> report properties -> code and paste the following code :
Public Function NDZ(Byval a As Decimal,Byval b As Decimal, Byval c As Decimal) As Decimal
' Fix for divide by zero problem in VB
' calculates a/b and either returns result or c if b = 0
if b = 0 then
return c
else
return a/b
end if
end function
Now on your report, enter the following expression in your text box you want to perform the calculation :
=code.NDZ(SUM(Fields!Consumption.Value), Fields!daysINDate.Value, 0)
so if either Fields!daysINDate.Value or SUM(Fields!Consumption.Value) is 0, then it will return 0 and not an error.

SSRS - IIF error in Report Builder

I am having an issue with a divide by zero error that I have half way worked through.
Basically I need (EstimatedValuePlanned - EAC) / EstimatedValuePlanned
I have the following, however I am still getting #Error on some:
= IIF (Fields!EstimatedValuePlanned.Value = 0 , 0,
Fields!EstimatedValuePlanned.Value - Fields!EAC.Value)
/
SUM(Fields!EstimatedValuePlanned.Value)
I have changed the code around several times but I still either get Error or NaN
Thank you
IIF will always evaluate both parts of the function, so when SUM(Fields!EstimatedValuePlanned.Value) is zero you will get the error even though that's not what will be returned.
Try using a SWITCH statement. SWITCH stops once an expression returns True.
Something like this
= SWITCH(
SUM(Fields!EstimatedValuePlanned.Value) = 0 , 0,
True, (Fields!EstimatedValuePlanned.Value - Fields!EAC.Value) / SUM(Fields!EstimatedValuePlanned.Value)
)
The True expression simply acts like an else
UPDATE WITH SAMPLE
I created a new report added a dataset and set the dataset query to be the following (just to generate some dummy data).
DECLARE #t TABLE (EVP float, EAC float)
INSERT INTO #t VALUES
(1,2),
(2,3),
(5,4),
(0,2),
(1,0),
(0,0)
SELECT * FROM #t
I then added a table, set the first to columns to be EVP and EAC respectively and set the 3rd column to an expression as follows.
=SWITCH (
Fields!EVP.Value = 0, 0
, True, (Fields!EVP.Value - Fields!EAC.Value) / Fields!EVP.Value
)
The report design looks like this.
And when it's run this is the result....
Try replicating the above steps and see if you can get it to work like this first then review difference to your report.
I got it to work with:
=IIF (Fields!EstimatedValuePlanned.Value = 0 , 0,
Fields!EstimatedValuePlanned.Value - Fields!EAC.Value)
/
IIF(Fields!EstimatedValuePlanned.Value =
0,1,Fields!EstimatedValuePlanned.Value)
Thank you both

SSRS #ERROR Issue

I am getting an #ERROR in SSRS. I believe it's happening when it tries to divide 0 by 0.
How can I change this to handle that scenario.
=Fields!Total_Incidents.Value/Fields!Units_Sold.Value
You can add a function in custom code of the report, ex:
function divide(a as decimal, b as decimal) as decimal
if (b > 0.0) then
divide = a/b
else
divide = 0
end if
end function
Then, call this function in your textbox expression
This was how I ended up doing it
=IIf(Fields!Units_Sold.Value = 0, 0, Fields!Total_Incidents.Value / IIf(Fields!Units_Sold.Value = 0, 1, Fields!Units_Sold.Value))
Check denominator is zero or not,
=IFF(Fields!Units_Sold.Value=0,0,Fields!Total_Incidents.Value/Fields!Units_Sold.Value)

Sum the expression

I need to sum this expression
= IIf(Fields!SupplyStrategyTypeName.Value="Supply",0,
IIf(Fields!IP_DA_FIN.Value>225,Fields!IP_DA_FIN.Value-225,0))
I tried following the example of the previous question but I get an error.
Any help on this?
You didn't list what you tried or the error you got so here's what should work:
=SUM(IIf(Fields!SupplyStrategyTypeName.Value = "Supply", 0,
IIf(Fields!IP_DA_FIN.Value > 225, Fields!IP_DA_FIN.Value - 225, 0)) )
So if a row of data's SupplyStrategyTypeName is Supply or IP_DA_FIN is 225 or less, it would result in 0 otherwise it would be IP_DA_FIN - 225. Then the sum will sum all the results.
Don't be worried about using too many spaces - they make it more readable when you go back through your formulas to figure out what's wrong.