+ Reply to Thread
Results 1 to 1 of 1

Thread: Is there an easier way to create a countif function?

  1. #1
    I like Data Cubes too... Lauramc has a spectacular aura about Lauramc has a spectacular aura about Lauramc's Avatar
    Join Date
    Mar 2008
    Location
    Far Far Away
    Posts
    387
    Real Name
    Laura
    Rep Power
    5

    Is there an easier way to create a countif function?

    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)
    Last edited by Lauramc; August 7th, 2008 at 07:41 PM.
    "The Enrichment Center is required to remind you that first you will be baked, then there will be cake." - GLaDOS

+ Reply to Thread

Similar Threads

  1. Create History Table / Outcome results
    By Rebelle in forum Database Design Help
    Replies: 11
    Last Post: June 10th, 2009, 11:54 AM
  2. Create ToolTip for GridView Header
    By jmurrayhead in forum .NET Code Samples
    Replies: 0
    Last Post: March 21st, 2008, 03:26 PM
  3. Create Virtual Directories in IIS
    By jmurrayhead in forum Microsoft IIS
    Replies: 0
    Last Post: March 21st, 2008, 11:08 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO