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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
obriaincian
Resolver I
Resolver I

DAX - Check number of months between 2 dates for each Product

Hi All,

 

 

I am relatively new to PBI and need some help.

 

I have a dataset which is similar to the below

Product             Expiry Date          Date          
Coke        02/02/2023           Jan-21       
Pepsi     23/08/2024          Jan-21         
7up      01/01/2023          Jan-21         
Fanta Jan-21
Coke       02/02/2023           Feb-21          
Pepsi        05/08/2024        Feb-21          
7up       01/01/2023          Feb-21           
Fanta02/04/2023Feb-21
Coke        08/05/2023         Mar-21           
Pepsi        23/08/2024         Mar-21          
7up        27/01/2023       Mar-21             
Fanta02/02/2023Mar-21

 

What I want to do is to create a table that groups each Product and then take the min date from the "Date" column and the max date from the "Date" column  (in this case it would be Jan-21 and Mar-21) and then compare the corresponding Expiry Date rows for each product to see if the expiry date had changed and log the change in a "Changes" column

 

The desired output would be something like this:

Product              Changes
Coke       > 1 month change
Pepsi       No change
7up    <1 month change
FantaNo Expiry Date

 

 

If an expiry date is missing for the min or max date return "No expiry date"

1 ACCEPTED SOLUTION
v-xiaotang
Community Support
Community Support

Hi @obriaincian 

Try this, 

Measure = 
var _minDate= CALCULATE(MIN('Table'[Date]),ALLEXCEPT('Table','Table'[Product]))
var _maxDate= CALCULATE(MAX('Table'[Date]),ALLEXCEPT('Table','Table'[Product]))
var _minED=  CALCULATE(MAX('Table'[Expiry Date]),FILTER( ALLEXCEPT('Table','Table'[Product]),'Table'[Date]=_minDate))
var _maxED=  CALCULATE(MAX('Table'[Expiry Date]),FILTER( ALLEXCEPT('Table','Table'[Product]),'Table'[Date]=_maxDate))
return SWITCH(TRUE(),
ISBLANK(_minED) || ISBLANK(_maxED), "No Expiry Date",
_maxED=_minED,"No change",
_maxED>EOMONTH(_minED,0),"> 1 month change",
_maxED<EOMONTH(_minED,0),"< 1 month change"
)

result

vxiaotang_0-1647844973258.png

 

Best Regards,

Community Support Team _Tang

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

View solution in original post

3 REPLIES 3
v-xiaotang
Community Support
Community Support

Hi @obriaincian 

Try this, 

Measure = 
var _minDate= CALCULATE(MIN('Table'[Date]),ALLEXCEPT('Table','Table'[Product]))
var _maxDate= CALCULATE(MAX('Table'[Date]),ALLEXCEPT('Table','Table'[Product]))
var _minED=  CALCULATE(MAX('Table'[Expiry Date]),FILTER( ALLEXCEPT('Table','Table'[Product]),'Table'[Date]=_minDate))
var _maxED=  CALCULATE(MAX('Table'[Expiry Date]),FILTER( ALLEXCEPT('Table','Table'[Product]),'Table'[Date]=_maxDate))
return SWITCH(TRUE(),
ISBLANK(_minED) || ISBLANK(_maxED), "No Expiry Date",
_maxED=_minED,"No change",
_maxED>EOMONTH(_minED,0),"> 1 month change",
_maxED<EOMONTH(_minED,0),"< 1 month change"
)

result

vxiaotang_0-1647844973258.png

 

Best Regards,

Community Support Team _Tang

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

PaulDBrown
Community Champion
Community Champion

Is the Date field an actual date or is it text?





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






it is a date field

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.