Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Calculated Column: TIme difference to next rowset

Hello:  I am writing to ask if it is possible to create a calculated column which calculates the time difference ( Duration ) between the next rowset ( timestamp ) ? Please see example below. Thank ...
  • Jimmy801's avatar
    5 years ago

    Hello Anonymous 

     

    if you need a solution in Power Query then you can add a custom column with this formula. You have to replace the "PreviousStep" with the name of your previous step. Be aware that i use your index column for referencing the "next" step. Here the formula

     

    try Duration.TotalMinutes(Table.SelectRows(PreviousStep, (sel)=> sel[Index] = [Index]+1)[timestamp]{0} - [timestamp]) otherwise null

     

    here a complete code example to understand it better

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rZLRagIxEEV/ZdjXik5msju6b7qmNWCzkGixyP7/b5jYbbUSIbA+JJCTmcnhkvO5YmSsZlVAhfC57nbWGbAB/NE56z7ijcT1e3GjvOAFISlQTUuYBc0y7t1JLwnS9L/DZh3MPk3rnRnrhlkSwZyI3e5TFd1ZjOjfi5IFzapQYTUqTM6CH7P4AYJlIrFuchaas0BUoYJ6VRbPRKhQhKZmIS1xFggXKvBrspCWKQtEF4roq0it04d+i6UKjPe9h3d7Ohy9AQXdd2zYGh+HzxlcfwBvvowPqV/d+V37woNNjVkgdaFeXQ3DBQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"rror num" = _t, #"error text" = _t, #"category num" = _t, #"category text" = _t, timestamp = _t, #"time no millis" = _t, Index = _t, #"short name" = _t, #"Line Name" = _t, IndexAll = _t]),
        PreviousStep = Table.TransformColumnTypes(Source,{{"rror num", Int64.Type}, {"error text", type text}, {"category num", Int64.Type}, {"category text", type text}, {"timestamp", type datetime}, {"time no millis", type datetime}, {"Index", Int64.Type}, {"short name", type text}, {"Line Name", type text}, {"IndexAll", Int64.Type}}),
        #"Added Custom" = Table.AddColumn(PreviousStep, "Custom", each try Duration.TotalMinutes(Table.SelectRows(PreviousStep, (sel)=> sel[Index] = [Index]+1)[timestamp]{0} - [timestamp]) otherwise null)
    in
        #"Added Custom"

     

    output

     

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy