Forum Discussion
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
- tackytechtomMost 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/- AnonymousNot applicable
Amazing! Thanks a lot, appreciate your help !
- tackytechtomMost Valuable Professional
Hi Anonymous ,
Please, could you share the data you are showing in your screenshot?
Meanwhile, I can recommend this here. Here a similiar use case is described.
/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/- AnonymousNot 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 ID Issue Key Change Time Field Name Field Value Total Days index Days in Status 80644 SLK-37935 30-07-21 0:51 Status To Do 1 0 80644 SLK-37935 30-07-21 19:39 Status Triaged 0 2 0 80644 SLK-37935 26-08-21 6:13 Status Pending Support 27 3 -27 80644 SLK-37935 26-08-21 6:33 Status In Progress 27 4 -17 80644 SLK-37935 08-09-21 13:45 Status Pending Support 40 5 -23 80644 SLK-37935 20-09-21 4:28 Status Pending Investigation 52 6 -14 80644 SLK-37935 22-09-21 14:11 Status Pending Support 54 7 -9 80644 SLK-37935 15-10-21 1:19 Status Information Provided 77 8 -25 80644 SLK-37935 15-10-21 1:19 Status To Do 77 9 -18 80644 SLK-37935 17-10-21 8:29 Status Triaged 79 10 -13 80644 SLK-37935 19-10-21 8:15 Status In Progress 81 11 -1 80644 SLK-37935 19-10-21 8:22 Status Triaged 81 12 6 80644 SLK-37935 24-11-21 9:51 Status Pending Support 117 13 -23 80644 SLK-37935 25-11-21 5:51 Status Information Provided 118 14 -17 80644 SLK-37935 25-11-21 5:51 Status To Do 118 15 -10 80644 SLK-37935 27-11-21 18:35 Status Triaged 120 16 9 80644 SLK-37935 12-12-21 13:22 Status Pending Support 135 17 0 80644 SLK-37935 12-12-21 21:15 Status Information Provided 135 18 1 80644 SLK-37935 13-12-21 13:57 Status To Do 136 19 0 80644 SLK-37935 13-12-21 14:25 Status Triaged 136 20 2 80644 SLK-37935 15-12-21 6:56 Status Pending Support 138 21 2 80644 SLK-37935 17-12-21 6:04 Status Information Provided 140 22 2 80644 SLK-37935 19-12-21 8:19 Status To Do 142 23 0 80644 SLK-37935 19-12-21 8:19 Status Triaged 142 24 1 80644 SLK-37935 20-12-21 6:10 Status Under Analysis 143 25 1 80644 SLK-37935 21-12-21 16:40 Status Pending Support 144 26 13 80644 SLK-37935 03-01-22 10:16 Status Information Provided 157 27 0 80644 SLK-37935 03-01-22 10:16 Status To Do 157 28 2 80644 SLK-37935 05-01-22 13:50 Status Triaged 159 29 4 80644 SLK-37935 09-01-22 11:43 Status Pending Support 163 30 0 80644 SLK-37935 09-01-22 11:43 Status Information Provided 163 31 0 80644 SLK-37935 09-01-22 11:43 Status To Do 163 32 0 80644 SLK-37935 09-01-22 11:43 Status Triaged 163 33 51 80644 SLK-37935 01-03-22 6:31 Status Pending Support 214 34 1 80644 SLK-37935 02-03-22 21:49 Status Information Provided 215 35 0 80644 SLK-37935 02-03-22 21:49 Status To Do 215 36 0 80644 SLK-37935 02-03-22 22:05 Status Triaged 215 37 6 80644 SLK-37935 08-03-22 9:51 Status Pending Support 221 38 3 80644 SLK-37935 11-03-22 20:17 Status Information Provided 224 39 0 80644 SLK-37935 11-03-22 20:17 Status To Do 224 40 2 80644 SLK-37935 13-03-22 12:00 Status Triaged 226 41 10 80644 SLK-37935 23-03-22 11:26 Status Pending Support 236 42 5 80644 SLK-37935 28-03-22 12:43 Status Information Provided 241 43 0 80644 SLK-37935 28-03-22 12:43 Status To Do 241 44 0 80644 SLK-37935 28-03-22 14:56 Status Triaged 241 45 7 80644 SLK-37935 04-04-22 7:24 Status Pending Support 248 46 1 80644 SLK-37935 05-04-22 10:52 Status Triaged 249 47 This is a sample of a much larger table with many more Issue Keys. I can't share all of it.
Thanks