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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
CRT
Frequent Visitor

Datediff by column values

Dear all, I'm quite new to DAX formulas and I've been searching this forum and racking my head how to do a datediff by 1) type of appointment 2) by specific customer:

 

I would like to be able to do a column like the Diff column that finds the difference between two types of appointments (66 and 64 in the column Appt_type) for each specific customer in the column Cust.

 

Appt_type

Cust.

Date

Diff

64

12345

01/02/2019

 

53

12345

03/03/2019

 

49

12345

02/04/2019

 

66

12345

02/05/2019

90

64

32467

29/05/2019

 

53

32467

25/06/2019

 

49

32467

22/07/2019

 

66

32467

18/08/2019

81

64

96784

20/09/2019

 

53

96784

23/10/2019

 

49

96784

25/11/2019

 

66

96784

28/12/2019

99

64

12345

31/12/2019

 

49

12345

03/01/2020

 

66

12345

06/01/2020

9

 

A customer can have several appointments so I need to find the most recent, any help would be greatly appreciated!

Best regards

1 ACCEPTED SOLUTION

I added a rank column and does it

date diff = if(Sheet1[Appt_type]=66,datediff(minx(filter(Sheet1,Sheet1[Appt_type]=64 && Sheet1[Cust.]=EARLIEST(Sheet1[Cust.]) && Sheet1[rank]=EARLIEST(Sheet1[rank])),Sheet1[Date]),minx(filter(Sheet1,Sheet1[Appt_type]=66 && Sheet1[Cust.]=EARLIEST(Sheet1[Cust.]) && Sheet1[rank]=EARLIEST(Sheet1[rank])),Sheet1[Date]),DAY),BLANK())

 

rank = countx(filter(Sheet1,Sheet1[Appt_type]=64 && Sheet1[Date]<=EARLIER(Sheet1[Date])),[Appt_type])

 

https://www.dropbox.com/s/yo0x4s76ww1vfuo/datediff64.pbix?dl=0

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

View solution in original post

9 REPLIES 9
amitchandak
Super User
Super User

try like

 

date diff =datediff(minx(filter(table,table[Cust]=earlier(table[Cust])),table[date]),table[date],day)

or
date diff =
if(maxx(filter(table,table[Cust]=earlier(table[Cust])),table[date])=table[date],
datediff(minx(filter(table,table[Cust]=earlier(table[Cust])),table[date]),table[date],day),blank())

 

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

Thank you very much!

However I can't quite get it to work, I still need to do the difference between the Appt_type value 64 vs 66?

Like this

 

date diff =
if(maxx(filter(table,table[Cust]=earlier(table[Cust]) && [Appt_type]=66),table[date])=table[date],
datediff(minx(filter(table,table[Cust]=earlier(table[Cust]) && [Appt_type]=64),table[date]),table[date],day),blank())

 

Or

date diff = if(Sheet1[Appt_type]=66,datediff(minx(filter(Sheet1,Sheet1[Appt_type]=64 && Sheet1[Cust.]=EARLIEST(Sheet1[Cust.])),Sheet1[Date]),minx(filter(Sheet1,Sheet1[Appt_type]=66 && Sheet1[Cust.]=EARLIEST(Sheet1[Cust.])),Sheet1[Date]),DAY),BLANK())

 

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

This will get you started:

 

Appointment Date Diff = 
--VAR CurrentAppointmentType = MAX('Table'[Appt_type])
VAR CurrentCustomer = MAX('Table'[Customer])
VAR AppointmentCustomerGrouping64 = 
    FILTER(
        ALL('Table'),
        'Table'[Appt_type] = 64 && 'Table'[Customer] = CurrentCustomer
    )
VAR AppointmentCustomerGrouping66 = 
    FILTER(
        ALL('Table'),
        'Table'[Appt_type] = 66 && 'Table'[Customer] = CurrentCustomer
    )
VAR StartDate = 
    CALCULATE(
        MIN('Table'[Date]),
        AppointmentCustomerGrouping64
    )
VAR EndDate = 
    CALCULATE(
        MAX('Table'[Date]),
        AppointmentCustomerGrouping66
    )
VAR DateDifference = 
    DATEDIFF(StartDate,EndDate,DAY)
VAR Type66 = 
    IF(MAX('Table'[Appt_type]) = 66, DateDifference, 0)
RETURN
Type66

 

The problem is this shows the first date for a customer of appt type 64 and compares to the last date for that same customer in appt 66. Your table seems to show you have some other way to group data. For example, you are showing that for customer 12345 at the bottom that type 64 is Dec 31, 2019, and type 66 is Jan 6, 2020. Which is 7 days, not 9. Not sure where 9 comes from.

But the first date for customer 1234 of type 64 isn't Dec 31, 2019, it is Feb 1, 2019, thus the result my measure returns of 339 days.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
CRT
Frequent Visitor

I'm very sorry, the last bit is a manual error, and should be saying 6 days I.e.

 

Appt_typeCust.DateDiff
641234501/02/2019 
531234503/03/2019 
491234502/04/2019 
661234502/05/201990
643246729/05/2019 
533246725/06/2019 
493246722/07/2019 
663246718/08/201981
649678420/09/2019 
539678423/10/2019 
499678425/11/2019 
669678428/12/201999
641234531/12/2019 
491234503/01/2020 
661234506/01/20206

 

The problem is that I have the same customer (12345) repeating the buying process and I am still only interested how many days it takes for him between appt_type 64 and appt_type 66, therefore instead of 339 at the end I need something that looks at the latest value for appt_type

 

Once again thank you very much!

I added a rank column and does it

date diff = if(Sheet1[Appt_type]=66,datediff(minx(filter(Sheet1,Sheet1[Appt_type]=64 && Sheet1[Cust.]=EARLIEST(Sheet1[Cust.]) && Sheet1[rank]=EARLIEST(Sheet1[rank])),Sheet1[Date]),minx(filter(Sheet1,Sheet1[Appt_type]=66 && Sheet1[Cust.]=EARLIEST(Sheet1[Cust.]) && Sheet1[rank]=EARLIEST(Sheet1[rank])),Sheet1[Date]),DAY),BLANK())

 

rank = countx(filter(Sheet1,Sheet1[Appt_type]=64 && Sheet1[Date]<=EARLIER(Sheet1[Date])),[Appt_type])

 

https://www.dropbox.com/s/yo0x4s76ww1vfuo/datediff64.pbix?dl=0

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
CRT
Frequent Visitor

Thank you very much for your help, if I change the formula from amitchandak to maxx in both instances I get the result I want!

 

date diff =
if(maxx(filter('Table';'Table'[Cust.]=EARLIER('Table'[Cust.]) && 'Table'[Appt_type]=66);'Table'[Date])='Table'[Date];
datediff(maxx(filter('Table';'Table'[Cust.]=EARLIER('Table'[Cust.]) && 'Table'[Appt_type]=64);'Table'[Date]);'Table'[Date];day);blank())
 
 
Capture_PBI.PNG
 
 

Interesting. If I change the StartDate variable to do MAX in my measure, I get the same, but neither returns the 90 days you have near the top of your data...



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Please look at your data. Customer 12345 for type 64 the first date in your table is Feb 1, 2019. The very first record in the table.

 

How is the formula supposed to know you don't mean that one, but you want the one that is Dec 31, 2019?



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

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.