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

Prepping for a Fabric certification exam? Join us for a live prep session with exam experts to learn how to pass the exam. Register now.

Reply
yishenhui
Helper I
Helper I

distinctcount and countx

Hi, 

 

I'd like to have a dax like distinctcount and countx. discount a table/column with filter. 

 

sample table as below left, I have created a measure m1  (m1 = DISTINCTCOUNT(table1[c1])) and it's able to show in the table visual with a filter of c2. I want to show another column with the measure m2 with all distinctcount(table[c1]) without any filter from c2. I try to create one but got the error message.  How can I get it?

 

 

Thanks

 

image.pngimage.pngimage.png

2 ACCEPTED SOLUTIONS
Greg_Deckler
Super User
Super User

@yishenhui Try:

m2 = COUNTROWS( DISTINCT( SELECTCOLUMNS( ALL('table1'), "c1", [c1] ) ) )


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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

@yishenhui Try:

m2 = 
  VAR __c3Table = SELECTECOLUMNS( 'table1', "c3", [c3] )
  VAR __Result = 
    COUNTROWS( 
      DISTINCT( 
        SELECTCOLUMNS( 
          FILTER( ALL('table1'), [c3] IN __c3Table ),
          "c1", [c1] 
        ) 
      ) 
    )
RETURN
  __Result


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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

4 REPLIES 4
Greg_Deckler
Super User
Super User

@yishenhui Try:

m2 = COUNTROWS( DISTINCT( SELECTCOLUMNS( ALL('table1'), "c1", [c1] ) ) )


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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Hi Greg,

 

It did work. I will accept as solution. I also added another column and some data as below. c3 will be a column in the table visual. The second column shows all counts as before. can I have the DAX that show the distcount with filter c3 but without c2? I tried write another DAX M3 but looks like it cannot see [c3].

image.pngimage.png

@yishenhui Try:

m2 = 
  VAR __c3Table = SELECTECOLUMNS( 'table1', "c3", [c3] )
  VAR __Result = 
    COUNTROWS( 
      DISTINCT( 
        SELECTCOLUMNS( 
          FILTER( ALL('table1'), [c3] IN __c3Table ),
          "c1", [c1] 
        ) 
      ) 
    )
RETURN
  __Result


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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Hi Greg,

 

I found another issue. Here assume c3 is always existed in all selection of c2 with is good. But if I change the data a little bit, replaced one "c" to to "b" (hightlighted). Then when I choose the "c" in the slicer, because "c" is nolonger existed with any rows having ca, the result table doesn't have record for ca any more. Is it possible even the selection doesn't exist in that group, the total count still showing. Means in this case, for row ca, m1 will be 0 and m3 will continue show 2?

image.pngimage.png

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

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

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

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

Top Solution Authors