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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
SpencerRTT
Frequent Visitor

Help with Date Difference with Filters

Hi All

 

I'm looking to create a column in my table that calculates the age of a tyre by using DATEDIFF with filters included so it can determine the age of a particular tyre based on the car make and location. A sample dataset would look like one below, but would like the tyre age to calculate number of days since it was last replaced based on its make and location. Ideally i wouldn't use an IF statement because the full dataset includes many car makes amongst other variables.

 

Thanks!

 

CARLOCATIONDATE REPLACEDTYRE AGE
ToyotaFront Left1/06/2015 
FordFront Right3/08/2015 
MazdaBack Left16/08/2015 
ToyotaBack Right5/09/2015 
FordFront Left14/09/2015 
MazdaFront Right16/09/2015 
ToyotaBack Left5/10/2015 
FordBack Right10/10/2015 
MazdaFront Left1/11/2015 
ToyotaFront Right8/11/2015 
FordBack Left12/11/2015 
MazdaBack Right25/12/2015 
ToyotaFront Left1/01/2016 
FordFront Right8/01/2016 
MazdaBack Left15/01/2016 
ToyotaBack Right22/01/2016 
FordFront Left1/02/2016 
MazdaFront Right8/02/2016 
ToyotaBack Left15/02/2016 
FordBack Right22/02/2016 

 

1 ACCEPTED SOLUTION

Hi,

This calculated column formula works

=if(ISBLANK(CALCULATE(MAX(Data[DATE REPLACED]),FILTER(Data,Data[CAR]=EARLIER(Data[CAR])&&Data[LOCATION]=EARLIER(Data[LOCATION])&&Data[DATE REPLACED]<EARLIER(Data[DATE REPLACED])))),today()-Data[DATE REPLACED],Data[DATE REPLACED]-CALCULATE(MAX(Data[DATE REPLACED]),FILTER(Data,Data[CAR]=EARLIER(Data[CAR])&&Data[LOCATION]=EARLIER(Data[LOCATION])&&Data[DATE REPLACED]<EARLIER(Data[DATE REPLACED]))))

Hope this helps.

Untitled.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Hi, 

You can create new column using following DAX

TYRE AGE 1 = DATEDIFF('CAR data'[DATE REPLACED],TODAY(),YEAR)
Here, I have used interval as YEAR you can use day,week,month according to your requirement
 
Hope this works!!
ThanksCare poc.PNG

Hi All, thanks for the replies. Apologies for not being too clear in the question, what i'm looking for is the age of the tyre between replacements. ie, Toyota front left was replaced on 1/06/2015 and then again on 01/01/2016, so the age corresponding to the row on 01/01/2016 will be the date difference between these dates, else an age to the present date.

 

Any ideas?

 

Thanks

 

Spencer.

 

Hi,

This calculated column formula works

=if(ISBLANK(CALCULATE(MAX(Data[DATE REPLACED]),FILTER(Data,Data[CAR]=EARLIER(Data[CAR])&&Data[LOCATION]=EARLIER(Data[LOCATION])&&Data[DATE REPLACED]<EARLIER(Data[DATE REPLACED])))),today()-Data[DATE REPLACED],Data[DATE REPLACED]-CALCULATE(MAX(Data[DATE REPLACED]),FILTER(Data,Data[CAR]=EARLIER(Data[CAR])&&Data[LOCATION]=EARLIER(Data[LOCATION])&&Data[DATE REPLACED]<EARLIER(Data[DATE REPLACED]))))

Hope this helps.

Untitled.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thanks, this worked well. Much appreciated.

TomMartens
Super User
Super User

Hey @SpencerRTT , 

 

I'm wondering if this DAX statement creating a calculated column provides what you are looking for:

Column = DATEDIFF('Table'[DATE REPLACED] , TODAY() , DAY)

As always with calculations around timespans, you have to consider, if the result is correct.

The result of the above formula will return 1 for a tire that has been replaced yesterday, but maybe you want to add 1 as it also could be possible to consider the tire has been used for 2 days already. Sometimes this little difference can become important.

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
amitchandak
Super User
Super User

@SpencerRTT 

Not sure I got it completely

You can get a new Column

 

New column = datediff([	DATE REPLACED],today(),DAY)

 

Or a measure like

sumx(summarize(table,table[CAR],table[LOCATION],"diff", datediff(min(Table[DATE REPLACED]),today(),DAY)),[diff])

 

 

 

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 NL Carousel

Fabric Community Update - February 2025

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