Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Generating a random sequence of numbers

Hi all,

I'd like to know if there's a way in M to generate a random sequence of numbers of predefined lenght in each row

I need this because I'm creating a demo of a report and I'd like to add the phone number of the users in a table but I have to randomly generate it as I cannot show real people telephone numbers 

Therefore the requirement is to generate, in each row, a random sequence of 8 numbers, with as less effort as possible 

Thanks

  • Anonymous's avatar
    Anonymous
    6 years ago

    here it is with effort zero (from you 😁)

     

     

    let
    telnum= (digits,seed) =>Text.Combine(List.Transform(List.Random(digits,seed), each Text.From(Number.RoundDown(_*10)))),
    lst=List.Transform({1..20}, each telnum(8,_) )
    in
    lst

     

  • Hi, Anonymous 

     

    You may try what is suggested by Anonymous . I created data to reproduce your scenario. The pbix file is attached in the end.

     

    Table:

     

    Here is the codes in 'Advanced Editor'.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclSK1YlWcgKTzmDSBUy6KsXGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
        temp = (digits,seed) =>Text.Combine(List.Transform(List.Random(digits,seed), each Text.From(Number.RoundDown(_*10)))),
        #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each List.Transform({1..20}, each temp(8,_)){[Index]}),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"})
    in
        #"Removed Columns"

     

    Result:

     

    Best Regards

    Allan

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

3 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    here it is with effort zero (from you 😁)

     

     

    let
    telnum= (digits,seed) =>Text.Combine(List.Transform(List.Random(digits,seed), each Text.From(Number.RoundDown(_*10)))),
    lst=List.Transform({1..20}, each telnum(8,_) )
    in
    lst

     

  • v-alq-msft's avatar
    v-alq-msft
    Community Support

    Hi, Anonymous 

     

    You may try what is suggested by Anonymous . I created data to reproduce your scenario. The pbix file is attached in the end.

     

    Table:

     

    Here is the codes in 'Advanced Editor'.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclSK1YlWcgKTzmDSBUy6KsXGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
        temp = (digits,seed) =>Text.Combine(List.Transform(List.Random(digits,seed), each Text.From(Number.RoundDown(_*10)))),
        #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each List.Transform({1..20}, each temp(8,_)){[Index]}),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"})
    in
        #"Removed Columns"

     

    Result:

     

    Best Regards

    Allan

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.