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
wenners68
Helper II
Helper II

Subquery

Hi,

I want to create a measure that sums sales figures based on customers who bought something this edition, but not last.

The edition is just a book edition and not relevant to a time/date. I would write the following in SQL:

 

SELECT

SUM(a.Total) AS TotalSales

FROM SalesTable a

WHERE a.edition = "2017"

AND a.accountid NOT IN (SELECT accountid FROM SalesTable WHERE a.edition  = "2016")

 

Any idea if this is posibble to do in a Measure?

 

 

4 REPLIES 4
v-chuncz-msft
Community Support
Community Support

@wenners68,

 

You may take a good look at the article below.

https://www.sqlbi.com/articles/from-sql-to-dax-in-and-exists/

Community Support Team _ Sam Zha
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
ricardocamargos
Continued Contributor
Continued Contributor

Hi @wenners68,

 

Try this code:

 

_2017 =
VAR _2016 = SELECTCOLUMNS(FILTER(SalesTable; SalesTable[EDITION] = 2016); "ACCOUNTID"; SalesTable[ACCOUNT_ID])
VAR _2017 = SELECTCOLUMNS(FILTER(SalesTable; SalesTable[EDITION] = 2017); "ACCOUNTID"; SalesTable[ACCOUNT_ID])
VAR _tbl = EXCEPT(_2017; _2016)
RETURN
SUMX(FILTER(SalesTable; SalesTable[ACCOUNT_ID] IN (_tbl)); SalesTable[TOTAL])

Hi,

thanks, but that will only give you a total for the entire edition.

Greg_Deckler
Super User
Super User

Perhaps create a calculated column first that goes something like:

 

 

BoughtLastYear = IF(ISBLANK(SUMX(FILTER(TotalSales,TotalSales[accountid]=EARLIER(TotalSales[accountid])&&TotalSales[edition]=EARLIER(TotalSales[edition]-1)),TotalSales[Total)),"No","Yes")

Should create a column with Yes if they bought last year and No if they didn't. Then you could create a Measure like:

 

 

 

MyMeasure = CALCULATE(SUM(TotalSales[Total]),FILTER(TotalSales,BoughtLastYear="Yes"))

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.

Top Solution Authors