Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
bammyd36
Frequent Visitor

Count how many times a value appears in a column when there are more than one value in the rows

Hello Power bi Community,
 

Please is there a measure or DAX in power bi that can be used to count the number of times a specific value appears in a column when there is more than one value in some of the rows?

For example;

 

TABLE XX

DateValue
01/08/2017AA
02/08/2017AA
03/08/2017AB
04/08/2017AA; AB
05/08/2017AC
06/08/2017AB
07/08/2017AA; AC

 

I tried the measure below but I didn't get the desired result

CountValues = CALCULATE ( COUNTROWS ( TableName ); TableName[ColumnName] = “This Value” )

I don't want to unpivot the table because it will mess with the other data.

 

Result

CategoryResult
AA4
AB3
AC2

 

Thank you.

 

 

 

1 ACCEPTED SOLUTION

Hi @bammyd36 
Please refer to the attached sample file containing the same proposed solution but explained in more details.
Initially I assumed that you already have the category table. However, you can create it using the following DAX

1.png

Category = 
DISTINCT (
    SELECTCOLUMNS (
        GENERATE ( 
            VALUES ( 'Table'[Value] ),
            VAR String = 'Table'[Value]
            VAR Items = SUBSTITUTE ( String, "; ", "|" )
            VAR Length = COALESCE ( PATHLENGTH ( Items ), 1 )
            VAR T = GENERATESERIES ( 1, Length, 1 )
            RETURN
                SELECTCOLUMNS ( T, "@Item", PATHITEM ( Items, [Value] ) )
        ),
        "Category", [@Item]
    )
)

Then to obtain the desired result use the same measure proposed in my original reply

2.png

Result = 
SUMX (
    VALUES ( Category[Category] ),
    COUNTROWS (
        FILTER ( 'Table', CONTAINSSTRING ( 'Table'[Value], Category[Category] ) )
    )
)

View solution in original post

5 REPLIES 5
tamerj1
Super User
Super User

Hi @bammyd36 

please try

Result =
SUMX (
VALUES ( Category[Category] ),
COUNTROWS (
FILTER ( 'Table', CONTAINSSTRING ( 'Table'[Value], Category[Category] ) )
)
)

Thank you for your reply.

I want to count the number of times each item in the Value column appears in the Table.

i.e AA appears 4 times, AB appears 3 times and AC appears 2 times.

 

 

Hi @bammyd36 
Please refer to the attached sample file containing the same proposed solution but explained in more details.
Initially I assumed that you already have the category table. However, you can create it using the following DAX

1.png

Category = 
DISTINCT (
    SELECTCOLUMNS (
        GENERATE ( 
            VALUES ( 'Table'[Value] ),
            VAR String = 'Table'[Value]
            VAR Items = SUBSTITUTE ( String, "; ", "|" )
            VAR Length = COALESCE ( PATHLENGTH ( Items ), 1 )
            VAR T = GENERATESERIES ( 1, Length, 1 )
            RETURN
                SELECTCOLUMNS ( T, "@Item", PATHITEM ( Items, [Value] ) )
        ),
        "Category", [@Item]
    )
)

Then to obtain the desired result use the same measure proposed in my original reply

2.png

Result = 
SUMX (
    VALUES ( Category[Category] ),
    COUNTROWS (
        FILTER ( 'Table', CONTAINSSTRING ( 'Table'[Value], Category[Category] ) )
    )
)

youssefabdelwah_0-1697727833204.png

 

When I use the same DAX calculation to make a seperate table and not a temporary visual , i get a sum and not the count . Do you know why perhaps?

 

Hi @youssefabdelwah 

Replace SUMX - VALUES with COUNTROWS

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.