Power Query Number.Random Function Guide
In this article
The Power Query Number.Random function generates a random decimal number between 0 and 1. It's useful for sampling, adding test data, or creating randomized splits in your data. This guide shows you exactly how to use it in Power Query (available in Excel and Power BI), with practical examples and troubleshooting tips.
What You'll Need
- Power Query Editor (in Excel: Data > Get Data > Launch Power Query Editor; in Power BI: Home > Transform Data)
- A data source (a table, CSV, or any query) to which you want to add random values
- Basic familiarity with adding custom columns in Power Query
Step-by-Step
- Open Power Query Editor – Load your data into Power Query as you normally would. For example, in Excel, select your data range and go to Data > From Table/Range. In Power BI, use Get Data and then choose Transform Data.
- Add a custom column – In the Power Query Editor ribbon, go to the Add Column tab and click Custom Column. This opens a dialog where you can write a formula.
- Enter the formula – In the Custom Column Formula box, type
Number.Random(). Note the parentheses – they are required. The function takes no arguments. Give the column a name, such as "RandomValue", and click OK. - Check the result – Power Query adds a new column with a decimal number between 0 and 1 for each row. The numbers are uniformly distributed, meaning any value in that range is equally likely.
- Generate a random integer – If you need a whole number, say between 1 and 10, use a formula like
Number.RoundUp(Number.Random() * 10)orInt(Number.Random() * 10) + 1. The first gives 1 to 10, the second gives 1 to 10 as well but with a slightly different distribution (both are fine for most purposes). - Create a random split (e.g., 80/20) – To randomly assign rows to a training or test set, add a conditional column that checks if the random value is less than 0.8. In the Add Column tab, choose Conditional Column. Set the condition: if your random column is less than 0.8, then output "Train", else "Test".
- Refresh to get new numbers – Each time you refresh the query, the random numbers change. If you need a stable set, you can copy the results and paste as values, or use a seed-based approach (see Tips).
Common Mistakes
- Forgetting parentheses – Writing
Number.Randomwithout parentheses returns a function, not a value. Always useNumber.Random(). - Using it in a calculated column in DAX – This function is for Power Query only. In DAX (for calculated columns or measures), use
RAND()instead. - Expecting integers – The function returns decimals between 0 and 1. If you need integers, you must multiply and round.
- Assuming it is stable – The numbers change every refresh. If you need the same random numbers across refreshes, you must store them or use a seed (see Tips).
- Using it in a custom function without a parameter – If you write a custom function that calls
Number.Random(), each call returns a new random number, which may not be what you expect if you want a single value for the whole table.
Tips
- Use a seed for reproducibility – Power Query does not have a seeded random function, but you can simulate one by adding an index column and using a formula like
Number.Random()combined with a fixed offset. For example, add an index column, then create a custom column:Number.Random() + [Index]– but this is not truly reproducible. A better approach is to useList.Random(1, seed)in a custom column – this returns a list with one random number based on a seed. For example:List.Random(1, 12345){0}gives a reproducible random number. This is useful for test data. - For random integers without bias – Use
Number.RoundDown(Number.Random() * n) + 1to get a number from 1 to n. AvoidNumber.Round()because it can give 0 or n+1 due to rounding. - Combine with sorting – To randomly sort rows, add a random column and then sort by that column. This is a common way to shuffle data.
- Use in sampling – To take a random sample of rows, add a random column, sort by it, and then keep the first N rows using Keep Rows.
- Performance – The function is fast, but if you have millions of rows, adding a random column is still fine. It does not cause a huge performance hit.
Troubleshooting
- I get an error saying "Expression.Error: The name 'Number.Random' wasn't recognized" – This usually means you are not in Power Query. Check that you are in the Power Query Editor, not in a DAX calculated column or measure. If you are in Power Query, ensure there is no typo – it is case-sensitive.
- All random values are the same – This can happen if you use the function in a custom column that is not evaluated per row, such as in a table-level step. For example, if you use
Table.AddColumnwith a constant expression, it might evaluate once. Use the UI's Custom Column which applies per row. - Random numbers change every time I refresh, and I don't want that – As mentioned, you can use
List.Random(1, seed){0}for a reproducible value. Alternatively, after generating the numbers, copy the query results and paste them as values in Excel, or use Table.Buffer in Power Query to freeze the values for the session (though they will still change on refresh). - I need a random date – Generate a random number between 0 and 1, then add it to a base date. For example, to get a random date in 2023:
#date(2023,1,1) + Duration.From(Number.Random() * 365). This gives a date between Jan 1 and Dec 31, 2023. - The function is not available in my version –
Number.Randomhas been part of Power Query since early versions, so it should be available in all current versions of Excel and Power BI. If you are using an older version, consider updating.
Frequently Asked Questions
Can I generate a random integer between 1 and 100?
Yes. Use a custom column with the formula Number.RoundDown(Number.Random() * 100) + 1. This gives a whole number from 1 to 100.
Does Number.Random produce truly random numbers?
It uses a pseudo-random algorithm, which is sufficient for most data tasks like sampling or shuffling. It is not cryptographically secure, so do not use it for security purposes.
How can I make the random numbers stay the same after refresh?
Use a seeded approach: List.Random(1, 42){0} in a custom column. The number 42 is your seed; you can use any integer. This will give the same random number each time you refresh, as long as the row order is unchanged.
Can I use Number.Random in a conditional column?
Yes, but you need to add it as a separate custom column first, then use that column in a conditional column. The conditional column dialog does not allow formulas directly, only column comparisons.
Why do I get a different result each time I refresh?
That is by design. The function recalculates on every refresh. If you need stability, either store the values or use a seed.
Is there a way to generate a random number without adding a column?
You can use the function in a custom step in the formula bar. For example, in the query's formula, you can add a step like Table.AddColumn(#"Previous Step", "Random", each Number.Random()). This is essentially the same as adding a custom column via the UI.