I created a function that will determine whether an item should be counted based on the parameters entered. It returns a 1 if the criteria is met and a 0 if it is not. Basically it is similar to the COUNTIF in Excel, but requires an operator ( = > < <= >= <> ). It evaluates one set of criteria, because in Reporting Services, the functions are exectued once for each row in the result set.
The main use for this is in reporting services which does not have a function like this. It is written in VB, and I am wondering if anyone can think of how this might be done more easily. Do any of you have a similar function?
See code below:
Code:
Public Function CountIF(ByVal myExpression As Object, ByVal myOperator As Object, ByVal myCondition As Object) As Integer
Dim shouldCount As Boolean
Dim Count As Integer
Select Case myOperator
Case "="
shouldCount = (myExpression = myCondition)
Case "<"
shouldCount = (myExpression < myCondition)
Case "<="
shouldCount = (myExpression <= myCondition)
Case ">"
shouldCount = (myExpression > myCondition)
Case ">="
shouldCount = (myExpression >= myCondition)
Case "<>"
shouldCount = (myExpression <> myCondition)
End Select
If shouldCount = True Then
Count = 1
Else : Count = 0
End If
Return Count
End Function
This could be called like so (in Reporting Services) and it does work:
=Code.CountIF(Fields!MyField.Value, "<>", 0)