Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Number.RandomBetween Function Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The Number.RandomBetween function in Power Query generates a random decimal number between two specified values. It is useful for creating sample data, adding jitter to metrics, or simulating scenarios. You need Power Query (available in Excel and Power BI) and a query where you want to add randomness. The function is simple: Number.RandomBetween(bottom, top), where bottom is the lower bound and top is the upper bound. The result is a decimal number greater than or equal to bottom and less than top. Note that the function is non-deterministic—it returns a different value each time the query refreshes.

What You'll Need

  • Power Query Editor (available in Excel 2016 or later, or Power BI Desktop).
  • A data source or a blank query to test the function.
  • Basic familiarity with the Power Query interface, such as the formula bar and the Add Column tab.

Step-by-Step

  1. Open Power Query Editor. In Excel, go to the Data tab and click From Table/Range (or Get Data > Launch Power Query Editor). In Power BI, go to Home > Transform Data.
  2. Create a new query or use an existing one. For a quick test, create a blank query: in the Home tab, click New Source > Blank Query. Alternatively, use an existing query where you want to add random numbers.
  3. Add a custom column. Go to the Add Column tab and click Custom Column. This opens a dialog where you can enter a formula.
  4. Enter the formula. In the Custom Column Formula box, type Number.RandomBetween(10, 20) (replace 10 and 20 with your desired bounds). Give the column a name, such as "RandomValue", and click OK.
  5. Check the result. A new column appears with decimal numbers between 10 and 20. The values will be different each time you refresh the query.
  6. Use dynamic bounds. Instead of hard-coded numbers, you can reference columns. For example, if you have columns named "Min" and "Max", use Number.RandomBetween([Min], [Max]).
  7. Convert to integer if needed. To get whole numbers, wrap the function with Number.Round or use Int32.From. For example: Number.Round(Number.RandomBetween(1, 10)) gives a rounded decimal. To get a true integer, use Int32.From(Number.RandomBetween(1, 11))—note that the upper bound is exclusive, so to include 10, use 11.
  8. Refresh to get new values. In Excel, go to Data > Refresh All. In Power BI, click Refresh in the Home tab. Each refresh recalculates the random numbers.

Common Mistakes

  • Using the wrong order of arguments. The syntax is Number.RandomBetween(bottom, top). Swapping them causes an error because the bottom must be less than the top.
  • Expecting integers. The function returns decimals. If you need whole numbers, you must explicitly round or convert.
  • Assuming the upper bound is inclusive. The result is always less than the top value. If you want to include the top, add 1 to it (for integers) or use a slightly larger top.
  • Using it in a calculated column in DAX. This function is only available in Power Query (M language), not in DAX. In DAX, use RAND() or RANDBETWEEN().
  • Forgetting that it recalculates on refresh. If you need stable random values, you must generate them once and store them, or use a seed-based approach (which Power Query does not natively support).

Tips

  • Use it for sample data. Generate random ages, prices, or scores for testing dashboards.
  • Combine with other functions. For example, Number.RandomBetween(0, 1) gives a random decimal between 0 and 1, which you can use for probability simulations.
  • Create a list of random numbers. Use List.Generate or List.Transform with Number.RandomBetween to generate multiple values at once.
  • Use it in conditional columns. For example, assign a random discount percentage based on a category.
  • Be aware of performance. Generating large numbers of random values can slow down refresh, especially if used in many rows. Consider generating a smaller set and reusing it.

Troubleshooting

If the function returns an error or unexpected results, check the following:

  • Error: "The value isn't a number." This means one of the arguments is not a numeric value. Ensure that any column references are numeric and do not contain nulls or text. Use Number.From to convert if needed.
  • Error: "Bottom is greater than or equal to top." The function requires bottom < top. Check your values, especially if using dynamic references. If the top can be less than the bottom, use Number.RandomBetween(Number.Min([Min],[Max]), Number.Max([Min],[Max])).
  • Random values are the same on each refresh. This can happen if Power Query caches results. Try changing a step or using Number.Random() instead. Also ensure the query is actually refreshing—check the data source settings.
  • Values are not changing when you refresh. In Excel, you may have the workbook calculation set to manual. Change it to automatic, or press Ctrl+Alt+F9 to force a full recalculation.
  • Function not recognized. This function was introduced in Power Query version 2.35. If you are using an older version, update Excel or Power BI. In older versions, use Number.Random() and scale it: bottom + (top - bottom) * Number.Random().

Frequently Asked Questions

Can I get a random integer between 1 and 10?

Yes. Use Int32.From(Number.RandomBetween(1, 11)) because the upper bound is exclusive. This gives integers 1 through 10.

Does Number.RandomBetween work in DAX?

No. It is an M function used in Power Query. In DAX, use RAND() or RANDBETWEEN() for calculated columns.

How can I make the random values stay the same?

Power Query has no built-in seed. To keep values stable, generate the random numbers once, copy the results as a static table, or use a separate query that you do not refresh.

Can I use negative numbers as bounds?

Yes. The function works with any numeric values, including negatives, as long as the bottom is less than the top.

What is the difference between Number.Random and Number.RandomBetween?

Number.Random() returns a decimal between 0 and 1. Number.RandomBetween scales that to your specified range. You can replicate it with bottom + (top - bottom) * Number.Random().

Why does the function return decimals even when I use whole numbers?

Because the function is designed to return a decimal. To get whole numbers, you must round or convert as shown above.