Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

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!

 

  • 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:

    For the related .pbix file,pls see attached.

     

    Best Regards,
    Kelly

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

4 Replies

  • v-kelly-msft's avatar
    v-kelly-msft
    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's avatar
      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.

      • v-kelly-msft's avatar
        v-kelly-msft
        Community Support

        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:

        For the related .pbix file,pls see attached.

         

        Best Regards,
        Kelly

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