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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Date difference between two different tables

I searched the forums but could not find a solution to my particular issue.    I have a date table.  

 

My date table is connected to "date shipped" in my shipped table, and to "order date" in the orders table.  

 

I need to calculate the days difference between date shipped.......but to "request date" in the orders table   (not to order date).    But my relationship from my date table goes to the "order date" and I cannot change that.  

 

Any help is appreciated!  

 

Annotation 2020-01-23 205052.png

1 ACCEPTED SOLUTION

I tried to create a Data model that can refer to your data model. Please find this pbix, where I created 4 ways how can you get date diff from two different tables.

I created a common dim, Date dim. Common dim for date diff. The date dim just for your ref.

I also populated dates from one table to another to get date diff.

 

https://www.dropbox.com/s/y47ah38sr157l7t/Oder_delivery.pbix?dl=0

 

Let me know if this can solve your purpose.

 

View solution in original post

21 REPLIES 21
Ashish_Mathur
Super User
Super User

Hi,

Take a simple example and show the expected result.


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

Due Date                  Shipped Date        Days Early/Late  

 

Jan 10 2020              Jan 15 2020                5

 

Jan 12 2020              Jan 09 2020               -3   

Hi,

With that little information, there is nothing i can do to help you.  All i can say is that you need to create an inactive relationship and then use the USERELATIONSHIP() function.


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

I have given more information in my previous posts.  

Someone else will help you.


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

@Anonymous 

Please check the solution in file, if that can help https://www.dropbox.com/s/y47ah38sr157l7t/Oder_delivery.pbix?dl=0

Anonymous
Not applicable

Also, these results are strange.     Note that sometimes they show correctly that the shipment was 3 days early.   But for the same dates sometimes it says -9.    It could be because there are multiple lines attached to one order...and maybe the lines shipped at different times and it is picking up the latest ship date?   Checking into that now.  

 

dates.png

 

 

@Anonymous 

I am assuming, you have created both new columns as calculation. Can you share the formula

Anonymous
Not applicable

.Yes, I created 2 calculated columns in the Orders table

 

Way 2 Delivery Date = maxx(filter('Flu Shipped','Flu Shipped'[Order]=Orders[Order]),'Flu Shipped'[Date Shipped])
 
Way 2 date Diff = DATEDIFF('Orders'[Due Date],'Orders'[Way 2 Delivery Date],DAY)
 
I checked the lines and that was not the issue. I am still getting what seems to be random strange results.
 
dates.png
 

@Anonymous 

Are checking result at table level or visual level. In the case of visual level change Agg to Avg. There seems to be more than one line for the order.

Anonymous
Not applicable

Hi Amit - Almost there!

 

Changing to avg on the visual did fix that issue.  

 

However, now I am noticing a different issue.   The measure is not returning all of the records.    In the shipped table for January there are 435 disctinct records (orders).    But the measure is only returning results for 37.   

As orders might me repeating it is grouping those up. Can add other columns and try.

Or check distinct order , by putting them into a matrix

Anonymous
Not applicable

Hi Amit -

 

Thanks so much for that file...it was great...something I can learn from.  

 

I tried "way 2" and it seems to work, although I am getting results for dates that have not even happened yet.   See image.   Note the first two dates.

 

dates.png

amitchandak
Super User
Super User

If you want to use measure, means run time calc you need ensure that it happens at level

like

datediff =

var _order = Min(sales[order_date])
var _Delivery = max(deilvery[Deilvery_date])
//Assume you common order and date dim
calculate(sumx(values(order[ID]),datediff([_order],[_Delivery]),DAY))
//OR
//calculate(sumx(Order,datediff([_order],[_Delivery],DAY)),values(order[ID]))

 

Another method is to get the date in one table or maybe order dim

New column

Max date in Sales 1 = maxx(filter(deilvery,sales[Order_id] = deilvery[Order_id] ,deilvery[Deilvery_date]))

Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution. In case it does not help, please provide additional information and mark me with @
Thanks. My Recent Blog -
Winner-Topper-on-Map-How-to-Color-States-on-a-Map-with-Winners , HR-Analytics-Active-Employee-Hire-and-Termination-trend
Power-BI-Working-with-Non-Standard-Time-Periods And Comparing-Data-Across-Date-Ranges

Connect on Linkedin

 

Anonymous
Not applicable

Hello,

 

I am not really following your solutions because you are using completely different table names than my real information.   

I tried to create a Data model that can refer to your data model. Please find this pbix, where I created 4 ways how can you get date diff from two different tables.

I created a common dim, Date dim. Common dim for date diff. The date dim just for your ref.

I also populated dates from one table to another to get date diff.

 

https://www.dropbox.com/s/y47ah38sr157l7t/Oder_delivery.pbix?dl=0

 

Let me know if this can solve your purpose.

 

Anonymous
Not applicable

Hi Amit - Thank you for all of your help!!

 

I ended up adding an Orders Dim table and using Way 1 from your example.    Seems to be working perfectly! 

 

Cheers!

Anonymous
Not applicable

Hi Amit - I marked everything as a solution and gave kudos...again very much appreciated.  

 

I did just notice one small thing that perhaps you can answer.  When I average the measure results in a table, I get 4.81.   (complete table not shown).    But if I put the measure in a card it gives me 4.70.     Also in excel I get 4.81.  

 

average.png

If it is measure use way4 so that line-level calculations are done and you get the same number.

Anonymous
Not applicable

I tried the first method but get an error on the comma  (after date shipped).   

 

Remember that these are in two different tables, and that "request date" is not connected to my date table  (but it is in the same table as order date).   

 

datediff =

var _order = Min(Orders[Request Date])
var _Delivery = max('Flu Shipped'[Date Shipped]),

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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