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
Post Patron
Post Patron

List Generate , how it iterates

Hi, 
I have a question regardign how gen accumulates , I have an accumulation which re starts when 10 is reached / exceeded, 
So this works, 

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

each [ x = [x] + 1, z = alist{x} ,

y = if [y] + z > 10 then z else [y] + z] ,
each [y] )

so from this ; 

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

But if you try an if condtion; 

= let alist = {3} & List.Repeat({2} , 25) in
List.Generate( ()=> [ x = 0 , y = alist{0} ,z = 0 ] ,
each [x] < List.Count(alist) ,
each [ x = [x] + 1, y = [y] + alist{x} , z = if y > 10   then alist{x}   else y ] ,
each [z] )

 then the accumulation does not re start ? , but if the current value = ie 2 then 2 < 10 so [y] + alist{x} 
but that does not seem to be the case, not sure why , 
some advice help sought on this . 

Richard.

1 ACCEPTED SOLUTION

As I wrote above: in that example you are never resetting y. So once y becomes greater than 10, y always remains greater than 10.

Therefore z will always be set to alist{x}

View solution in original post

5 REPLIES 5
Dicken
Post Patron
Post Patron

Thanks, sorry for slow response. 

Omid_Motamedise
Super User
Super User

You can also solve this problem by List.Accumulate

let 
    alist = {3} & List.Repeat({2} , 25) 
in
List.Accumulate(alist, {0},(a,b)=> if List.Last(a)+b>10 then a&{b} else a& {List.Last(a)+b})
If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
ronrsnfld
Super User
Super User

In your non-working snippet, your sequence will not repeat because you never reset "y". So once y>10 = true, it will always be >10 and z will always return alist{x}

let 
    alist = {3} & List.Repeat({2} , 25) 
in
    List.Generate( ()=> [ x = 0 , y = alist{0} ,z = 0 ] ,
    each [x] < List.Count(alist) ,
    each [ x = [x] + 1, 
           y = [y] + alist{x} , 
           z = if y > 10   
                    then alist{x}   
                    else y ] ,
    each [z] )

 

 

Try:

 

 

let 
    alist = {3} & List.Repeat({2} , 25) 
in
    List.Generate( ()=> [ x = 0 , y = alist{0} ,z = 0 ] ,
        each [x] < List.Count(alist) ,
        each [x = [x] + 1, 
              y = if [y] + alist{x} > 10 
                then alist{x} 
                else [y] + alist{x}, 
                z = y ] ,
        each [z] )

 

 

 

although you don't really need z at all

 

 

 

let 
    alist = {3} & List.Repeat({2} , 25) 
in
    List.Generate( ()=> [ x = 0 , y = alist{0}] ,
        each [x] < List.Count(alist) ,
        each [x = [x] + 1, 
              y = if [y] + alist{x} > 10 
                then alist{x} 
                else [y]+alist{x}] ,
        each [y] )

 

 

I still don't see why the condition does not kick in as expected the third example, 
so this works y = if [y] + alist{x} > 10 then alist{x} else [y] + alist{x} ] works, 
but if [y] + alist{x} is declared as a variabel y  
 and then ; if     z = if  y > 10 then alistt{x}    it returns a different incorrect restult? 
that's the question, I not bothered about solving it just why the two operate differently

As I wrote above: in that example you are never resetting y. So once y becomes greater than 10, y always remains greater than 10.

Therefore z will always be set to alist{x}

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.

Top Solution Authors