-
Notifications
You must be signed in to change notification settings - Fork 74
/
Table.ColumnRunningTotal.pq
43 lines (41 loc) · 2.22 KB
/
Table.ColumnRunningTotal.pq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
let
func = (Table as table, SortColumn as text, AmountColumn as text) =>
let
/* Debug parameters
Table = #"Price Paid",
SortColumn = "Date",
AmountColumn = "Price paid",
*/
// Sort table and buffer it
Sorted = Table.Buffer(Table.AddIndexColumn(Table.Sort(Table,{{SortColumn, Order.Ascending}}), "Index",1,1)),
// Select the Columns
SelectColumns = Table.SelectColumns(Sorted, {SortColumn, AmountColumn, "Index"}),
// Extract Amount column and buffer it
ExtractAmountColumn = List.Buffer(Table.Column(SelectColumns, AmountColumn)),
// Calculate a list with all running Totals
RunningTotal = List.Skip(List.Generate( ()=> [ListItem=0, Counter=0]
,each [Counter] <= List.Count(ExtractAmountColumn)
,each [ ListItem = ExtractAmountColumn{[Counter]}+[ListItem],
Counter = [Counter]+1
]
),1),
ConvertedTable = Table.FromList(RunningTotal, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
ExpandedColumn = Table.ExpandRecordColumn( ConvertedTable, "Column1", {"ListItem", "Counter"}, {"ListItem", "Counter"}),
MergedQueries = Table.NestedJoin(Sorted,{"Index"}, ExpandedColumn,{"Counter"},"Expanded Column1",JoinKind.LeftOuter),
Expand = Table.ExpandTableColumn( MergedQueries, "Expanded Column1", {"ListItem"}, {"RunningTotal"}),
#"Removed Columns" = Table.RemoveColumns(Expand,{"Index"}),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"RunningTotal", type number}})
in
#"Changed Type"
//*
,
documentation = [ Documentation.Name = " Table.ColumnRunningTotal"
,Documentation.Description = " Fast way to add running total to a table"
,Documentation.LongDescription = " Fast way to add running total to a table"
,Documentation.Category = " Table"
,Documentation.Source = " local"
,Documentation.Author = " Imke Feldmann: www.TheBIccountant.com"
,Documentation.Examples = {[Description = " ", Code = " ", Result = " "]}]
in
Value.ReplaceType(func, Value.ReplaceMetadata(Value.Type(func), documentation))
//*/