Forum Discussion

PhotoBiker's avatar
PhotoBiker
Icon for Advocate I rankAdvocate I
8 years ago
Solved

DISTINCTCOUNT with filters

 

Hi,

 

I'm trying to get a DISTINCTCOUNT after I filtered a table. In the table below, I'm only interested in the records where Answer = Yes. Then, I want to count the distinct clients. 

 

 

The number of distinct clients should work in any filter context, i. e.:

Overall: 2 clients (A and C)

 

By year:  2015: 1; 2016: 1; 2017: 1

 

By city: New York: 1, Moscow: 1

 

I tried the following code, but it's not considering the filter context. Instead, I always get the total number of distinct clients, no matter the filter context:

 

Clients who answered Yes:=
CALCULATE (
DISTINCTCOUNT ( Table[Client] ),
FILTER(Table,Table[Answer] = "Yes"
))

 

I'd really appreciate some help here :) 

 

 

 

 

 

 

  • Anonymous's avatar
    Anonymous
    8 years ago

    Hey PhotoBiker,

     

    I tried your code and it worked for me. I got a "Clients who Answered Yes" of 1 for all of the specific filters, and 2 if no filters were selected. Did you create a measure or a calculated column? A measure should produce the proper output while a calculated column would behave the way you are describing.

     

    Thanks,

    Parker

8 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hey PhotoBiker,

     

    I tried your code and it worked for me. I got a "Clients who Answered Yes" of 1 for all of the specific filters, and 2 if no filters were selected. Did you create a measure or a calculated column? A measure should produce the proper output while a calculated column would behave the way you are describing.

     

    Thanks,

    Parker

    • PhotoBiker's avatar
      PhotoBiker
      Icon for Advocate I rankAdvocate I

      Hey Anonymous,

       

      OMG, you just saved my day :) I'm not sure if it's actually funny or embarrassing...

       

      I created a measure, but my data model is fairly complex and there's quite a number of tables that are not related to each other. When I created my measure, I picked the client from an intellisense suggestion - but I chose an unrelated table, that's why the counts were always the same.

       

      Thank you very much for double checking my code, and for confirming that it actually works! Based on that I reviewed my actual code and spotted the error.

       

      Much appreciated!

      • Anonymous's avatar
        Anonymous
        Not applicable

        PhotoBiker

         

        Awesome, glad to hear it! I like to create a Measures Table where all of my measures are attached. They can get chaotic pretty quick

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi

      Can someone highlight the error in my syntax please?

      Stage_2_submitted = calculate(
      DISTINCTCOUNT('Weekly Report Template'[Application Stage])
      ,
      FILTER('Weekly Report Template',[Application Stage]="23 Retention Payment - Application Closed"))


  • gkeune's avatar
    gkeune
    Frequent Visitor

    I use just measures and is still doent work. 

    New count = CALCULATE(DISTINCTCOUNT(Sheet1[name]), FILTER('Calendar', [new] = "new"))
    new = IF( [Last 365 days1]-[Last 90 days1]= 0,"New")
    Last 90 days1 = CALCULATE(SUM(Sheet1[amount ]),DATESBETWEEN(Sheet1[date],MAX([Date])-90,MAX([Date])))
    Last 365 days1 = CALCULATE(SUM(Sheet1[amount ]),DATESBETWEEN(Sheet1[date],MAX([Date])-365,MAX([Date])))
     
    I also combined all in one new statement it still doent work. Hope u can help.