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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

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
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Carousel June 2024

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

2
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.