Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Formula to calculate difference between rows doesn't work properly for every row

Hi guys, 

I want to calculate a difference between two rows, 

Here is my formula:

Days in Status = 
VAR Index = 'JIRA Issues History'[Index]
VAR Reference = 'JIRA Issues History'[Issue Key]
VAR NextDays =
    CALCULATE (
        FIRSTNONBLANK ( 'JIRA Issues History'[Days], TRUE () ),
        FILTER ( 'JIRA Issues History', 'JIRA Issues History'[Index] = Index + 1 && 'JIRA Issues History'[Issue Key] = Reference )
    )
RETURN
    IF (
        ISBLANK ( NextDays ),
        BLANK (),
        NextDays - 'JIRA Issues History'[Days]
    )

However, it seems to work properly for some rows, but wrong for others, for example:

(The number should be the days passed between the 'Change Time' value of each row to the next one)

Can anyone help me understand where is the mistake? 

Thanks

  • Hi Anonymous ,

     

    With the sample above and the following DAX code, I get the subsequent solution, which seems to be correct:

     

    The code:

    DateDiffColumnWithIndex = 
    DATEDIFF (
        CALCULATE (
            MAX ( 'Table'[Total Days] ),
            FILTER ( 'Table',  'Table'[Index] = EARLIER ( 'Table'[Index] ) - 1 )
        ),
       'Table'[Total Days],
        DAY
    )   

     

    The memory problem is of course a different kind of issue. I recommend to outsource the solution to Power Query instead. While DAX can do such things, it is way better to utilize PQ for this.

     

    Result in PQ:

     

     

    1) Add a new custom column with the following name and code:

     

    2) Click on the left top corner in your query and choose merge query:

     

    3) Do a left outer join on the same table again and choose the grouping and index columns for the upper one and the grouping and IndexNextRow columns for the lower part. Note, click on the columns in the right order and hold CTRL to select multiple columns:

     

    4) click on the two arrows in the top right corner of the new column and just select change time:

     

    5) Add a new column by calculating the duration in days:

     

    6) remove all the columns that you do not need. 

     

    Here the whole code that you could use in the advanced editor:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZdNi9swEIb/ivF5BZqR5A/dCr0s7WFh29OSQyBpMGydJcku9N9XM5KdOOuR3UI+TGI9Gb3vfCgvL2WjK2vLh/L5+zdl6ta4cG200rVCKLR3QN9dtpf3c7j4cSy+HsN7EZ70hS43DwsIaL1pJ4xTtz3sd7z6ocQsBSulG6JUHswt5Gnf77r+UDy/v70dTxe6taZfDU8VrlbwzIT32BdPp+PhtD+fBxatVSCzAki3vEPjrcsHZ2mrjoMzcnA6Aa3HZo732H/sz5fusL10x56AJF7FYVqZikOY1gPkw3S0mPauWpEHToFmnod2KuGv4+k3x0ZafnQ7NrkmXsNbd/8OHRKOKS1vtZEpdaI0HoWUq+lz0AySnYB2BIErxTRpSE7WVMEaFuJ8UJETzRRttAqAMO1dSX52EYDEArOUbi4R3R1RMBKAXISlshCxg5WJw9UAmdKvEwcab9y8cIDkJFScG6IBqMIj1unUgRnpUh5lm9IIRPiUHvPKxWW8bZlqrmG6elY4U3E+5YMbMaGLSLIxCGP7zZUkxlbpqiXVaGsIeV498rRdIxr3TDYsA20TtBH6BlgGmLxmIuUqWeTYrIWhgQ9b5BYzcn72u/2p+NJvX/+cuzPjDJdKHgeDlZW3E96MBbwaq1j44sgySoeawgK0h4mpkgmciZivB5k6uhAxTdZL7QZMKIDJdm9scOQPN3d54oVxl0Dg7cKpASq6wej8BkWgJFuk5k9IMnWULWLyR6QM5ipbBNGLk/MtYIKVgRQORwsjBnkMmHw5aEy80CftmqMCcjeNa/+DOsiWMNVKDHot9MkEotyVBzMdAxm0PJiRWyQ3y8zZY3ABQzXVa1TjrmQWxoJIHVVjjF0YCiZhIKgm1ChyF7J8psmM95EEHhfmC7KX3IHlIyQ219BWFSlyiDY/GmTqqFvE2JUYezdNb3SLILo986fDqvAIoNqjXVDNUqLZKl+jLvFC73bC8RS5zCxlTVFuNn8B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Issue ID" = _t, #"Issue Key" = _t, #"Change Time" = _t, #"Field Name" = _t, #"Field Value" = _t, #"Total Days" = _t, index = _t, #"Days in Status" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Issue ID", Int64.Type}, {"Issue Key", type text}, {"Change Time", type datetime}, {"Field Name", type text}, {"Field Value", type text}, {"Total Days", Int64.Type}, {"index", Int64.Type}, {"Days in Status", Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "IndexNextRow", each [index] + 1),
        #"Merged Queries" = Table.NestedJoin(#"Added Custom", {"Issue ID", "index"}, #"Added Custom", {"Issue ID", "IndexNextRow"}, "Changed Type", JoinKind.LeftOuter),
        #"Expanded Changed Type" = Table.ExpandTableColumn(#"Merged Queries", "Changed Type", {"Change Time"}, {"Changed Type.Change Time"}),
        #"Added Custom1" = Table.AddColumn(#"Expanded Changed Type", "Custom", each Duration.Days( Date.From([Change Time]) - Date.From([Changed Type.Change Time])))
    in
        #"Added Custom1"

     

    Does this help? 🙂

     

    /Tom
    https://www.tackytech.blog/
    https://www.instagram.com/tackytechtom/

     

4 Replies

  • tackytechtom's avatar
    tackytechtom
    Most Valuable Professional

    Hi Anonymous ,

     

    With the sample above and the following DAX code, I get the subsequent solution, which seems to be correct:

     

    The code:

    DateDiffColumnWithIndex = 
    DATEDIFF (
        CALCULATE (
            MAX ( 'Table'[Total Days] ),
            FILTER ( 'Table',  'Table'[Index] = EARLIER ( 'Table'[Index] ) - 1 )
        ),
       'Table'[Total Days],
        DAY
    )   

     

    The memory problem is of course a different kind of issue. I recommend to outsource the solution to Power Query instead. While DAX can do such things, it is way better to utilize PQ for this.

     

    Result in PQ:

     

     

    1) Add a new custom column with the following name and code:

     

    2) Click on the left top corner in your query and choose merge query:

     

    3) Do a left outer join on the same table again and choose the grouping and index columns for the upper one and the grouping and IndexNextRow columns for the lower part. Note, click on the columns in the right order and hold CTRL to select multiple columns:

     

    4) click on the two arrows in the top right corner of the new column and just select change time:

     

    5) Add a new column by calculating the duration in days:

     

    6) remove all the columns that you do not need. 

     

    Here the whole code that you could use in the advanced editor:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZdNi9swEIb/ivF5BZqR5A/dCr0s7WFh29OSQyBpMGydJcku9N9XM5KdOOuR3UI+TGI9Gb3vfCgvL2WjK2vLh/L5+zdl6ta4cG200rVCKLR3QN9dtpf3c7j4cSy+HsN7EZ70hS43DwsIaL1pJ4xTtz3sd7z6ocQsBSulG6JUHswt5Gnf77r+UDy/v70dTxe6taZfDU8VrlbwzIT32BdPp+PhtD+fBxatVSCzAki3vEPjrcsHZ2mrjoMzcnA6Aa3HZo732H/sz5fusL10x56AJF7FYVqZikOY1gPkw3S0mPauWpEHToFmnod2KuGv4+k3x0ZafnQ7NrkmXsNbd/8OHRKOKS1vtZEpdaI0HoWUq+lz0AySnYB2BIErxTRpSE7WVMEaFuJ8UJETzRRttAqAMO1dSX52EYDEArOUbi4R3R1RMBKAXISlshCxg5WJw9UAmdKvEwcab9y8cIDkJFScG6IBqMIj1unUgRnpUh5lm9IIRPiUHvPKxWW8bZlqrmG6elY4U3E+5YMbMaGLSLIxCGP7zZUkxlbpqiXVaGsIeV498rRdIxr3TDYsA20TtBH6BlgGmLxmIuUqWeTYrIWhgQ9b5BYzcn72u/2p+NJvX/+cuzPjDJdKHgeDlZW3E96MBbwaq1j44sgySoeawgK0h4mpkgmciZivB5k6uhAxTdZL7QZMKIDJdm9scOQPN3d54oVxl0Dg7cKpASq6wej8BkWgJFuk5k9IMnWULWLyR6QM5ipbBNGLk/MtYIKVgRQORwsjBnkMmHw5aEy80CftmqMCcjeNa/+DOsiWMNVKDHot9MkEotyVBzMdAxm0PJiRWyQ3y8zZY3ABQzXVa1TjrmQWxoJIHVVjjF0YCiZhIKgm1ChyF7J8psmM95EEHhfmC7KX3IHlIyQ219BWFSlyiDY/GmTqqFvE2JUYezdNb3SLILo986fDqvAIoNqjXVDNUqLZKl+jLvFC73bC8RS5zCxlTVFuNn8B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Issue ID" = _t, #"Issue Key" = _t, #"Change Time" = _t, #"Field Name" = _t, #"Field Value" = _t, #"Total Days" = _t, index = _t, #"Days in Status" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Issue ID", Int64.Type}, {"Issue Key", type text}, {"Change Time", type datetime}, {"Field Name", type text}, {"Field Value", type text}, {"Total Days", Int64.Type}, {"index", Int64.Type}, {"Days in Status", Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "IndexNextRow", each [index] + 1),
        #"Merged Queries" = Table.NestedJoin(#"Added Custom", {"Issue ID", "index"}, #"Added Custom", {"Issue ID", "IndexNextRow"}, "Changed Type", JoinKind.LeftOuter),
        #"Expanded Changed Type" = Table.ExpandTableColumn(#"Merged Queries", "Changed Type", {"Change Time"}, {"Changed Type.Change Time"}),
        #"Added Custom1" = Table.AddColumn(#"Expanded Changed Type", "Custom", each Duration.Days( Date.From([Change Time]) - Date.From([Changed Type.Change Time])))
    in
        #"Added Custom1"

     

    Does this help? 🙂

     

    /Tom
    https://www.tackytech.blog/
    https://www.instagram.com/tackytechtom/

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Amazing! Thanks a lot, appreciate your help ! 

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi tackytechtom , 

      It seems like a good solution but it raises an error of memory :

      "There's not enough memory to complete this operation. Please try again later when there may be more memory available. "

      Here is the table in use:

      Issue IDIssue KeyChange TimeField NameField ValueTotal DaysindexDays in Status
      80644SLK-3793530-07-21 0:51StatusTo Do 10
      80644SLK-3793530-07-21 19:39StatusTriaged020
      80644SLK-3793526-08-21 6:13StatusPending Support273-27
      80644SLK-3793526-08-21 6:33StatusIn Progress274-17
      80644SLK-3793508-09-21 13:45StatusPending Support405-23
      80644SLK-3793520-09-21 4:28StatusPending Investigation526-14
      80644SLK-3793522-09-21 14:11StatusPending Support547-9
      80644SLK-3793515-10-21 1:19StatusInformation Provided778-25
      80644SLK-3793515-10-21 1:19StatusTo Do779-18
      80644SLK-3793517-10-21 8:29StatusTriaged7910-13
      80644SLK-3793519-10-21 8:15StatusIn Progress8111-1
      80644SLK-3793519-10-21 8:22StatusTriaged81126
      80644SLK-3793524-11-21 9:51StatusPending Support11713-23
      80644SLK-3793525-11-21 5:51StatusInformation Provided11814-17
      80644SLK-3793525-11-21 5:51StatusTo Do11815-10
      80644SLK-3793527-11-21 18:35StatusTriaged120169
      80644SLK-3793512-12-21 13:22StatusPending Support135170
      80644SLK-3793512-12-21 21:15StatusInformation Provided135181
      80644SLK-3793513-12-21 13:57StatusTo Do136190
      80644SLK-3793513-12-21 14:25StatusTriaged136202
      80644SLK-3793515-12-21 6:56StatusPending Support138212
      80644SLK-3793517-12-21 6:04StatusInformation Provided140222
      80644SLK-3793519-12-21 8:19StatusTo Do142230
      80644SLK-3793519-12-21 8:19StatusTriaged142241
      80644SLK-3793520-12-21 6:10StatusUnder Analysis143251
      80644SLK-3793521-12-21 16:40StatusPending Support1442613
      80644SLK-3793503-01-22 10:16StatusInformation Provided157270
      80644SLK-3793503-01-22 10:16StatusTo Do157282
      80644SLK-3793505-01-22 13:50StatusTriaged159294
      80644SLK-3793509-01-22 11:43StatusPending Support163300
      80644SLK-3793509-01-22 11:43StatusInformation Provided163310
      80644SLK-3793509-01-22 11:43StatusTo Do163320
      80644SLK-3793509-01-22 11:43StatusTriaged1633351
      80644SLK-3793501-03-22 6:31StatusPending Support214341
      80644SLK-3793502-03-22 21:49StatusInformation Provided215350
      80644SLK-3793502-03-22 21:49StatusTo Do215360
      80644SLK-3793502-03-22 22:05StatusTriaged215376
      80644SLK-3793508-03-22 9:51StatusPending Support221383
      80644SLK-3793511-03-22 20:17StatusInformation Provided224390
      80644SLK-3793511-03-22 20:17StatusTo Do224402
      80644SLK-3793513-03-22 12:00StatusTriaged2264110
      80644SLK-3793523-03-22 11:26StatusPending Support236425
      80644SLK-3793528-03-22 12:43StatusInformation Provided241430
      80644SLK-3793528-03-22 12:43StatusTo Do241440
      80644SLK-3793528-03-22 14:56StatusTriaged241457
      80644SLK-3793504-04-22 7:24StatusPending Support248461
      80644SLK-3793505-04-22 10:52StatusTriaged24947 

       

      This is a sample of a much larger table with many more Issue Keys. I can't share all of it.

      Thanks