cancel
Showing results for
Did you mean:

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Frequent Visitor

Distinctcount on two filter values

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

I have the folowing table:

 Enterprise_Nbr LOB (Line_of_business) 1 A 2 B 3 B 1 B 4 C 5 B 2 B 1 C

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
Super User

Hi @YBavré

``````X =
COUNTROWS (
FILTER (
VALUES ( ENHANCED_CKI_CUSTOMER[Enterprise_Nbr] ),
COUNTROWS (
INTERSECT (
CALCULATETABLE ( VALUES ( ENHANCED_CKI_PRODUCTS[LOB] ) ),
{ "A", "B" }
)
) = 2
)
)``````
11 REPLIES 11
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.

Super User

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

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

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

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

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.

Super User

Hi @YBavré

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

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

Best regards.

Super User

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

Frequent Visitor

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

Super User

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

Frequent Visitor

Yes, that does it, thanks again!

Announcements

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

Power BI Monthly Update - May 2024

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

Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors
Top Kudoed Authors