Forum Discussion
List.Generate() takes only last element from IF statement?
- 3 years ago
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
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
resultThanks, that explains! 🙂