Search code examples
crystal-reports

How do I count if a number appears once per group in an array


I'm running a report where I need to count if integers have appeared in an array; however, I can only count them once per group.

Therefore if I'm looking for 3 in the following groups.

Tim  
 1,2  
 1,3,4  
 3  
 3,6,7  

Beth  
 1,3  
 2  
 7,8  

Luke  
 1  
 2,4  
 1  

I would get a count of 2 because it's appeared in two groups despite the fact that it's appeared more than once in the one group.


Solution

  • It's not clear how you're looking for the number 3 inside the array.

    Because of this, I assumed that there's a formula {@ThreeAppearedInArray} that returns True if 3 is found in the array else False.

    Then you should get the desired result like following:

    Create a formula, let's call it {@ThreeAppeared}

    If {@ThreeAppearedInArray} Then
        {yourTable.PersonName}
    Else
        "#notAppeared#"
    

    Then create following formula to show the distinct count:

    // distinct count of the formula {@ThreeAppeared}-1 (to subtract the "#notAppeared#" value)
    DistinctCount ({@ThreeAppeared})-1
    

    EDIT

    According to your comment, the formula should look like following:

    If Instr({staff_sales.upgrades12}, '1') > 0 Then
        {yourTable.PersonName}
    Else
        "#notAppeared#"
    

    It is important that the formula returns the column you used to group the persons.
    Replace {yourTable.PersonName} with the appropriate column.

    Then do a distinct-count on that formula minus 1.