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
Anonymous
Not applicable

Dax - distinctcount as SQL query (no exists)

I need a Dax that count the Names distinctly that has the Flag=1  but don´t has the Flag=0

In SQL I could do something like this:

Select count(distinct Name) Qtd
from Table1 A
where Flag = 1
and not exists (select 1 from Table1 B where A.Name = B.Name and A.Flag =0)


Qtd Names With flag 1 but not 0 = ?

 

NameFlag
Joao1
Joao0
Andre1
Mateus0


The result espected is just Andre, beucause he is the only that has the flag 1 and hasn´t the 0

 

NameFlag
Andre1



4 REPLIES 4
v-jayw-msft
Community Support
Community Support

Hi @souzacaleb

 

Here’s my sample data:

1.PNG

Please check following steps as below:

1. Create calculated columns:

    Column =

    var a = CALCULATE(COUNTROWS('table'),FILTER('table','table'[Name] = EARLIER('table'[Name])))

    var b = CALCULATE(COUNTROWS('table'),FILTER('table','table'[Name] = EARLIER('table'[Name])&&'table'[Flag] = 1))

    return

    a=b

2. Create measure:

    Measure = CALCULATE(DISTINCTCOUNT('table'[Name]),FILTER('table','table'[Column] = TRUE()))

3. Result would be shown as below:

2.PNG3.PNG

 

Best Regards,

Jay

 

Community Support Team _ Jay Wang

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

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.
Anonymous
Not applicable

Hi @v-jayw-msft 
Is there any way achieve the same result without create the columns step? Is it possible to do all in a measure?
I aprecciate for you help. 

Hi @Anonymous ,
 
The following measure might be helpful for you:
Measure 2 = CALCULATE(DISTINCTCOUNT('table'[Name]),FILTER('table',MIN('table'[Flag])>0))
And the result would be shown as below:
4.PNG
 
Best Regards,
Jay
Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.
venal
Memorable Member
Memorable Member

@Anonymous 

small change on the sql query.


select distinct a.Name
from FlagTable a
where a.Flag=1
and not exists(select 1 from FlagTable b where b.Flag=0 and a.Name=b.Name)

 

we will expect someone need to help with DAX.

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.