cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Lenn
New Member

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!

10 REPLIES 10
VahidDM
Resident Rockstar
Resident Rockstar

Hi @Lenn 
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 !!

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

VahidDM
Resident Rockstar
Resident Rockstar

Hi @Lenn 

 

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 ✌️ !!

 

@Lenn 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!!!
Check out my External Tool for Power BI Desktop!
Microsoft Hates Greg's Quick Measures
Check out my latest book!

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 Supplier
Solution Supplier

Works fine for me. Perhaps share your file.

Regards

Jos_Woolley
Solution Supplier
Solution Supplier

Hi,

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

Regards

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

Lenn_0-1630483560649.png

 

Greg_Deckler
Super User
Super User

@Lenn 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!!!
Check out my External Tool for Power BI Desktop!
Microsoft Hates Greg's Quick Measures
Check out my latest book!

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
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Power BI October Update 2021.jpg

Power BI Release

Click here to read more about the October 2021 Release!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Teds Dev Camp Oct. 2021 768x460.jpg

Power BI Dev Camp - October 28th, 2021

Mark your calendars and join us for our next Power BI Dev Camp!