Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

I dont understand properly how List.Generate() works

    let my_list = List.Generate( ()=>[x=1], // Failr here with: "ExpressionError: The name 'x' wasn't recogniced. Make sure it's spelled correctly." each [x]<101, each ...
  • AntrikshSharma's avatar
    3 years ago

    Anonymous The issue is at the third argument of List.Generate, when incrementing you need to consider that the current iterated value is a record so you need to use each [x= [x]+1 ]

    Complete code:

    let
        my_list = 
            List.Generate (
                () => [ x = 1 ],                                                                                                                                                                                                                                                                                                                      
                each [x] < 101,
                each [ x = [x] + 1 ],
                each Number.Round ( Number.RandomBetween ( 1, 101 ), 0 )
            )
    in
        my_list

    If there isn't any other logic that you are going to use in List.Generate then you can also try List.Trasnform

    = 
    List.Transform ( 
        { 1.. 100 }, 
        each Number.Round ( Number.RandomBetween ( 1, 101 ), 0 ) 
    )