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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Fromit87
Advocate I
Advocate I

Counting rows, that have a duplicate value in one column and either value 2 or 3 in another column

Hi!

 

I have a dataset that has duplicates in column A. When there are duplicates in column A, I like to filter column B by the following condition: if count of text value in column A > 1 (check if duplicate) and Column B value for all duplicated entries is either 1 or 3 specifically, then count each row towards a total.

 

The data looks as follows:

Column AColumn BColumn C (count)
Car120
Car220
Car220
Car320
Car310
Car431
Car411
Car420

 

The expected result here should count only rows with value Car4 in A and values 3 and 1 in B, because there is more than one entry for Car4 and the condition in column B is only met for two out of the three Car4 rows.

 

I already have a calculated column that tells me if column A has duplicate values. But I don't know how to bring it together with the criteria filter from column B.

 

Any help is highly appreciated! Thank you!

1 ACCEPTED SOLUTION
VahidDM
Super User
Super User

Hi @Fromit87 

 

Try this code to add a new column to your table:

Column =
VAR _A =
    FILTER ( 'Table', 'Table'[Column A] = EARLIER ( 'Table'[Column A] ) )
VAR _CA =
    COUNTAX ( _A, [Column A] )
VAR _1 =
    COUNTX (
        FILTER (
            'Table',
            'Table'[Column A] = EARLIER ( 'Table'[Column A] )
                && 'Table'[Column B] = 1
        ),
        [Column B]
    )
VAR _3 =
    COUNTX (
        FILTER (
            'Table',
            'Table'[Column A] = EARLIER ( 'Table'[Column A] )
                && 'Table'[Column B] = 3
        ),
        [Column B]
    )
RETURN
    IF (
        'Table'[Column B] = 1
            || 'Table'[Column B] = 3,
        IF ( _CA > 1 && _1 <> 0 && _3 <> 0, 1, 0 ),
        0
    )

 

Output:

VahidDM_0-1634109162500.png

 

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.

Appreciate your Kudos!!

 

View solution in original post

2 REPLIES 2
VahidDM
Super User
Super User

Hi @Fromit87 

 

Try this code to add a new column to your table:

Column =
VAR _A =
    FILTER ( 'Table', 'Table'[Column A] = EARLIER ( 'Table'[Column A] ) )
VAR _CA =
    COUNTAX ( _A, [Column A] )
VAR _1 =
    COUNTX (
        FILTER (
            'Table',
            'Table'[Column A] = EARLIER ( 'Table'[Column A] )
                && 'Table'[Column B] = 1
        ),
        [Column B]
    )
VAR _3 =
    COUNTX (
        FILTER (
            'Table',
            'Table'[Column A] = EARLIER ( 'Table'[Column A] )
                && 'Table'[Column B] = 3
        ),
        [Column B]
    )
RETURN
    IF (
        'Table'[Column B] = 1
            || 'Table'[Column B] = 3,
        IF ( _CA > 1 && _1 <> 0 && _3 <> 0, 1, 0 ),
        0
    )

 

Output:

VahidDM_0-1634109162500.png

 

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.

Appreciate your Kudos!!

 

Thank you @VahidDM  that works perfectly. It took me a little time to understand what the code is doing, but it makes complete sense to me now.

 

One bonus question, if I may: if column B would be located in a different table than column A (but both tables are in a 1:n relationship), how would that code change? With countx and filter I am only able to operate within one table, or is it possible to use the existing relationship to another table? Thanks!

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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