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

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
Anonymous
Not applicable

Counting the number of matching items in another column

Hi!

I'm new to Power BI and I wonder if it is possible to count the number of matching elements between two columns. For instance, I have a table that looks like this:

IDItem1Item2
1BananaApple
2PeachApple
3AppleOrange
4BananaOrange
5OrangeApple

 

The problem is that I want to know how many apples there are in total, how many bananas there are in total, how many peaches there are in total, and so on. I don't know if there is a way to do that dynamically (without having to specify the item's name), and then calculate it all in the same query.

2 ACCEPTED SOLUTIONS
Greg_Deckler
Community Champion
Community Champion

Sure, you should be able to do this although the exact DAX code will vary depending upon what you are specifically trying to accomplish, but as a column calculation, that would look something along the lines of:

 

Column = 
  VAR __table = FILTER(ALL('Table'),[Item1] = EARLIER('Table'[Item1)))
  VAR __table1 = FILTER(ALL('Table'),[Item2] = EARLIER('Table'[Item1))
  RETURN
    COUNTROWS(__table) + COUNTROWS (__table1)

Something along those lines. The above code should give you the total count of the current value of Item1 in your row for both Item1 and Item2 columns assuming I didn't miss a paren or something because I didn't test it.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

Anonymous
Not applicable

Actually, I modified the query a bit by adding the column as well.

Column = 
  VAR __table = FILTER(ALL('Table'[Item1]),[Item1] = EARLIER('Table'[Item1]))
  VAR __table1 = FILTER(ALL('Table'[Item2]),[Item2] = EARLIER('Table'[Item1]))
  RETURN
    COUNTROWS(__table) + COUNTROWS (__table1)

 
Thanks for that, it helped!
Though I've got another question. Is there a way to merge the tables together and calculate a distinct count ?

View solution in original post

2 REPLIES 2
Greg_Deckler
Community Champion
Community Champion

Sure, you should be able to do this although the exact DAX code will vary depending upon what you are specifically trying to accomplish, but as a column calculation, that would look something along the lines of:

 

Column = 
  VAR __table = FILTER(ALL('Table'),[Item1] = EARLIER('Table'[Item1)))
  VAR __table1 = FILTER(ALL('Table'),[Item2] = EARLIER('Table'[Item1))
  RETURN
    COUNTROWS(__table) + COUNTROWS (__table1)

Something along those lines. The above code should give you the total count of the current value of Item1 in your row for both Item1 and Item2 columns assuming I didn't miss a paren or something because I didn't test it.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Actually, I modified the query a bit by adding the column as well.

Column = 
  VAR __table = FILTER(ALL('Table'[Item1]),[Item1] = EARLIER('Table'[Item1]))
  VAR __table1 = FILTER(ALL('Table'[Item2]),[Item2] = EARLIER('Table'[Item1]))
  RETURN
    COUNTROWS(__table) + COUNTROWS (__table1)

 
Thanks for that, it helped!
Though I've got another question. Is there a way to merge the tables together and calculate a distinct count ?

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! It's time to submit your entry.

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.