Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Count appearance of string in a comma-separated data column does not count values after the first co

I have two tables:

 

Products table has a single column with one product per field:

 

 

 

 

 

List table has several columns but the column I'm interested in has fields that can have as little as one product to as much as 17, all separated with a comma and no spaces in between:

 

 

 

 

The issue I'm facing and need help with is to correctly count each product either, ideally as as a measure. I tried two measures but they both gave me an incorrect count of blanks; there are no blank values either table.
What I found is that both measures count only the *first* value they find in the List column and then continue counting subsequent rows:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

How can I correctly count all values in each field from the List table and have zero "blank" counts?

 

Thanks!

  • tamerj1's avatar
    tamerj1
    3 years ago

    Anonymous 
    Please delete or deactivate that relationship or use 

    Customer Products at M0 =
    CALCULATE (
        SUMX (
            VALUES ( Products[Product] ),
            SUMX (
                VALUES ( Query1[M0_LIST] ),
                INT ( CONTAINSSTRING ( Query1[M0_LIST], Products[Product] ) )
            )
        ),
        CROSSFILTER ( Query1[M0_LIST], Products[Product], NONE )
    )

7 Replies

  • tamerj1's avatar
    tamerj1
    Icon for Community Champion rankCommunity Champion

    Hi Anonymous 

    If the same product is repeated multiple times inside the string and you want to consider that in the counting then please use

    Customer Products at M0 =
    SUMX (
        VALUES ( Products[Product] ),
        SUMX (
            VALUES ( Query1[M0_LIST] ),
            VAR String = Query1[M0_LIST]
            VAR Items =
                SUBSTITUTE ( String, ",", "|" )
            VAR Length =
                COALESCE ( PATHLENGTH ( Items ), 1 )
            VAR T1 =
                GENERATESERIES ( 1, Length, 1 )
            VAR T2 =
                SELECTCOLUMNS ( T1, "@Item", PATHITEM ( Items, [Value] ) )
            RETURN
                COUNTROWS ( FILTER ( T2, [@Item] = Products[Product] ) )
        )
    )
    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi tamerj1 , thank you. I tried using the first measure and still gave me the incorrect counts, using the second one gave me this result.

       

       

      The same product is not repeated inside the string, but two clients can definitely have the same products but they would be listed in different rows.

      • tamerj1's avatar
        tamerj1
        Icon for Community Champion rankCommunity Champion

        Anonymous 

        Can you please explain exactly what is the problem?

  • tamerj1's avatar
    tamerj1
    Icon for Community Champion rankCommunity Champion

    Hi Anonymous 

    please try

    Customer Products at M0 =
    SUMX (
    VALUES ( Products[Product] ),
    SUMX (
    VALUES ( Query1[M0_LIST] ),
    INT ( CONTAINSSTRING ( Query1[M0_LIST], Products[Product] ) )
    )
    )