Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Adding Duration to time in previous row within the same currently calculated column

Hello Everybody,   Working on Power Query and ran into some obstacles I would like to make the table add durations to the time in the previous rows   Currently my table looks like this: Item No...
  • Jimmy801's avatar
    6 years ago

    Hello Anonymous 

     

    you can use List.Generate to achive this. Here the example with your data provided

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSAWNLKwMDpVidaCUjIMcIWcAYyDFGFjABckzgArEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Item No." = _t, Duration = _t, #"Start time" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item No.", Int64.Type}, {"Duration", Int64.Type}, {"Start time", type time}}),
        #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Item No.", Order.Ascending}}),
        CreateStartEnd = List.Generate
        (
            ()=> [Counter = 0, Start= #"Sorted Rows"[Start time]{0}, End= #"Sorted Rows"[Start time]{0} + #duration(0,#"Sorted Rows"[Duration]{0},0,0)],
            each [Counter]<= Table.RowCount(#"Sorted Rows")-1,
            each 
            [
                Counter = [Counter]+1,
                Start = [End],
                End= [End]+ #duration(0,#"Sorted Rows"[Duration]{[Counter]+1},0,0)
            ],
            each {[Start], [End]}
        ),
        ToRows = Table.FromRows(CreateStartEnd, {"Start", "End"}),
        Combine = Table.FromColumns(Table.ToColumns(#"Sorted Rows")&Table.ToColumns(ToRows),Table.ColumnNames(#"Sorted Rows")&Table.ColumnNames(ToRows))
    
    in
        Combine

    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