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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
BartekStr
Regular Visitor

List.Generate() takes only last element from IF statement?

I'm having trouble understanding why List.Generate() seems to take only the last element when an IF statement is used to generate next element. For example:

 

let
        result = List.Generate( () => 
                        [listResult = null, nextOffset = 0, counter = 1],
                        each [counter] <= 2 ,
                        each [
                                listResult0 = {1,2},
                                listResult1 = {3,4},
                                listResult = if counter = 1 then listResult0 else listResult1,
                                counter = [counter] + 1],
                        each [listResult])
in
    result

 



This returns a List of null (default element) and {3,4} as if dureing the first iteration the element {1,2} is not added to the list.

BartekStr_0-1662644771928.png => (expand)

BartekStr_1-1662644803076.png

 

 

Would anyone have an idea why the first element {1,2} is not added to the list?

 

Thanks,

Bartek

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

On the first iteration after the initialization, you have counter = [counter] + 1 = 1 + 1 = 2, so it returns listResult1. You probably want to write "if [counter] = 1" instead of "if counter = 1" since the latter is already incremented. Note that it doesn't matter if you write "counter = [counter] + 1" before or after listResult.

 

You may also want to start your counter at zero:

let
    result =
        List.Generate(
            () => [listResult = null, nextOffset = 0, counter = 0],
            each [counter] <= 2 ,
            each [
                    listResult0 = {1,2},
                    listResult1 = {3,4},
                    listResult = if [counter] = 1 then listResult0 else listResult1,
                    counter = [counter] + 1
                ],
            each [listResult]
        )
in
    result

View solution in original post

2 REPLIES 2
AlexisOlson
Super User
Super User

On the first iteration after the initialization, you have counter = [counter] + 1 = 1 + 1 = 2, so it returns listResult1. You probably want to write "if [counter] = 1" instead of "if counter = 1" since the latter is already incremented. Note that it doesn't matter if you write "counter = [counter] + 1" before or after listResult.

 

You may also want to start your counter at zero:

let
    result =
        List.Generate(
            () => [listResult = null, nextOffset = 0, counter = 0],
            each [counter] <= 2 ,
            each [
                    listResult0 = {1,2},
                    listResult1 = {3,4},
                    listResult = if [counter] = 1 then listResult0 else listResult1,
                    counter = [counter] + 1
                ],
            each [listResult]
        )
in
    result

Thanks, that explains! 🙂

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors