Forum Discussion
Adding Duration to time in previous row within the same currently calculated column
- 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 CombineCopy 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
Anonymous
This style of code should feel some operational efficiency improvements when the data volume is large.
ziying35 where did you read about this operator?
My previous comment was related to a guess I made to change you code inside List.Generator in this way (I forgot to update the lines involved):
from this:
{List.FirstN(rows{_{1}}, 2)&{_{0}{3}?}&{_{0}{3}?+dur}, i}??{rows{_{1}}&{rows{_{1}}{2}+dur}, i},
ti this
{List.FirstN(rows{_{1}}, 2)&{_{0}{3}?}&{_{0}{3}?+dur}??rows{_{1}}&{rows{_{1}}{2}+dur}, i}
But the problem seems derive from the fact that {null} in not equal to null.
From this I guess that this operator is mainly usefull for scalar values
In any case my proposal to the problem:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSAWNLpVidaCUjIMsIzjMGsozhPBMgywTCiwUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [item = _t, dur = _t, start = _t]),
ct = Table.TransformColumnTypes(Source,{{"item", Int64.Type}, {"dur", Int64.Type}, {"start", Int64.Type}}),
#"Added Custom" = Table.AddColumn(ct, "end", each [start]+List.Sum(List.FirstN(ct[dur], List.PositionOf(ct[dur],[dur])+1))),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"start"}),
#"Added Custom1" = Table.AddColumn(#"Removed Columns", "start", each [end]-[dur])
in
#"Added Custom1"
PS
ziying35 why do you use list instead record inside list.generate?
why
- ziying356 years ago
Impactful Individual
Anonymous
I think I saw this code syntax article on Chris's blog, I can't remember exactly.It's a habit for me to use list instead of record in the code I write for the List.Generate function, and add some notes as appropriate.Depending on the problem scenario, I also write my code using the record method
PS:
The above information is translated via DeepL, hopefully you can understand the general idea, it may not be syntactically correct