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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
MhdMur
New Member

How to use (COUNTIF) function in Power BI Desktop between 2 tables

Hi,

I have 2 data tables and each of them have in common a column with a unique code for each row, "customer ID"

I linked the 2 tables using the common unique value "customer ID"

 

My objective is to count which and how many of each unique code "customer ID" in [Table 1] are existing in [Table 2] , and to return zero count if the unique value doesn't exist at all in [Table 2]

 

I've read and watched alot of explinations but non of them to work out for me

 

1 ACCEPTED SOLUTION
v-heq-msft
Community Support
Community Support

Hi @MhdMur ,
Based on your description, and based on my understanding, if there are duplicate values in Table II, I'm not quite sure if you need to count all Table I values that appear in Table II, or just how many unique Table I values appear in Table II i.e. de-duplicated. You can try the following two methods.
Here are my test data:

vheqmsft_0-1705978097826.pngvheqmsft_1-1705978113974.png

1. Calculate contains duplicate values

Count of Customer ID in Table 1 = 
COUNTROWS(
    FILTER(
        'Table1',
        NOT(ISBLANK('Table1'[Table1 customer ID]))
            &&
        CALCULATE(
            COUNTROWS('Table2'),
            FILTER(
                'Table2',
                'Table2'[Table2 customer ID] = 'Table2'[Table2 customer ID]
            )
        ) > 0
    )
)

vheqmsft_2-1705978153429.png

2. Calculate without duplicates

Count = COUNTROWS(
    FILTER(
        'Table1',
        NOT(ISBLANK(
            LOOKUPVALUE(
                'Table2'[Table2 customer ID],
                'Table2'[Table2 customer ID],
                'Table1'[Table1 customer ID]
            )
        )
    )
)
)

vheqmsft_3-1705978171276.png

Best regards

Albert He

 

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

 

 

View solution in original post

3 REPLIES 3
v-heq-msft
Community Support
Community Support

Hi @MhdMur ,
Based on your description, and based on my understanding, if there are duplicate values in Table II, I'm not quite sure if you need to count all Table I values that appear in Table II, or just how many unique Table I values appear in Table II i.e. de-duplicated. You can try the following two methods.
Here are my test data:

vheqmsft_0-1705978097826.pngvheqmsft_1-1705978113974.png

1. Calculate contains duplicate values

Count of Customer ID in Table 1 = 
COUNTROWS(
    FILTER(
        'Table1',
        NOT(ISBLANK('Table1'[Table1 customer ID]))
            &&
        CALCULATE(
            COUNTROWS('Table2'),
            FILTER(
                'Table2',
                'Table2'[Table2 customer ID] = 'Table2'[Table2 customer ID]
            )
        ) > 0
    )
)

vheqmsft_2-1705978153429.png

2. Calculate without duplicates

Count = COUNTROWS(
    FILTER(
        'Table1',
        NOT(ISBLANK(
            LOOKUPVALUE(
                'Table2'[Table2 customer ID],
                'Table2'[Table2 customer ID],
                'Table1'[Table1 customer ID]
            )
        )
    )
)
)

vheqmsft_3-1705978171276.png

Best regards

Albert He

 

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

 

 

MhdMur
New Member

It either turns bakc with blank results or all 0 as shown below , whten trying another sequesnes it gives below error

 

forgive me I'm still a beginner 

 

MhdMur_0-1705907650995.png

 

MhdMur_1-1705907669434.png

 

zenisekd
Super User
Super User

Hi, create a table visual, 
first columumn is Customer ID from table 1 
second dolumn is a measure = count(customer id from table 2) +0

There you go.

Kudos and mark as solution appreciated.

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

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