Forum Discussion
donpep0
3 years agoFrequent Visitor
Repeat steps multiple times over the same table
Hi all! I'm a little stuck whit power Query, I'm trying to repeat multiple steps over the same table. My input(Source) looks like this: Date Index Volume 9/01 1 1100 9/02 2 1...
jbwtp
3 years agoMemorable Member
Hi donpep0,
You can try something like this:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WstT1SsxT0lEyBGFDAwOlWB2QoFtqElDACCxoaQoV9E0sAgoYgwSNLaFijgUgMROQmIGhMVxhJVDAFIjNLGAmepWCrDEDYTNjuFgOkG8OxDDTStOBHAuEQHBqAZBjCRaIBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Index = _t, Volume = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Index", Int64.Type}, {"Volume", Int64.Type}}),
Calculate = List.Accumulate(Table.ToRecords(#"Changed Type"), {[lst = {}]}, (a, n)=>
let
last = List.Last(a)[lst],
updateVolume = if n[Volume] = null then Record.TransformFields(n, {"Volume", each List.Average(last)}) else n,
updateLst = if List.Count(last) = 4 then List.Skip(last) else last,
addLst = Record.AddField(updateVolume, "lst", updateLst & {updateVolume[Volume]})
in a & {addLst}),
Output = Table.FromRecords(List.Skip(Calculate), Value.Type(#"Changed Type"))
in
Output
Kind regards,
John