#44 SQL Reporting Blank Row, avg() & min() For Zero Values
Often just setting your formatting to #.## will make sure you display an empty cell (display nothing) for zero value. If you need a different format mask for whatever reason you can use this routine.
Report Props – select Code and drop this vbs function in. It is referenced in a cell via the prefix Code.
i.e. =Code.BlankZeroRow(Fields!FieldName.Value, “datasetName”),4)
Average & Min values can check for numbers less than reporting precision and not count them so as not to report zero values as minimum and skew average values by inflating the count with zero values collected:
=Min(iif(Fields!fieldName.Value > 0.009, Fields!fieldName.Value, Nothing))
=Sum(Fields!fieldName.Value) / IIf(Count(iif(Fields!fieldName.Value > 0.009, 1, Nothing)) = 0, 1, Count(iif(Fields!fieldName.Value > 0.009, 1, Nothing)))
Simpler Average Formula not considering values outside significant precision:
=Avg(iif(IsNumeric(Fields!fieldName.Value), CDbl(Fields!fieldName.Value), Nothing))
The precision check is for both formatting decimal places and counting values less than the presented precision to be zero. The formatting can be set on the cell (Text Box Props) so may not be necessary. You can probably better use the MOD function for the minimal value check but I have not tested that out yet.
Function BlankZeroRow(VALUE as Double, Precision as Integer) Dim sRETURN as string sRETURN = "" ' default blank Select Case Precision ' Test Precision Case 0 If (Value > 0.9) then sRETURN = Format(VALUE,"##0") End If Case 1 If (Value > 0.09) then sRETURN = Format(VALUE,"##0.0") End If Case 2 If (Value > 0.009) then sRETURN = Format(VALUE,"##0.00") End If Case 3 If (Value > 0.0009) then sRETURN = Format(VALUE,"##0.000") End If Case 4 If (Value > 0.00009) then sRETURN = Format(VALUE,"##0.0000") End If Case Else If (Value > 0.009) then sRETURN = Format(VALUE,"##0.00") End If End Select BlankZeroRow = sRETURN End Function
Cell definitions for Min/Max/Average/Sum when receiving nulls:
=Min(Fields!Value_Col14.Value) =Max(Fields!Value_Col14.Value) =Avg(iif(IsNumeric(Fields!Value_Col14.Value), CDbl(Fields!Value_Col14.Value), Nothing)) =Sum(iif(Fields!Value_Col14.Value > 0, CDbl(Fields!Value_Col14.Value), Nothing))