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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
YBavré
Frequent Visitor

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.
1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @YBavré 

please try

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

View solution in original post

11 REPLIES 11
speedramps
Super User
Super User

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.

 

 

@speedramps 

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

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?  😀😀😀

 

 

 

speedramps
Super User
Super User

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))
 
speedramps_1-1666173094911.png

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. 😀😀😀

 

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.

tamerj1
Super User
Super User

Hi @YBavré 

please try

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

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

Best regards.

@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] ) )
            )
        )
    )
)

 

Hi @tamerj1 , I get the error message "a table of multiple values was supplied where a single value was expected" when I run your latest measure...

@YBavré 
Sorry I should have used ISEPTY not ISBLANK. Updated in the reply

Yes, that does it, thanks again!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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