Forum Discussion

YBavré's avatar
YBavré
Frequent Visitor
3 years ago
Solved

Distinctcount on two filter values

Hi, I wonder if someone could help me with this issue.

I have the folowing table:

Enterprise_NbrLOB (Line_of_business)
1A
2B
3B
1B
4C
5B
2B
1C

 

I need to count the number of distinct Enterprise_Nbr which have LOB "A" and "B"

 

When I use the formula 

X= CALCULATE(DISTINCTCOUNT(ENHANCED_CKI_CUSTOMER[Enterprise_Nbr]), FILTER(ENHANCED_CKI_PRODUCTS, ENHANCED_CKI_PRODUCTS[LOB] = "A" || ENHANCED_CKI_PRODUCTS[LOB] = "B"))
it returns X = 4 (being Enterprise_Nbrs 1, 2 3 and 5)
 
In fact I expect to get X = 1 (being just Enterprise_Nbr 1, as it is the only one that has LOB "A" and "B"
 
How ca I solve this?
Thanks for the help.
  • Hi YBavré 

    please try

    X =
    COUNTROWS (
        FILTER (
            VALUES ( ENHANCED_CKI_CUSTOMER[Enterprise_Nbr] ),
            COUNTROWS (
                INTERSECT (
                    CALCULATETABLE ( VALUES ( ENHANCED_CKI_PRODUCTS[LOB] ) ),
                    { "A", "B" }
                )
            ) = 2
        )
    )

11 Replies

  • Thanks Tamerj1 for your suggestion, 

     

    In this case

    ISEMPTY('Table') is more efficient than COUNTROWS because it just requires one attempted read for A and one for B.

     

    Whereas COUNTROWS will have to scan all the records for the context.

     

    Thanks.

     

     

    • tamerj1's avatar
      tamerj1
      Community Champion

      speedramps 

      Counting the rows of a table of maximum two rows in not significant 

      • speedramps's avatar
        speedramps
        Super User

        Thanks Tamerj1 

        I dont mind other Super Super offering a better solution but ISEMPTY is the the best practice for cheching if a scenario does or does not exist.

        Please dont assume there is just one record per enterprise and teach newbees to use COUNTROWS.

         

        The clue is the command names !

         

        ISEMPTY is a boolean checks if the table expression is empty

        where as COUNTROWS is computative.

         

        Please use ISEMPTY for checking and COUNTROWS for computations.

         

        No, if, buts or any excuses ... ISEMPTY is the better solution.  OK?  😀😀😀

         

         

         

  • tamerj1's avatar
    tamerj1
    Community Champion

    Hi YBavré 

    please try

    X =
    COUNTROWS (
        FILTER (
            VALUES ( ENHANCED_CKI_CUSTOMER[Enterprise_Nbr] ),
            COUNTROWS (
                INTERSECT (
                    CALCULATETABLE ( VALUES ( ENHANCED_CKI_PRODUCTS[LOB] ) ),
                    { "A", "B" }
                )
            ) = 2
        )
    )
    • YBavré's avatar
      YBavré
      Frequent Visitor

      tamerj1  thnaks a lot: it does the job and I get the resut I was looking for. 

      Best regards.

      • tamerj1's avatar
        tamerj1
        Community Champion

        YBavré 
        As per speedramps recommendation, the following should perform faster.

         

        X =
        COUNTROWS (
            FILTER (
                VALUES ( ENHANCED_CKI_CUSTOMER[Enterprise_Nbr] ),
                ISEMPTY(
                    EXCEPT (
                        { "A", "B" },
                        CALCULATETABLE ( VALUES ( ENHANCED_CKI_PRODUCTS[LOB] ) )
                    )
                )
            )
        )

         

  • Try this ...

     

    Has A and B =
    Var tempA =
    CALCULATE(
        ISEMPTY('Table'),
        'Table'[LOB (Line_of_business)] = "A"
    )

    Var tempB =
    CALCULATE(
        ISEMPTY('Table'),
        'Table'[LOB (Line_of_business)] = "B"
    )

    RETURN
    INT(NOT(tempA || tempB) )
     
     
    Your answer  =
    CALCULATE(
        DISTINCTCOUNT('Table'[Enterprise_Nbr]),
        FILTER('Table',[Has A and B] =1))
     

    Thanks for reaching out for help.

    I put in a lot of effort to help you, now please quickly help me by giving kudos.

    Remember we are unpaid volunteers and here to coach you with Power BI and DAX skills and techniques, not do the users job for them. So please click the thumbs up and accept as solution button. 

     

    One question per ticket please. If you need to extend your request then please raise a new ticket.

    You will get a quicker response and each volunteer solver will get the kudos they deserve. Thank you ! 

     

    Incidentally, next time please DON'T copy & paste your DAX that does not work and expect us to fathom what you want to do. That is crazy. 😀

     

    Please just give a simple non technical functional decscription of what you want, then let us suggest the DAX. Thank you. 😀😀😀

     

    • YBavré's avatar
      YBavré
      Frequent Visitor

      Hi speedramps , thank you for trying helping me with this issue. However, it does not seem to be functionning when I aplpy it on my dataset.

      I'm totally new on this forum, so I do not know yet all the rules, but I keep your tips in mind.