Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago

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

  • Hi,

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

    Regards

    • Anonymous's avatar
      Anonymous
      Not applicable

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

       

  • Hi Anonymous 
    Try this measure:

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

    Table:

     

    Output:

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

    Appreciate your Kudos  !!

    • Greg_Deckler's avatar
      Greg_Deckler
      Icon for Community Champion rankCommunity Champion

      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. 

      • Anonymous's avatar
        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

    • Anonymous's avatar
      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

      • VahidDM's avatar
        VahidDM
        Icon for Super User rankSuper User

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

         

  • Greg_Deckler's avatar
    Greg_Deckler
    Icon for Community Champion rankCommunity Champion

    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.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks Greg_Deckler

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