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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
SaGuJO
Frequent Visitor

How do I keep a random number from changing every step of a Query?

Hi

If I write code like this, the generated number changes every step of the Query which might give unexpected results.. Is there anyway to make Number.Random evalute once and keep exactly that result for the rest of the Query?

let
    Source = Number.Random(),
    Buffered = List.Buffer({Source}),
    Rnd = Buffered
in
    Rnd

1 ACCEPTED SOLUTION
v-sihou-msft
Microsoft Employee
Microsoft Employee

@SaGuJO

 

No, it's not possible. You can understand that Power Query is not executed step by step. It's always evaluated up to current step so that the result from Number.Random() will change in each step.

 

Regards, 

View solution in original post

3 REPLIES 3
MarkLaf
Solution Sage
Solution Sage

Don't mean to ressurect this, but I was also looking for an answer to this question and looked here. I figured out a workaround: use List.Random, which let's you specify a seed. You can generally leverage other parts of your data to apply some variation to the seed.

 

Here is an example. I start in Source with a single-column table with a few Departments. Then, in next step, RandomExample, I add a "random" number custom column. Note the innermost portion is the List.Random function where we create 15 numbers with a seed and then add variation to said seed with text from [Department] (length * character number of 3rd character). We then aggregate the list from List.Random with List.Add. I threw some other variations with Mod, which let's you just get just the decimal (more likely to get larger variation in numbers I found if you catch some leading 0's).

 

 

let
    Source = 
    Table.FromColumns( 
        {{"Finance","Human Resources","Sales","Research","Legal"}}, 
        type table [Department=text] 
    ),
    RandomExample = 
    Table.AddColumn( 
        Source, 
        "Consistent Random", 
        each Number.Mod( 
            List.Sum( 
                List.Random( 
                    15, 
                    101023 
                    * Text.Length([Department]) 
                    * Character.ToNumber(Text.Middle([Department],2,1) ) 
                )                    
            ),
            1
        )*100, 
        type number
    )
in
    RandomExample

 

 

Output:

MarkLaf_0-1646101569548.png

 

To add some more details on using seed: I've found this article that suggest to use the second parameter of List.Random() fucntion - seed: "If ... you want the list to be generated randomly only once, and doesn’t change with every function call, then you can specify the seed parameter".  - Generate Random List of Numbers in Power BI Dataset Using Power Query - RADACAD

I've tested it and confirm that it works as expected: the random list is generating only once at the  moment of semantic model refresh. However, you need to keep the same seed number within semantic model, to obtain it it's possible to use DateTime.LocalNow() funciton without seconds part (if your model is refrehsing within a minute 🙂 )

Hope someone will find it useful as well 🙂

v-sihou-msft
Microsoft Employee
Microsoft Employee

@SaGuJO

 

No, it's not possible. You can understand that Power Query is not executed step by step. It's always evaluated up to current step so that the result from Number.Random() will change in each step.

 

Regards, 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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