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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
leb1_14
Frequent Visitor

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

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...

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
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

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.