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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
PowerBIPilgrim
Helper II
Helper II

Struggling with not in subqueries

Hi Team,

 

I have the following SQL Query: 

 

select,count(AccountID)
from Table
where [Date] = '2018-01-13' and Attribute = 1
and AccountID not in (select AccountID
from table
where date = '2017-12-13')

In my PowerBI schema, Date is joined to my Calendar table, so in the DAX I've written this:

measure =

CALCULATE(
count(Table[AccountID]),
FILTER(Table,Table[attribute] = 1),
FILTER(Table, NOT(Table[AccountID] in VALUES(Table[AccountID]) && PREVIOUSMONTH(Calendar[Date])
)))

 

And I'm pulling my hair out trying multiple combinations to replicate the SQL query in DAX, I feel the above comes closest, but I've fundamentally missed something

3 REPLIES 3
v-alq-msft
Community Support
Community Support

Hi, @PowerBIPilgrim 

 

I'd like to suggest you try the following measure to see if it helps.

measure =
VAR tab =
    CALCULATETABLE (
        DISTINCT ( Table[AccountID] ),
        FILTER ( ALL ( Table ), [Date] = DATE ( 2017, 12, 13 ) )
    )
RETURN
    CALCULATE (
        COUNT ( Table[AccountID] ),
        FILTER (
            ALL ( Table ),
            Table[attribute] = 1
                && Table[Date] = DATE ( 2018, 1, 13 )
                && NOT ( Table[AccountID] IN tab )
        )
    )

 

Best Regards

Allan

 

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

amitchandak
Super User
Super User

@PowerBIPilgrim , Try formula like

 

measure =
var _tab = selectcolumn(filter(Table, Table[Date] =date(2017,12,13)),"Account",Table[Account])

CALCULATE(
count(Table[AccountID]),
FILTER(Table,Table[attribute] = 1 && Table[Date] =date(2018,01,13) && Table[Account] in _tab )

)

 

or


measure =
var _tab = selectcolumn(filter(Table, datesmtd(Date[Date])),"Account",Table[Account])

CALCULATE(
count(Table[AccountID]),
FILTER(Table,Table[attribute] = 1 && Table[Account] in _tab )
,datesmtd(dateadd(Date[Date],-1,month))
)

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
AllisonKennedy
Super User
Super User

What are you trying to do in words, rather than SQL query? Looks like you're trying to find the new accounts that weren't in the previous month? Are you wanting this to be dynamic from month to month? The SQL focuses on 1 day only, but your DAX looks at an entire month. 

 

Start with this blog and see if it's close to what you want, then come back with some questions and more info on what does and doesn't work from that blog and what further questions you have: https://blog.enterprisedna.co/new-vs-existing-customers-advanced-analytics-w-dax/

 


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors