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
abhiram342
Microsoft Employee
Microsoft Employee

Optimized way to get Distinct count metric in DAX

Hi All,

 

I have two Fact Tables Table A and Table B. I want to get distinct count metric from Column which is comoon from both tables. Currenlty, I'm using below code and it's taking Time. Please Suggest optimized way for calculating it

 

Example:

FactA[Column] & FactB[Column]:

 

DistinctCount = DISTINCTCOUNT('FactA'[Column])+DISTINCTCOUNT('FactB'[Column])-CALCULATE(DISTINCTCOUNT('FactA'[Column]),TREATAS(VALUES('FactB'[Column]),'FactA'[Column]))

 

 

Thanks,

Abhiram

3 ACCEPTED SOLUTIONS
Greg_Deckler
Community Champion
Community Champion

@abhiram342 - Be easier to verify if had sample data or data set but maybe try:

DistinctCount =
  COUNTROWS(
    DISTINCT(
      UNION(
        SELECTCOLUMNS('FactA',"Column",[Column]),
        SELECTCOLUMNS('FactB',"Column",[Column])
      )
    )
  )


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

amitchandak
Super User
Super User

@abhiram342 , Try like

countx(distinct(union(all('FactA'[Column]),all('FactB'[Column]))),[Column])

or

countrows(distinct(union(all('FactA'[Column]),all('FactB'[Column]))))

 

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

Anonymous
Not applicable

[Distinct Count] =
COUNTROWS(
    DISTINCT(
        UNION(
            VALUES( FactA[Column] ),
            VALUES( FactB[Column] )
        )
    )
)

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

[Distinct Count] =
COUNTROWS(
    DISTINCT(
        UNION(
            VALUES( FactA[Column] ),
            VALUES( FactB[Column] )
        )
    )
)
amitchandak
Super User
Super User

@abhiram342 , Try like

countx(distinct(union(all('FactA'[Column]),all('FactB'[Column]))),[Column])

or

countrows(distinct(union(all('FactA'[Column]),all('FactB'[Column]))))

 

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Greg_Deckler
Community Champion
Community Champion

@abhiram342 - Be easier to verify if had sample data or data set but maybe try:

DistinctCount =
  COUNTROWS(
    DISTINCT(
      UNION(
        SELECTCOLUMNS('FactA',"Column",[Column]),
        SELECTCOLUMNS('FactB',"Column",[Column])
      )
    )
  )


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

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.