Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Dicken
Responsive Resident
Responsive Resident

List.Generate how it updates

Hi, 
I was comparing the the standard way of an accumulation with list.generate ;  but using a third variable,  
but it does not work and im not sure why,   can someone help of point me in  the direction of some theory on 
this;  so 
here  A; works, and B; does not, so  [y ] + alist {x}  <>   [y] + y  where y = alist {x}   

A; 
= let  alist =
 {2,1,2,3,2,2,1,2,3,1,2,2,1,2,3,2,3,2,1,2}
in 
List.Generate( ()=> [ x = 0 , y = alist{0}] , 
                    each [x] < List.Count( alist), 
                    each [x = [x] + 1, y = [y] + alist{x} ], 
                    each [y] )
B: 

let  alist =
 {2,1,2,3,2,2,1,2,3,1,2,2,1,2,3,2,3,2,1,2}
in 
List.Generate( ()=> [ x = 0, y = alist{0}, z = y  ],
                    each [x] < List.Count( alist),
                    each [x = [x] + 1, y = alist{x} , z = [y] + y ] , each [z] )

Richard 

2 ACCEPTED SOLUTIONS
AlexisOlson
Super User
Super User

In the second example, by substituting, we can see that

z = [y] + y
  = alist{[x]} + alist{x}
  = alist{[x]} + alist{[x]+1}

Thus z is always the sum of the trailing two alist items.

 

In the first example, y adds onto it's prior accumulated value rather than resetting as alist{x}. Let each set of [brackets] denote the value from the previous iteration. Then

y = [y] + alist{x}
  = [y] + alist([x]+1}
  = [[y]] + alist{[x]} + alist{[x]+1}
  = [[[y]]] + alist{[[x]]} + alist{[x]} + alist{[x]+1}
  = ...
  = alist{0} + ... + alist{[x]} + alist{[x]+1}

This is a cumulative sum rather than just the trailing two items.

 

The key difference is a self-referential update step. z doesn't accumulate because it never refers to its value from prior iterations.


Here's what the iterations look like as a table:

AlexisOlson_0-1749240078985.png

View solution in original post

burakkaragoz
Community Champion
Community Champion

Hi @Dicken ,

 

@AlexisOlson  has already provided a great explanation of why your two List.Generate approaches behave differently. If you’re interested in even more flexibility or want to experiment with more advanced accumulations, here are some extra tips:

  • You can manage multiple accumulators by extending your record in List.Generate (e.g., [x=..., y=..., z=...]) and updating each one based on previous iteration values.
  • For more complex rolling calculations (like moving averages or conditional accumulations), add fields to your state record and update them step by step—this lets you track any state across iterations.
  • If you want to “debug” or visualize how your state evolves, output the full record at each step (not just one variable). This helps you see exactly how values are carried forward or reset.
  • Beyond List.Generate, consider List.Accumulate for cases where you want to process a list with an accumulator function. Sometimes it’s a bit more straightforward for cumulative calculations.

If you have a specific accumulation or algorithm in mind beyond what’s already solved, feel free to share it—happy to dive deeper or optimize it together!

Happy coding!

View solution in original post

3 REPLIES 3
burakkaragoz
Community Champion
Community Champion

Hi @Dicken ,

 

@AlexisOlson  has already provided a great explanation of why your two List.Generate approaches behave differently. If you’re interested in even more flexibility or want to experiment with more advanced accumulations, here are some extra tips:

  • You can manage multiple accumulators by extending your record in List.Generate (e.g., [x=..., y=..., z=...]) and updating each one based on previous iteration values.
  • For more complex rolling calculations (like moving averages or conditional accumulations), add fields to your state record and update them step by step—this lets you track any state across iterations.
  • If you want to “debug” or visualize how your state evolves, output the full record at each step (not just one variable). This helps you see exactly how values are carried forward or reset.
  • Beyond List.Generate, consider List.Accumulate for cases where you want to process a list with an accumulator function. Sometimes it’s a bit more straightforward for cumulative calculations.

If you have a specific accumulation or algorithm in mind beyond what’s already solved, feel free to share it—happy to dive deeper or optimize it together!

Happy coding!

Thnks you both will follow up the link. 

Richard.

AlexisOlson
Super User
Super User

In the second example, by substituting, we can see that

z = [y] + y
  = alist{[x]} + alist{x}
  = alist{[x]} + alist{[x]+1}

Thus z is always the sum of the trailing two alist items.

 

In the first example, y adds onto it's prior accumulated value rather than resetting as alist{x}. Let each set of [brackets] denote the value from the previous iteration. Then

y = [y] + alist{x}
  = [y] + alist([x]+1}
  = [[y]] + alist{[x]} + alist{[x]+1}
  = [[[y]]] + alist{[[x]]} + alist{[x]} + alist{[x]+1}
  = ...
  = alist{0} + ... + alist{[x]} + alist{[x]+1}

This is a cumulative sum rather than just the trailing two items.

 

The key difference is a self-referential update step. z doesn't accumulate because it never refers to its value from prior iterations.


Here's what the iterations look like as a table:

AlexisOlson_0-1749240078985.png

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.