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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

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 [x=x+1],
        each Number.Round(Number.RandomBetween(1,101),0)
    )
in
    my_list

 

 

Im just trying to create a list with length 100 filled with random numbers from 1 to 100. I wanna know why It doesn´t work.

 

this is the example from the docs I was looking at before:

 

 

List.Generate(
    () => [x = 1, y = {}],
    each [x] < 10,
    each [x = List.Count([y]), y = [y] & {x}],
    each [x]
)

 

 

You can check that example at (https://learn.microsoft.com/en-us/powerquery-m/list-generate)

1 ACCEPTED SOLUTION
AntrikshSharma
Community Champion
Community Champion

@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 ) 
)

 

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Thanks a lot

AntrikshSharma
Community Champion
Community Champion

@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 ) 
)

 

Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors