Forum Discussion

thehalfboy's avatar
thehalfboy
Helper I
2 years ago
Solved

Calculating time between separate rows based on another column

Good afternoon,

 

I've come across a thorny little problem that I can't work out the logic on. Ideally I'd like to solve this woth DAX but if it's easier within Power Query that wouldn't be the end of the world.

 

My data looks like this:

 

Requisition NumberAction CodeAction DateCreation DateApproved Date
RR8N3SUBMIT20/09/2023 15:0320/09/2023 15:0316/10/2023 16:59
RR8N3APPROVE20/09/2023 15:0920/09/2023 15:0316/10/2023 16:59
RR8N3APPROVE20/09/2023 15:2820/09/2023 15:0316/10/2023 16:59
RR8N3APPROVE22/09/2023 15:2920/09/2023 15:0316/10/2023 16:59
RR8N3REJECT29/09/2023 15:5220/09/2023 15:0316/10/2023 16:59
RR8N3SUBMIT05/10/2023 11:5220/09/2023 15:0316/10/2023 16:59
RR8N3APPROVE05/10/2023 12:0120/09/2023 15:0316/10/2023 16:59
RR8N3APPROVE05/10/2023 14:4620/09/2023 15:0316/10/2023 16:59
RR8N3APPROVE10/10/2023 14:5720/09/2023 15:0316/10/2023 16:59
RR8N3APPROVE10/10/2023 14:5920/09/2023 15:0316/10/2023 16:59
RR8N3APPROVE10/10/2023 15:5620/09/2023 15:0316/10/2023 16:59
RR8N3APPROVE10/10/2023 16:1320/09/2023 15:0316/10/2023 16:59
RR8N3APPROVE10/10/2023 16:2820/09/2023 15:0316/10/2023 16:59
RR8N3APPROVE10/10/2023 16:4220/09/2023 15:0316/10/2023 16:59
RR8N3APPROVE16/10/2023 16:4420/09/2023 15:0316/10/2023 16:59
RR8N3APPROVE16/10/2023 16:5920/09/2023 15:0316/10/2023 16:59

 

For each order, there may be multiple "SUBMIT" lines and multiple "APPROVE". I need to find two different bits of data from this:

 

  • The amount of time in hours between the final "SUBMIT" and the final "APPROVE"
  • The amount of time in hours between the penultimate "APPROVE" and the final "APPROVE"

The plan is to be able to show how much time would be saved by skipping to the final stage of the approval process, so the first bit of data shows how long it took for an order to pass through the approval hierarchy, while the second bit of data shows how long it would have taken if the order had gone to the final step in the chain right away.

 

Any help would be greatly appreciated, and stop me from pulling out my last few remaining hairs.

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi thehalfboy ,

    Please follow these steps:

    1.        add new column

    Column = 
    CALCULATE (
        COUNTROWS ( 'Table' ),
        FILTER (
            'Table',
            'Table'[Requisition Number] = EARLIER ( 'Table'[Requisition Number] )
        )
    )
    

    the final "SUBMIT" and the final "APPROVE" = 
    VAR _1 =
        CALCULATE (
            MAX ( 'Table'[Action Date] ),
            FILTER (
                'Table',
                'Table'[Action Code] = "APPROVE"
                    && 'Table'[Column] = EARLIER ( 'Table'[Column] )
            )
        )
    VAR _2 =
        CALCULATE (
            MAX ( 'Table'[Action Date] ),
            FILTER (
                'Table',
                'Table'[Action Code] = "SUBMIT"
                    && 'Table'[Column] = EARLIER ( 'Table'[Column] )
            )
        )
    RETURN
        DATEDIFF ( _2, _1, HOUR )
    

    2.    sort APPROVE

    sort = 
    RANKX (
        FILTER (
            'Table',
            'Table'[Action Code] = "APPROVE"
                && 'Table'[Column] = EARLIER ( 'Table'[Column] )
        ),
        'Table'[Action Date],
        ,
        DESC
    )
    

    3.     the penultimate "APPROVE" and the final "APPROVE"

    the penultimate "APPROVE" and the final "APPROVE" 1 = 
    VAR _actDate =
        CALCULATE (
            MAX ( 'Table'[Action Date] ),
            FILTER (
                'Table',
                'Table'[sort] = 1
                    && 'Table'[Column] = SELECTEDVALUE ( 'Table'[Column] )
            )
        )
    VAR _actDate2 =
        CALCULATE (
            MAX ( 'Table'[Action Date] ),
            FILTER (
                'Table',
                'Table'[sort] = 2
                    && 'Table'[Column] = SELECTEDVALUE ( 'Table'[Column] )
            )
        )
    RETURN
        DATEDIFF ( _actDate2, _actDate, MINUTE )
    

    How to Get Your Question Answered Quickly - Microsoft Fabric Community

    If it does not help, please provide more details with your desired out put and pbix file without privacy information.

     

    Best Regards,

    Yifan Wang

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

     

     

     

4 Replies

  • thehalfboy , Create a column and following measure

     

    New column
    Approve Rank =
    RANKX (
    FILTER (
    'Table',
    'Table'[Requisition Number] = EARLIER ( 'Table'[Requisition Number] )
    && 'Table'[Action Code] = "APPROVE"
    ),
    'Table'[Action Date],
    ,
    DESC,
    Dense
    )

    New Measure

    Final Submit Date =
    CALCULATE (
    MAX ( 'Table'[Action Date] ),
    FILTER (
    allselected('Table'),
    'Table'[Requisition Number] = max ( 'Table'[Requisition Number] )
    && 'Table'[Action Code] = "SUBMIT"
    )
    )


    Final Approve Date =
    CALCULATE (
    MAX ( 'Table'[Action Date] ),
    FILTER (
    allselected('Table'),
    'Table'[Requisition Number] = max ( 'Table'[Requisition Number] )
    && 'Table'[Action Code] = "APPROVE"
    )
    )

    Time Between Submit and Approve (Hours) =
    DATEDIFF (
    [Final Submit Date],
    [Final Approve Date],
    HOUR
    )


    Penultimate Approve Date =
    CALCULATE (
    MAX ( 'Table'[Action Date] ),
    FILTER (
    allselected('Table'),
    'Table'[Requisition Number] = max ( 'Table'[Requisition Number] )
    && 'Table'[Approve Rank] = 2
    )
    )

    DATEDIFF (
    [Penultimate Approve Date],
    [Final Approve Date],
    HOUR
    )

     

     

     

    For the first rank measure, you can use the new rank function to create measure

     

     




    )


    Penultimate Approve Date =
    CALCULATE (
    MAX ( 'Table'[Action Date] ),
    FILTER (
    'Table',
    'Table'[Requisition Number] = EARLIER ( 'Table'[Requisition Number] )
    && 'Table'[Approve Rank] = 2
    )
    )







  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi thehalfboy ,

    Please follow these steps:

    1.        add new column

    the final "SUBMIT" and the final "APPROVE" = 
    VAR _1 =
        CALCULATE (
            MAX ( 'Table'[Action Date] ),
            FILTER ( 'Table', 'Table'[Action Code] = "APPROVE" )
        )
    VAR _2 =
        CALCULATE (
            MAX ( 'Table'[Action Date] ),
            FILTER ( 'Table', 'Table'[Action Code] = "SUBMIT" )
        )
    RETURN
        DATEDIFF ( _2, _1, HOUR )
    
    1.        sort APPROVE

    the penultimate "APPROVE" and the final "APPROVE" =
    RANKX (
        FILTER ( 'Table', 'Table'[Action Code] = "APPROVE" ),
        'Table'[Action Date],
        ,
        DESC
    )
    
    1.         the penultimate "APPROVE" and the final "APPROVE"
    the penultimate "APPROVE" and the final "APPROVE" = 
       var _actDate = CALCULATE(
           MAX('Table'[Action Date]),
           FILTER('Table','Table'[sort] = 1)
       )
       var _actDate2 = CALCULATE(
           MAX('Table'[Action Date]),
           FILTER('Table','Table'[sort] = 2)
       )
      RETURN
         DATEDIFF(_actDate2,_actDate,MINUTE
             )
    

     

    How to Get Your Question Answered Quickly - Microsoft Fabric Community

    If it does not help, please provide more details with your desired out put and pbix file without privacy information.

     

    Best Regards,

    Yifan Wang

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

  • Thank you for the replies. I'm sorry, but I think in my urge to simplify my data, I took it too far, and so the responses aren't working right. There are actually multiple different Requisition numbers, and so the calculation needs to find the "last submit" to "last approval" and "penultimate approval" to "last approval" for each requisition number in the table. Hopefully the data below is more like what is needed.

     

    Requisition NumberAction CodeAction DateCreation DateApproved Date
    RR8N2SUBMIT24/03/2023 09:3724/03/2023 07:3002/05/2023 18:06
    RR8N2APPROVE18/04/2023 11:0324/03/2023 07:3002/05/2023 18:06
    RR8N2APPROVE18/04/2023 11:0324/03/2023 07:3002/05/2023 18:06
    RR8N2APPROVE18/04/2023 11:0824/03/2023 07:3002/05/2023 18:06
    RR8N2APPROVE18/04/2023 12:5624/03/2023 07:3002/05/2023 18:06
    RR8N2APPROVE18/04/2023 13:0724/03/2023 07:3002/05/2023 18:06
    RR8N2REQUISITION WITHDRAW19/04/2023 16:3624/03/2023 07:3002/05/2023 18:06
    RR8N2SUBMIT19/04/2023 16:4324/03/2023 07:3002/05/2023 18:06
    RR8N2APPROVE20/04/2023 07:1224/03/2023 07:3002/05/2023 18:06
    RR8N2APPROVE25/04/2023 11:4124/03/2023 07:3002/05/2023 18:06
    RR8N2APPROVE25/04/2023 12:4024/03/2023 07:3002/05/2023 18:06
    RR8N2APPROVE25/04/2023 12:4124/03/2023 07:3002/05/2023 18:06
    RR8N2APPROVE27/04/2023 12:4424/03/2023 07:3002/05/2023 18:06
    RR8N2APPROVE27/04/2023 12:4524/03/2023 07:3002/05/2023 18:06
    RR8N2APPROVE27/04/2023 12:4624/03/2023 07:3002/05/2023 18:06
    RR8N2APPROVE27/04/2023 16:0924/03/2023 07:3002/05/2023 18:06
    RR8N2APPROVE27/04/2023 17:1224/03/2023 07:3002/05/2023 18:06
    RR8N2APPROVE02/05/2023 17:1624/03/2023 07:3002/05/2023 18:06
    RR8N2APPROVE02/05/2023 17:2324/03/2023 07:3002/05/2023 18:06
    RR8N2APPROVE02/05/2023 18:0624/03/2023 07:3002/05/2023 18:06
    RR8N3SUBMIT20/09/2023 15:0320/09/2023 15:0316/10/2023 16:59
    RR8N3APPROVE20/09/2023 15:0920/09/2023 15:0316/10/2023 16:59
    RR8N3APPROVE20/09/2023 15:2820/09/2023 15:0316/10/2023 16:59
    RR8N3APPROVE22/09/2023 15:2920/09/2023 15:0316/10/2023 16:59
    RR8N3REJECT29/09/2023 15:5220/09/2023 15:0316/10/2023 16:59
    RR8N3SUBMIT05/10/2023 11:5220/09/2023 15:0316/10/2023 16:59
    RR8N3APPROVE05/10/2023 12:0120/09/2023 15:0316/10/2023 16:59
    RR8N3APPROVE05/10/2023 14:4620/09/2023 15:0316/10/2023 16:59
    RR8N3APPROVE10/10/2023 14:5720/09/2023 15:0316/10/2023 16:59
    RR8N3APPROVE10/10/2023 14:5920/09/2023 15:0316/10/2023 16:59
    RR8N3APPROVE10/10/2023 15:5620/09/2023 15:0316/10/2023 16:59
    RR8N3APPROVE10/10/2023 16:1320/09/2023 15:0316/10/2023 16:59
    RR8N3APPROVE10/10/2023 16:2820/09/2023 15:0316/10/2023 16:59
    RR8N3APPROVE10/10/2023 16:4220/09/2023 15:0316/10/2023 16:59
    RR8N3APPROVE16/10/2023 16:4420/09/2023 15:0316/10/2023 16:59
    RR8N3APPROVE16/10/2023 16:5920/09/2023 15:0316/10/2023 16:59
    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi thehalfboy ,

      Please follow these steps:

      1.        add new column

      Column = 
      CALCULATE (
          COUNTROWS ( 'Table' ),
          FILTER (
              'Table',
              'Table'[Requisition Number] = EARLIER ( 'Table'[Requisition Number] )
          )
      )
      

      the final "SUBMIT" and the final "APPROVE" = 
      VAR _1 =
          CALCULATE (
              MAX ( 'Table'[Action Date] ),
              FILTER (
                  'Table',
                  'Table'[Action Code] = "APPROVE"
                      && 'Table'[Column] = EARLIER ( 'Table'[Column] )
              )
          )
      VAR _2 =
          CALCULATE (
              MAX ( 'Table'[Action Date] ),
              FILTER (
                  'Table',
                  'Table'[Action Code] = "SUBMIT"
                      && 'Table'[Column] = EARLIER ( 'Table'[Column] )
              )
          )
      RETURN
          DATEDIFF ( _2, _1, HOUR )
      

      2.    sort APPROVE

      sort = 
      RANKX (
          FILTER (
              'Table',
              'Table'[Action Code] = "APPROVE"
                  && 'Table'[Column] = EARLIER ( 'Table'[Column] )
          ),
          'Table'[Action Date],
          ,
          DESC
      )
      

      3.     the penultimate "APPROVE" and the final "APPROVE"

      the penultimate "APPROVE" and the final "APPROVE" 1 = 
      VAR _actDate =
          CALCULATE (
              MAX ( 'Table'[Action Date] ),
              FILTER (
                  'Table',
                  'Table'[sort] = 1
                      && 'Table'[Column] = SELECTEDVALUE ( 'Table'[Column] )
              )
          )
      VAR _actDate2 =
          CALCULATE (
              MAX ( 'Table'[Action Date] ),
              FILTER (
                  'Table',
                  'Table'[sort] = 2
                      && 'Table'[Column] = SELECTEDVALUE ( 'Table'[Column] )
              )
          )
      RETURN
          DATEDIFF ( _actDate2, _actDate, MINUTE )
      

      How to Get Your Question Answered Quickly - Microsoft Fabric Community

      If it does not help, please provide more details with your desired out put and pbix file without privacy information.

       

      Best Regards,

      Yifan Wang

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