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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
rjusa
New Member

Counting (frequency) and avoiding double counting due to LEFT JOIN

I have a dataset that represents testing cases for chemicals and I want to count the number of positive tests for each chemical.

 

case_idchemical_identifiedcase_type
24-0001benzeneA
24-0001benzeneB
24-0001tolueneA
24-0001tolueneB
24-0002benzeneA
24-0002tolueneA
24-0003phenol

 

There is a field I'll call "case_type" and case_id to case_type is one-to-many.    Due to the left join when bringing in the case_type table, counting chemicals will double count those records where there is more than one case_type.  I do have a slicer visual and the count work when I'm slicing on a single case_type, but of course breaks if I select multiples, or none at all.

 

My workaround is a new column where I concatenated case_id with chemical_identified, and then in my visual, I distinctcount that column for the true number of positive results.  

 

case_idchemical_identifiedcase_typecase_id_chemical
24-0001benzeneA24-0001benzene
24-0001benzeneB24-0001benzene
24-0001tolueneA24-0001toluene
24-0001tolueneB24-0001toluene
24-0002benzeneA24-0002benzene
24-0002tolueneA24-0002toluene
24-0003phenol24-0003phenol

 

My question is if this pattern is addressed better with a DAX function.  I attempted to create a temporary table to remove or ignore the case_type column but never figured it out and gave up since the new column was working.  I didn't want to pivot it out to new columns as I didn't know how to deal with that with my slicer visual.

 

Sorry for the squished table...the website is telling me it is invalid HTML when I try to set the width to something like 500px.

1 ACCEPTED SOLUTION
danextian
Super User
Super User

Hi @rjusa 

You can address that with a measure containing a tempory table of summarize case_id and chemical identifier and then counting the rows of the summary.

Case count = 
COUNTROWS (
    SUMMARIZE (
        'chemicals',
        'chemicals'[case_id],
        'chemicals'[chemical_identified]
    )
)

You can see in the screenshot below that there is a count for each case type row but at the chemical identifier level, the count is just one. This essentially  returns a unique combination of case_id and chemical_identified like you did with the calc column and then counting how many rows those are.

danextian_0-1733973362784.png

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

1 REPLY 1
danextian
Super User
Super User

Hi @rjusa 

You can address that with a measure containing a tempory table of summarize case_id and chemical identifier and then counting the rows of the summary.

Case count = 
COUNTROWS (
    SUMMARIZE (
        'chemicals',
        'chemicals'[case_id],
        'chemicals'[chemical_identified]
    )
)

You can see in the screenshot below that there is a count for each case type row but at the chemical identifier level, the count is just one. This essentially  returns a unique combination of case_id and chemical_identified like you did with the calc column and then counting how many rows those are.

danextian_0-1733973362784.png

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.