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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Anonymous
Not applicable

DAX: Distinct count with multiple filters

Hi,

I'm quite new to DAX and I don't seem to get a good formula for the below calculation.

I have many products that were sold over the years.

Now, I want to distinct count the products that were sold > 50 in the year 2021

 

I tried this but didn't work:

=IF(CALCULATE([Volume];'Calendar'[Year]=2021)>50;DISTINCTCOUNT([Product]))

 

Could you help out? Many thanks!

11 REPLIES 11
VahidDM
Super User
Super User

Hi @Anonymous 
Try this measure:

Measure = 
COUNTROWS (
    DISTINCT (
        FILTER (
            FILTER ( 'Table', YEAR ( 'Table'[Date] ) = 2021 ),
            'Table'[Sales (KG)] > 50
        )
    )
)

Table:

VahidDM_0-1630487098477.png

 

Output:

VahidDM_1-1630487116350.png

f this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Appreciate your Kudos VahidDM_2-1630487146220.png !!

Anonymous
Not applicable

Hi @VahidDM 

 

Thanks but it's still not working out for me. Could easily be the fact that I'm new but the tables are different.

So I have a "Calendar" table where I want the year to be 2021.

Than I have a measure 'Sales (kg)' that I want to be > 50

 

Many thanks,

Lenn

Hi @Anonymous 

 

Ii it possible to share sample of your table(s) here? not as a screenshot😎

Or can you share your PBIX file here?(Dropbod, gofile.io, google drive)

 

Appreciate your Kudos ✌️ !!

 

@Anonymous Yep, @VahidDM's solution was the route I was thinking about, COUNTROWS(DISTINCT(FILTER. Could also be written:

Measure =
  COUNTROWS
    DISTINCT(
      SELECTCOLUMNS(
        FILTER( 'Table', YEAR( 'Table'[Date]) = 2021 && [Sales (kg)] > 50),
        "__Product",[Product]
      )
    )
  )

 

The difference is that you are returning distinct products in the above measure versus distinct table rows. Not sure if that matters or not. 


@ 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!:
Mastering Power BI 2nd Edition

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

Hi @Greg_Deckler ,

 

Thanks but it's still not working out for me. Could easily be the fact that I'm new but the tables are different.

So I have a "Calendar" table where I want the year to be 2021.

Than I have a measure 'Sales (kg)' that I want to be > 50

 

Many thanks,

Lenn

Jos_Woolley
Solution Sage
Solution Sage

Works fine for me. Perhaps share your file.

Regards

Jos_Woolley
Solution Sage
Solution Sage

Hi,

Distinct_Vol>50_Year2021 =
CALCULATE (
    DISTINCTCOUNT ( Sales[Product] ),
    'Calendar'[Year] = 2021,
    Sales[Volume] > 50
)

Regards

Great simple & elegant solution. Thank you.

Anonymous
Not applicable

Thanks Jos, however this is not working, please find more info on figure below:

Lenn_0-1630483560649.png

 

Greg_Deckler
Super User
Super User

@Anonymous Perhaps try COUNTROWS(DISTINCT(FILTER('Table',<filters go here>)))

 

If that is not helpful. Sorry, having trouble following, can you post sample data as text and expected output?
Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.


@ 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!:
Mastering Power BI 2nd Edition

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

Thanks @Greg_Deckler

Please find a figure below to help, the yellow cell "4" is what I aim to get:

Lenn_1-1630483619445.png

 

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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

Top Solution Authors