Forum

A strange thing hap...
 
Notifications
Clear all

A strange thing happened in Power Query

5 Posts
2 Users
0 Reactions
252 Views
(@farshad4510)
Posts: 19
Eminent Member
Topic starter
 

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

 
Posted : 29/09/2024 2:24 pm
(@debaser)
Posts: 837
Member Moderator
 

For each row, the List.Accumulate is processing each item in the list that is in the Custom column, and it loops through that list adding each item to the current total. In all honesty List.Accumulate is a bit overkill there as you could just use List.Sum

 
Posted : 01/10/2024 5:02 am
(@farshad4510)
Posts: 19
Eminent Member
Topic starter
 

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

 

 
 
Posted : 01/10/2024 2:19 pm
(@debaser)
Posts: 837
Member Moderator
 

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?

 
Posted : 02/10/2024 5:30 am
(@farshad4510)
Posts: 19
Eminent Member
Topic starter
 

hello again Your explanation was excellent, I understood. I think the main point was in "each". thank you very much

I have to emphasize that I used Google translator for communication, if it translated something wrong. Forgive me, it is definitely not Google's fault

 
 
Posted : 02/10/2024 12:21 pm
Share: