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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
Anonymous
Not applicable

Date Difference between dates from multiple tables

HI,

 

I have 2 tables ( Orders, Tasks) with relationship Many-to-Many and Single Direction Filter, from Tasks=>Orders.

 

I need to calculate the difference between Order Date and the Task date. I've been using Datediff and Calculate, but didn't managed to get correct results. Can anyone help me with that?

Orders Table

 

Order IDUser IDProductOrder DateOrder Status
12689105Prod 110/6/2020Completed
22689105

Prod 2

10/1/2020Cancelled
31052689Prod 39/6/2020Completed
45268910Prod 47/12/2020Completed

 

Tasks Table

 

Task IDUser IDTask TypeTask Date
12689105Type 110/5/2020

2

2689105Type 29/18/2020
3 1052689Type 17/20/2020
45268910Type 28/12/2020
55268910Type 110/1/2020
64356744Type 1

6/25/2020

 

Key field is "User ID" . The Order dates are also filtered by a set of products , in this example "Prod 1" and "Prod 3", and the Tasks Table is filtered by Task Type, in this example "Type 1".

 

Expected result

 

Order IDUser IDProductOrder DateTask DateDateDiffOrder Status
12689105Prod 110/6/202010/5/20201 DayCompleted
31052689Prod 39/6/20207/20/202048 DaysCompleted

 

Thank you!

 

1 ACCEPTED SOLUTION

Hi @Anonymous,

 

First create 3 slicer tables as below:

ID slicer = VALUES('Tasks Table'[User ID])
Product slicer = VALUES('Orders Table'[Product])
Type slicer = VALUES('Tasks Table'[Task Type])

Then create  4 measures as below:

_Product = CALCULATE(MAX('Orders Table'[Product]),'Orders Table'[Product] in FILTERS('Product slicer'[Product]))
_User ID = CALCULATE(MAX('Orders Table'[User ID]),'Orders Table'[User ID] in FILTERS('ID slicer'[User ID]))
_Task date = CALCULATE(MAX('Tasks Table'[Task Date]),FILTER('Tasks Table','Tasks Table'[User ID]=[_User ID]&&'Tasks Table'[Task Type] in FILTERS('Type slicer'[Task Type])))
_Diff = 
var _diff= DATEDIFF([_Task date],MAX('Orders Table'[Order Date]),DAY)
Return
IF(_diff<=1,_diff&" day",_diff&" days")

And you will see:

Screenshot 2020-10-16 105053.png

For the related .pbix file,pls see attached.

 

Best Regards,
Kelly

Did I answer your question? Mark my post as a solution!

View solution in original post

4 REPLIES 4
v-kelly-msft
Community Support
Community Support

Hi  @Anonymous ,

 

Why there is no data about 5268910 in your expected result?How to match "Product" and "Task Type"?

 

 

Best Regards,
Kelly

Did I answer your question? Mark my post as a solution!

Anonymous
Not applicable

@v-kelly-msft as the data has the filters, basically 5268910 is excluded by the filter, therefore, the result will contain blank values for date diff.

Hi @Anonymous,

 

First create 3 slicer tables as below:

ID slicer = VALUES('Tasks Table'[User ID])
Product slicer = VALUES('Orders Table'[Product])
Type slicer = VALUES('Tasks Table'[Task Type])

Then create  4 measures as below:

_Product = CALCULATE(MAX('Orders Table'[Product]),'Orders Table'[Product] in FILTERS('Product slicer'[Product]))
_User ID = CALCULATE(MAX('Orders Table'[User ID]),'Orders Table'[User ID] in FILTERS('ID slicer'[User ID]))
_Task date = CALCULATE(MAX('Tasks Table'[Task Date]),FILTER('Tasks Table','Tasks Table'[User ID]=[_User ID]&&'Tasks Table'[Task Type] in FILTERS('Type slicer'[Task Type])))
_Diff = 
var _diff= DATEDIFF([_Task date],MAX('Orders Table'[Order Date]),DAY)
Return
IF(_diff<=1,_diff&" day",_diff&" days")

And you will see:

Screenshot 2020-10-16 105053.png

For the related .pbix file,pls see attached.

 

Best Regards,
Kelly

Did I answer your question? Mark my post as a solution!

amitchandak
Super User
Super User

@Anonymous , You need to have a common user ID table.

 

Then you can have

datediff(min(orders[Order Date]), max(Tasks[task Date]), Day)

refer

https://community.powerbi.com/t5/Community-Blog/Decoding-Direct-Query-in-Power-BI-Part-2-Date-Difference-Across/ba-p/934397#M451

 

Please provide your feedback comments and advice for new videos
Tutorial Series Dax Vs SQL Direct Query PBI Tips
Appreciate your Kudos.

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

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.