Forum Discussion

Kahrax's avatar
Kahrax
Frequent Visitor
5 years ago

Aggregating identical trailing rows

Consider the following table

IndexTimestamp  Text  Text ChangeDuration
101.01.2021 12:00:00  Text B  true90
201.01.2021 13:30:00  Text B  false30
301.01.2021 14:00:00   true60
401.01.2021 15:00:00  Text A  true105
501.01.2021 16:45:00  Text A  false15
601.01.2021 17:00:00  Text A  false60
701.01.2021 18:00:00  Text C  true80
801.01.2021 19:20:00  Text B  true40
901.01.2021 20:00:00 Text C true60

 

What I want to achieve in the end is show the duration of each text before it changes. To do this I want to aggregate the duration value for identical trailing rows, based on the text field. Se below table: 

IndexTimestamp  Text  Text ChangeDurationAggregated duration
101.01.2021 12:00:00  Text B  true90120
201.01.2021 13:30:00  Text B  false30120
301.01.2021 14:00:00   true6060
401.01.2021 15:00:00  Text A  true105180
501.01.2021 16:45:00  Text A  false15180
601.01.2021 17:00:00  Text A  false60180
701.01.2021 18:00:00  Text C  true8080
801.01.2021 19:20:00  Text B  true4040
901.01.2021 20:00:00  Text C  true6060

 

After this is done I can just filter on "Text Change = true" to hide text duplicates. 

 

Any ideas?

3 Replies

  • Does that mean you don't care about the timestamp for index 2 for example?

    • Kahrax's avatar
      Kahrax
      Frequent Visitor

      As for the final outcome you are correct. In other words, any timestamp in rows with 'Text change' = false is irrelevant. Only difference in time between rows with 'Text change' = true is relevant. 
      E.g. difference in time between index 1 and 3, 3 and 4, 4 and 7 etc. 

  •  

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddDNCsIwDAfwVxk9D0nSj7W9qa/grezgYZ486QQf32xxlkaFQEvIr/nTUgya3gDuuAgIO6QMwMXd0/ScuwNf5ttj4iOBGftiSAGbrQaX8/W+CCvCKuE+K+rjQUadGvVtmn0FCH4VXomQnVdii4MighLD945NvFMNSsRWHGuqKCAqkDL9+VQnILVgnf69YYk0vgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, Timestamp = _t, #"  Text" = _t, #"  Text Change" = _t, Duration = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Duration", Int64.Type}, {"Index", Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each List.PositionOf(List.Skip(#"Changed Type"[#"  Text Change"],[Index]),"true")),
        #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Custom", Int64.Type}}),
        #"Added Custom1" = Table.AddColumn(#"Changed Type1", "Aggregated duration", each List.Sum(Table.SelectRows(#"Changed Type1",(k)=>k[Index]>=[Index] and k[Index]<=[Index]+[Custom])[Duration])),
        #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"Index", "Timestamp", "  Text", "  Text Change", "Duration", "Aggregated duration"}),
        #"Filtered Rows" = Table.SelectRows(#"Removed Other Columns", each ([#"  Text Change"] = "true"))
    in
        #"Filtered Rows"

     

     

     

    not very elegant but works.

    I'll leave the handling of the null in the last row up to you.