Forum Discussion
CahabaData
8 years agoMemorable Member
Running Sum variant
say there is a Values column 5 5 5 -2 -1 5 the Running Sum measure would be: 5 10 15 13 12 17 if one needed a maximum of 12 and applied a simple IF/Switch result would be 5 10 ...
- 8 years ago
It should be something like this:
let Source = Excel.Workbook(File.Contents("D:\Projects\Internal CRM\Leave Data.xlsx"), null, true), Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data], #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]), #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}, {"Type", type text}, {"Debit/Credit", type number}}), RunningSum = List.Skip(List.Accumulate(#"Changed Type"[#"Debit/Credit"],{0},(sum,value) => sum & {List.Min({12,List.Last(sum)+value})})), TableWithRunningSum = Table.FromColumns(Table.ToColumns(#"Changed Type")&{RunningSum},Value.Type(Table.AddColumn(#"Changed Type","Running Sum", each 0, type number))) in TableWithRunningSum
MarcelBeug
8 years agoCommunity Champion
You're right. A few occurrences of "MyTable" in the function must be replaced by "SortedOnDate":
fnRunningSum = (MyTable as table, MyDateColumn as text, MyLeaveColumn as text, MyRunningSumColumnName as text, MyMaxValue as number) as table =>
let
SortedOnDate = Table.Sort(MyTable, {MyDateColumn, Order.Ascending}),
RunningSum = List.Skip(List.Accumulate(Table.Column(SortedOnDate,MyLeaveColumn),{0},(sum,value) => sum & {List.Min({MyMaxValue,List.Last(sum)+value})})),
TableWithRunningSum = Table.FromColumns(Table.ToColumns(SortedOnDate)&{RunningSum},Value.Type(Table.AddColumn(SortedOnDate,MyRunningSumColumnName, each 0, type number)))
in
TableWithRunningSum,Anonymous
8 years agoNot applicable
Works perfect!
Thank you very much! and sorry for troubleing too much for this one!