How can I eliminate #error in SSRS expression when subtracting by field that may or may not be null?

4.2k views Asked by At

I have several fields in my report, but this question is related to two specific fields (Field2 and Field3).

  • Field2 will always be populated with numeric.
  • Field3 can be null, 0, or other numeric.

I need to subtract Field3 from Field2 to come up with figure for new Field. My problem that I can't seem to figure out is that I get #Error in my new field when Field3 is Null. Below is the formula I have and get the error with when Field3 is Null.

=switch(isnothing(Fields!FIELD3.Value) = 1, "" ,Fields!FIELD3.Value = 0,"" , Fields!FIELD3.Value <> 0, Fields!FIELD3.Value - Fields!FIELD2.Value)

Below is sample data of Field2 and Field3 data and the formula field (Field4) results as well as what I want the results to actually be. I tried to attach actual image, but since new to this site don't have the level allowed to post images.

FIELD2    FIELD3    FIELD4             FIELD4_DESIRED_RESULTS
85.96     NULL      #Error             -
1428.85   476.28    -952.57            -952.57
500.00    600.00    100.00             100.00 
2

There are 2 answers

2
Mike On

Reporting Services evaluates null as "". The expression below should work:

=IIF(Fields!FIELD3.Value = "",0,Fields!FIELD3.Value) - Fields!FIELD2.Value
0
Barry On

Well, I'm not sure about NULL being an empty string, but this works for me

=IIF(IsNothing(Fields!FIELD3.Value),0,Fields!FIELD3.Value) - 
 IIF(IsNothing(Fields!FIELD2.Value),0,Fields!FIELD2.Value)

I know IsNothing works and it should be applied to all fields in the calculation unless you are absolutely certain that a field can never be NULL.