cancel
Showing results for 
Search instead for 
Did you mean: 
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
Microsoft

@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

2 REPLIES 2
MarkLaf
Super User
Super User

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

 

v-sihou-msft
Microsoft
Microsoft

@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
PBI Sept Update Carousel

Power BI September 2023 Update

Take a look at the September 2023 Power BI update to learn more.

Learn Live

Learn Live: Event Series

Join Microsoft Reactor and learn from developers.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

MPPC 2023 PBI Carousel

Power Platform Conference-Power BI and Fabric Sessions

Join us Oct 1 - 6 in Las Vegas for the Microsoft Power Platform Conference.

Top Solution Authors