SSRS multiple iif statements rounds instead of keeping 2 decimal calculation - reporting-services

I have the following statement in a calculated field that will only return whole numbers instead of a 2 decimal dollar amount. If I only use one of the 3 iif conditions it works fine but as soon as I add the second or all 3 it will not return a decimal value. Ive also tried all 3 iif conditions separaately and they all work. It appears to be just the OR's causing the issue???
=
(iif ((Fields!loc.Value = "C08") OR (Fields!loc.Value = "C09"), Fields!price1.Value, Fields!price1.Value - Fields!price2.Value) )
OR
(iif ((Fields!loc.Value = "C33") OR (Fields!loc.Value = "C32") OR (Fields!loc.Value = "C10") OR (Fields!loc.Value = "C30"), 0, Fields!price1.Value - Fields!price2.Value) )
OR
(iif (Fields!price1.Value = 0, 0, Fields!price1.Value - Fields!price2.Value))

You can't write and IIF like that, you would need to nest you IIFs which would get a bit messy.
You might find it easier to use a SWITCH statement which is a bit easier to read.
I might not have understood your logic properly but it should be close enough. Note that the first expression that returns true is what will get returned so in thsi case I have assume that if price1 is 0 then the returned value will always be zero, if this is incorrect, just re-order the expression pairs
=SWITCH(
Fields!price1.Value = 0, 0,
Fields!loc.Value = "C08" OR Fields!loc.Value = "C09", Fields!price1.Value,
Fields!loc.Value = "C33" OR Fields!loc.Value = "C32" OR Fields!loc.Value = "C10" OR Fields!loc.Value = "C30", 0,
True, Fields!price1.Value - Fields!price2.Value
)
This reads... "If price1 is zero then return 0" else "if loc is C08 or C09 return price1" else "if loc is C33, C32, C10 or C30 then return 0". Finally, if no previous test return true, then return price1-price2 .
The final True acts like and else
This could be simplified but I wanted to leave it as close to your original attempt as I could.

Related

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.

Access remove line break in string, program tells it's NULL

I have code that does something then you press enter in textfield, problem is when you use Ctrl+Enter, i can capture that event but access tells me in next line that that field is apparently NULL
Private Sub Text5_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Or KeyAscii = 10 Then
If Len(Me.Text5) = 0 Then Exit Sub
If Val(Right(Me.Text5, 1)) > 2 Then Me.Text5 = Left(Me.Text5, Len(Me.Text5) - 1) & "0"
So 'Len' works fine, but the 'Right' function gives out 'Invalid use of null', when i hit debug and check the value it is NULL
I can't figure it out
I guess i need to remove new line characters but how to do that when the text box is null and every function for strings spits out that error
The problem with your check is that Len(Null) is not 0, it's Null.
There are a couple ways to get around this. First, as mentioned in the comments, you can simply add a check for IsNull:
If IsNull(Me.Text5) Or Len(Me.Text5) = 0 Then
The other way you can do this is force it to coalesce by concatenating vbNullString:
If Len(Me.Text5 & vbNullString) = 0 Then
Also you could use Nz and set a return value of your wish in case if the expression is null, in this example also vbNullString and check the result of this function:
If Nz(Me.Text5, vbNullString) = vbNullString Then
or
If Len(Nz(Me.Text5, vbNullString) = 0) Then
or
If Nz(Me.Text5, 0) = 0 Then
or
If Not Nz(Me.Text5, False) Then
For sure you can store the result in a variable first and then check and work with this later on.
Whatever fulfills your needs.
Well, i test it as much as i can and it's just that when you use Ctrl+Enter on a field and capture the key press, field will be null for some reason, i don't see possible way around this

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

Only first pass trough logic gates raising a flag, second deactivating it, others doing nothing

I would like to model a bitarray of flags, where only the first pass through a logical expression sets a flag, but the second pass (computing with the previous state) deactivates the flag and all other passes do nothing to it.
I cannot think of a right combo of the previous state, the right logical expression and a second input (0/1).
bitarray[i] = operand(bitarray[i], value_making_a_change)
Desired value after the 1. pass: bitarray[i] = boolean_value.
Desired value after the second pass: bitarrayp[i] = neg(boolean_value).
Desired value after every consequential pass: bitarrayp[i] = neg(boolean_value).
I think I could just test the value, but my bitarray could get quite large, so I would not want to do that for every index. Is there a standard logic gate/expression I could use?
I tried just using XOR(old_value, 1), but that changes the value back and forth:
value = 0
value = XOR(value, 1) = XOR(0, 1) = 1
value = XOR(value, 1) = XOR(1, 1) = 0
value = XOR(value, 1) = XOR(0, 1) = 1
...

SSRS Row Visibility

I am using the Switch function in Reporting Services to determine the visibility of a row. It happens that I am using more that one column or field to test my expression like so:
=Switch(Parameters!View.Value = "Green" AND Fields!Tax.Value = "N",TRUE,Parameters!View.Value = "Current" AND Fields!PastVal.Value = 0 AND Fields!DatePay.Value = 0 AND Fields!Comment.Value = 0,True)
With the expression above, I want that if the first part is true, the row should be hidden likewise for the second part of the expression, I want to hid a row when all the conditions are met. But this not yielding the desired result.
I equally tried with another expression like so:
=IIF(Parameters!View.Value = "Green" AND Fields!Tax.Value = N",False, IIF(Parameters!View.Value = "Current" AND Fields!PastVal.Value = 0 AND Fields!DatePay.Value = 0 AND Fields!Comment.Value = 0,True,False))
That still did not work.
I anticipate your help. Thank you
If I follow you correctly, I think putting both conditions in an IIF and separating them with an "OR" will work:
=IIF( (Parameters!View.Value = "Green" AND Fields!Tax.Value = "N") OR (Parameters!View.Value = "Current" AND Fields!PastVal.Value = 0 AND Fields!DatePay.Value = 0 AND Fields!Comment.Value = 0) ),True,False)