March 13, 2022
Why does this code work? While the type of items in the Custom column is a list, it is added with s which is a number
let
Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
AddedIndex = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
AddedCustom = Table.AddColumn(AddedIndex, "Custom", each List.FirstN(AddedIndex[QTY] , [Index])),
RT = Table.AddColumn(AddedCustom, "RunningTotal", each List.Accumulate(
[Custom] ,
0 ,
(S , C )=> S + C
))
in
RT
Trusted Members
Moderators
November 1, 2018
March 13, 2022
Hello, yes, you are right, but I wanted to use this example and several other examples to introduce List. Accumulate. Do you agree that the first argument of a list contains several sub-lists, so C refers to each sub-list? To be honest, I still don't understand
Thank you for your reply
QTY
Qumars | 10 |
Fardin | 20 |
Beyk | 15 |
Halou | 30 |
Trusted Members
Moderators
November 1, 2018
No, not quite. You are using each List.Accumulate, so the List.Accumulate is processing the list in each row of the Custom column, one at a time. It is not processing the whole column in one go.
Each time it is called, you pass a list of numbers to it. The starting value is 0 (the second argument) then it iterates through the numbers in the list (C is the number currently being processed and S is the running total that gets added to each time).
Let's say the list in the first row is {1,3,5}. The List.Accumulate starts with S=0 and at the first pass C = 1 and that is added to S so S is now 1. At the next iteration, C = 3, which is added to S so S is now 4. At the third and final pass, C = 5 and that is added to S, so that is now 9 and that is the final result for that row. The each iterator then moves onto the next row, and does the same process with the list of numbers in the Custom column for that row.
Does that make sense?
1 Guest(s)