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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Talal141218
Helper III
Helper III

Use Datediff function in two tables, which located in unrelatet tables

Hello Profis, 

i need your help to create a measure to subtract two dates in days, which located in tabels no realtet between them. 

Please find the screen shot maybe help you to solve my issue. In addtion i want to create this measure in Fact_SalesOrderLine as you see in the Pic. 

Please if you need more  infos, don't hesitate to contect me.

Thanks in Advance. 

Talal141218_0-1730972585068.png

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Talal141218 ,
According to your description, after you arrive at the results, there may be data mismatches in the results, or the calculated values do not make logical sense, I try to reproduce your problem with some sample data, I hope to provide some ideas for the problems you encountered!
My sample data is calculating the difference between the date of each sales order and the date of the latest purchase order, and in the case where I am using the error code , although the value is returned, there is a logic error, as detailed below:

vxingshenmsft_0-1731031911414.png

Order corresponds to multiple Purch values, and since we don't have a relationship, we need the other columns to help us make sure that there are no duplicate values, and we can get the correct value.

DateDiffMeasure = 
VAR OrderDate = MAX(Sales[OrderDate])
VAR ClosestPurchaseDate = 
    CALCULATE(
        MIN(Purch[PurchaseDate]),
        FILTER(
            Purch,
            Purch[PurchaseDate] <= OrderDate // Ensure that the purchase date is before the order date
                && Purch[Index] =MAX('Sales'[OrderID]) // Ensure that each order is uniquely matched
        ),
        TOPN(1, Purch, Purch[PurchaseDate], DESC) // Get the most recent PurchaseDate
    )
RETURN
IF(
    NOT(ISBLANK(ClosestPurchaseDate)),
    ABS(DATEDIFF(OrderDate, ClosestPurchaseDate, DAY)),
    BLANK()
)

vxingshenmsft_1-1731032062739.png

If you still have questions you can check out my pbix, I hope my answer can further answer your questions and I would be honored if I can solve your problem!

Hope it helps!

Best regards,
Community Support Team_ Tom Shen

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

 

 



View solution in original post

3 REPLIES 3
ValtteriN
Super User
Super User

Hello,

You can calculate the dates you want to compare in variables and then place them in datediff. 

e.g. Here I have a custom condition that date2 needs to be in year 2023, date1 has no conditions:

DDiff no relation =
var _date1 = MAX(GantDemo[E_date])
var _date2 = CALCULATE(MAX('Calendar'[Date]),YEAR('Calendar'[Date])=2023)
RETURN
datediff(_date1,_date2,DAY)

ValtteriN_0-1730976941635.png

 

The tables are not related. You can add whatever conditions you need in the variable calculations (e.g. id needs to be some specific id).



I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!

My LinkedIn: https://www.linkedin.com/in/n%C3%A4ttiahov-00001/





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

Proud to be a Super User!




Hello, 

Firstful thanks alot for your response,and i have results now but those are neither corresponding nor logic.

 

Anonymous
Not applicable

Hi @Talal141218 ,
According to your description, after you arrive at the results, there may be data mismatches in the results, or the calculated values do not make logical sense, I try to reproduce your problem with some sample data, I hope to provide some ideas for the problems you encountered!
My sample data is calculating the difference between the date of each sales order and the date of the latest purchase order, and in the case where I am using the error code , although the value is returned, there is a logic error, as detailed below:

vxingshenmsft_0-1731031911414.png

Order corresponds to multiple Purch values, and since we don't have a relationship, we need the other columns to help us make sure that there are no duplicate values, and we can get the correct value.

DateDiffMeasure = 
VAR OrderDate = MAX(Sales[OrderDate])
VAR ClosestPurchaseDate = 
    CALCULATE(
        MIN(Purch[PurchaseDate]),
        FILTER(
            Purch,
            Purch[PurchaseDate] <= OrderDate // Ensure that the purchase date is before the order date
                && Purch[Index] =MAX('Sales'[OrderID]) // Ensure that each order is uniquely matched
        ),
        TOPN(1, Purch, Purch[PurchaseDate], DESC) // Get the most recent PurchaseDate
    )
RETURN
IF(
    NOT(ISBLANK(ClosestPurchaseDate)),
    ABS(DATEDIFF(OrderDate, ClosestPurchaseDate, DAY)),
    BLANK()
)

vxingshenmsft_1-1731032062739.png

If you still have questions you can check out my pbix, I hope my answer can further answer your questions and I would be honored if I can solve your problem!

Hope it helps!

Best regards,
Community Support Team_ Tom Shen

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

 

 



Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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