cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
hcho10
Regular Visitor

DAX Formula

Hello,

 

I need help on writing DAX formulas for the following criterias.

 

I have a set of data as follows.

 

Customer #ProductDateQty
1493719A1/1/20141
1493719A2/1/20141
1493719B5/1/20151
1493719A6/1/20161
1493719C7/1/20171
1493719C8/1/20181
1863579A2/1/20141
1863579C5/1/20151
1863579A6/1/20161
1761625B5/1/20151
1761625B7/1/20171
1761625A8/1/20181

 

Case 1) I would like to create a measure to indicate 1 if a customer bought "A" product within last two years.

Case 2) I would like to create a measure to indicate 1 if a customer did not buy any product within last two years and bought product "A" during the first three years but the third year must be "A"

 

* Last two years would be 2018, 2017 in this data 

* First three years would be 2014, 2015, 2016 in this data

 

If any additional information is needed, please let me know.

 

Thank you for your help.

 

1 REPLY 1
Seward12533
Solution Sage
Solution Sage

 

A in Last 2 years = CALCULATE(DATEDIFF(MAX(Table1[Date]),TODAY(),YEAR),Table1[Product]="A")>0

A not in two years but in last 3 = IF(NOT([A in Last 2 years]),CALCULATE(DATEDIFF(MAX(Table1[Date]),TODAY(),YEAR)<=3,Table1[Product]="A"),FALSE())

 

Note, I probably would not explicitly write the measure to filter for Product = A but rather use a visual filters to limit the product

 

In Last 2 years = DATEDIFF(MAX(Table1[Date]),TODAY(),YEAR)>0
Not in 2 Years but in last 3 = IF(NOT([In Last 2 years]),DATEDIFF(MAX(Table1[Date]),TODAY(),YEAR)<=3,FALSE())

//If you really need to explicit measure then write additional measures A in Last 2 years = CALCULATE([In Last 2 years],Table1[Product]="A")>0 A not in two years but in last 3 = IF(NOT([A in Last 2 years]),CALCULATE([Not in 2 Years but in last 3],Table1[Product]="A"),FALSE())

 

Helpful resources

Announcements
June 2022 update 768X460.jpg

Check it out!

Click here to read more about the June 2022 updates!

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Power BI Dev Camp Session 23 768x460.jpg

Check it Out!

Mark your calendars and join us on Thursday, June 30 at 11a PDT for a great session with Ted Pattison!

June 20 episode 7 with aka link 768x460.jpg

The Power BI Community Show

Join us on June 20 at 11 am PDT when Kim Manis shares the latest on Azure Synapse analytics, the Microsoft Intelligent Data Platform, and notable Power BI Updates from Microsoft Build 2022.

Top Solution Authors