Forum Discussion

POSPOS's avatar
POSPOS
Icon for Post Partisan rankPost Partisan
2 years ago
Solved

Calculate difference between previous date of same column using DAX

Hi All,

I have a requirement to calculate the difference between date of the current line to the previous line of the same column.

Please find sample data below:

ProtocolReceivedStatusDate
A6/30/2022Assigned11/7/2022
A 6/30/2022In-Progress1/18/2023
A 6/30/2022Pre-Review2/6/2323
A 6/30/2022Agenda2/6/2023
A 6/30/2022Pre-Review2/15/2023
B7/10/2023Assigned7/15/2023
B7/10/2023In-Progress7/15/2323
B7/10/2023Agenda8/1/2023
B7/10/2023Pre-Review 8/10/2023
B7/10/2023Pre-Review8/15/2023

 

Expected Output:

ProtocolReceived DateStatusEffective DateNo. of days
A6/30/2022Assigned11/7/2022130
A 6/30/2022In-Progress1/18/202372
A 6/30/2022Pre-Review2/6/202319
A 6/30/2022Agenda2/6/20230
A 6/30/2022Pre-Review2/15/20239
B7/10/2023Assigned7/15/20235
B7/10/2023In-Progress7/15/20230
B7/10/2023Modified8/1/202317
B7/10/2023Pre-Review 8/10/20239
B7/10/2023Modified8/15/20235

 

1. First value of a group will be the received date minus effective date

2. The other values will be difference from the previous date of the effective date column.

 

I followed the blog below but this is not working when the dates are same as well the status are getting aggregated instead of showing it separately.

DAX Fridays! #85: Difference between dates in the same column

Could someone pleas advise how this can be solved using DAX.

Thank you.

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi POSPOS 

    You can add a index column in power query first and apply it

    Then create a calculated column

    Column = var a=FILTER('Table',[Protocol]=EARLIER('Table'[Protocol]))
    var b=MINX(FILTER(a,[Date]=MINX(a,[Date])),[Index])
    return IF([Index]=b,DATEDIFF([Received],[Date],DAY),DATEDIFF(MAXX(FILTER(a,[Index]=EARLIER('Table'[Index])-1),[Date]),[Date],DAY))

    Output

    Best Regards!

    Yolo Zhu

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

     

     

3 Replies

    • POSPOS's avatar
      POSPOS
      Icon for Post Partisan rankPost Partisan

      ThxAlot  - Have you created index using DAX? Is there a way I can create index column using DAX.
      Could you share you pbix pls.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi POSPOS 

    You can add a index column in power query first and apply it

    Then create a calculated column

    Column = var a=FILTER('Table',[Protocol]=EARLIER('Table'[Protocol]))
    var b=MINX(FILTER(a,[Date]=MINX(a,[Date])),[Index])
    return IF([Index]=b,DATEDIFF([Received],[Date],DAY),DATEDIFF(MAXX(FILTER(a,[Index]=EARLIER('Table'[Index])-1),[Date]),[Date],DAY))

    Output

    Best Regards!

    Yolo Zhu

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