Forum Discussion

_Andrew_'s avatar
_Andrew_
Regular Visitor
9 years ago
Solved

Difference between two dates in two rows in a matrix

Hi, I'm new to PowerBI and am having trouble calculating the difference between two rows in a matrix.

 

What I want is a matrix showing the earliest successful deployment and the interval between the first successful deployment for each version:

 

VersionEarliest Completed DateInterval
2.1219 June 2019 
2.1312 July 201923
3.010 August 201929
3.105 September 201926

 

I can easily get the first two columns, but can't come up with a measure to calculate the difference between the dates in neighbouring rows.

 

Is this possible in PowerBI?

 

Thanks.

Andrew.

 

A mockup of the raw data is something like this (obviously the actual data is more complicated, but this narrows it down to this exact question):

 

Unique Task IDTaskStateCompleted DateVersion
15428Success19/06/20192.12
15585Success22/06/20192.12
15726Success26/06/20192.12
15771Success27/06/20192.12
15773Success27/06/20192.12
15784Success27/06/20192.12
15841Success28/06/20192.12
15843Success28/06/20192.12
15886Success29/06/20192.12
15895Success29/06/20192.12
16131Success04/07/20192.12
16137Failed04/07/20192.12
16139Failed04/07/20192.12
16142Success04/07/20192.12
16145Success04/07/20192.12
16247Success06/07/20192.12
16280Failed06/07/20192.12
16281Failed06/07/20192.12
16282Failed06/07/20192.12
16284Success06/07/20192.12
16559Success12/07/20192.13
16611Success13/07/20192.13
16764Success17/07/20192.13
16846Success19/07/20192.13
16847Success19/07/20192.13
16854Success19/07/20192.13
16855Success19/07/20192.13
16890Success20/07/20192.13
16892Success20/07/20192.13
16900Success20/07/20192.13
16901Success20/07/20192.13
16942Success21/07/20192.13
16943Success21/07/20192.13
16945Success21/07/20192.13
16946Success21/07/20192.13
17800Failed10/08/20193.0
17801Failed10/08/20193.0
17802Failed10/08/20193.0
17803Success10/08/20193.0
18056Success17/08/20193.0
18066Success17/08/20193.0
18107Success18/08/20193.0
18109Success18/08/20193.0
18205Success21/08/20193.0
18210Success21/08/20193.0
18211Success21/08/20193.0
18217Success21/08/20193.0
18252Success22/08/20193.0
18255Success22/08/20193.0
18256Success22/08/20193.0
18263Success22/08/20193.0
18264Success22/08/20193.0
18295Success23/08/20193.0
18298Success23/08/20193.0
18303Success23/08/20193.0
18408Failed25/08/20193.0
18409Success25/08/20193.0
18874Success05/09/20193.1
18943Success07/09/20193.1
18944Success07/09/20193.1
18952Success07/09/20193.1
19109Success12/09/20193.1
19110Success12/09/20193.1
19111Success12/09/20193.1
19113Success12/09/20193.1
19117Success12/09/20193.1
19118Failed12/09/20193.1
19119Success12/09/20193.1
19153Success13/09/20193.1
19184Success14/09/20193.1
19185Success14/09/20193.1
19187Success14/09/20193.1
19189Success14/09/20193.1
19193Success14/09/20193.1
19194Success14/09/20193.1
19225Failed15/09/20193.1
19226Failed15/09/20193.1
19228Success15/09/20193.1

 

Like you'd get out of Octopus deploy.

 

 

 

 

  • Hi _Andrew_,

     

    Suppose the data type of [Version] is numeric.

     

    You need a [Rank] column in source table (suppose it's Table1).

    Rank = RANKX(Table1,Table1[Version],,ASC,Dense)

    Then, please create some measures as below:

    earliest date this version = MIN(Table1[Completed Date]) 
    earliest date last version =
    CALCULATE (
        MIN ( Table1[Completed Date] ),
        FILTER (
            ALL ( Table1 ),
            Table1[Rank]
                = MAX ( Table1[Rank] ) - 1
                && Table1[TaskState] = "Success"
        )
    )
    
    Interval =
    SWITCH (
        TRUE (),
        [earliest date last version] < [earliest date this version], DATEDIFF ( [earliest date last version], [earliest date this version], DAY ),
        [earliest date last version] > [earliest date this version], -1
            * DATEDIFF ( [earliest date this version], [earliest date last version], DAY ),
        0
    )

    Add corresponding columns into Matrix.

     

    Best regards,
    Yuliana Gu

4 Replies

  • v-yulgu-msft's avatar
    v-yulgu-msft
    Microsoft Employee

    Hi _Andrew_,

     

    Suppose the data type of [Version] is numeric.

     

    You need a [Rank] column in source table (suppose it's Table1).

    Rank = RANKX(Table1,Table1[Version],,ASC,Dense)

    Then, please create some measures as below:

    earliest date this version = MIN(Table1[Completed Date]) 
    earliest date last version =
    CALCULATE (
        MIN ( Table1[Completed Date] ),
        FILTER (
            ALL ( Table1 ),
            Table1[Rank]
                = MAX ( Table1[Rank] ) - 1
                && Table1[TaskState] = "Success"
        )
    )
    
    Interval =
    SWITCH (
        TRUE (),
        [earliest date last version] < [earliest date this version], DATEDIFF ( [earliest date last version], [earliest date this version], DAY ),
        [earliest date last version] > [earliest date this version], -1
            * DATEDIFF ( [earliest date this version], [earliest date last version], DAY ),
        0
    )

    Add corresponding columns into Matrix.

     

    Best regards,
    Yuliana Gu

  • fhill's avatar
    fhill
    Resident Rockstar

    See if this gives you what you need...   you had your blank on the first line, but my logic moves the 'unkown' to the last line.  To do calcuations, I had to choose some 'date' to end the logic so I put code in there to always use MAX(Date) at the end of the file.  This is neccessary to 'skip' the final Version comparison b/c there's no higher versions to compare it too...  I also want to give a Shout Out to this AWESOME article that helped me understand EARLIER/EARLIST to write this 'first time use' code...  http://tinylizard.com/dax-earlier-function/

     

    If anyone has an easier way, please feel free to jump in!

    FOrrest

     

    Calculated Clumns:

    End of Version = IF( Table1[Version] = MAX(Table1[Version]),MAX(Table1[Date]), // Figure out what to do when we reach the MAX Version number since there's nothing higher than it to use to calcute a higher date...  I've put in the MAX date, but you will need to descide how you want to handel this???
    CALCULATE( MIN (Table1[Date]), // For Non Max Version Numbers Get MIN Date by...
    FILTER(ALL(Table1), Table1[Date] > EARLIER(Table1[Date])), // A Row GREATER Than the current Row for Date
    FILTER(Table1, Table1[Version] > EARLIER(Table1[Version])), // A Row Greater Than the current Row for Version
    FILTER(Table1, Table1[TaskState] = "Success") // That's a SUCCESS
    ) -1 ) // Go back 1 day & Close the IF Statement

     

    Measures:

    Min_Date = MIN (Table1[Date])

    Interval = DATEDIFF([Min_Date],AVERAGE(Table1[End of Version]),DAY)

     

  • _Andrew_'s avatar
    _Andrew_
    Regular Visitor

    Thanks fhill - that looks like an interesting approach, but I couldn't quite get it to work.  I'll keep a note of it though.

     

    v-yulgu-msft - that does exactly what I want thank you very much for your help.  I only needed to tweak earliest date last version slightly to work with my actual data.

     

    I think a good think for me to investigate next would be having the measure react to any filters that are applied instead of hardcoding it in the measure.

     

    Thank you.