The M Code Behind the Power Query M function Number.Random

In this article, we’ll dive into the M code that powers Number.Random, exploring its syntax and functionality.

Syntax of the Number.Random Function

The syntax of the Number.Random function is straightforward. It takes no arguments and returns a decimal number between 0 and 1. Here’s the basic syntax:


That's it! When you call the Number.Random function, it generates a random decimal number.

The M Code Behind Number.Random

Behind the scenes, the M code that powers Number.Random is a bit more complex. Here's what it looks like:


Source = #table({“”}, {{1}}),

RandomNumber = Number.RandomBetween(0, 100000) / 100000



Let's break this down line by line.

Line 1: Creating a Table

The first line of the code creates a table with a single column and a single row. This table is essentially just a placeholder that allows us to generate a random number.

Source = #table({“”}, {{1}})

The #table function creates a table with the specified columns and rows. In this case, we're creating a table with a single column named "" (an empty string) and a single row with the value 1.

Line 2: Generating a Random Number

The second line of the code generates a random number between 0 and 100000 using the Number.RandomBetween function.

RandomNumber = Number.RandomBetween(0, 100000) / 100000

The Number.RandomBetween function generates a random integer between two specified values. In this case, we're generating a random number between 0 and 100000.

We then divide this number by 100000 to convert it to a decimal between 0 and 1.

Line 3: Returning the Random Number

The final line of the code returns the random number.


Using Number.Random in Power Query

Now that we understand the M code behind Number.Random, let's take a look at how we can use it in Power Query.

To generate a random number between 0 and 1, we simply call the Number.Random function in a new column:

1. Open Power Query and load your data.

2. Select the column where you want to generate random numbers.

3. Click the "Add Column" tab and select "Custom Column."

4. In the "Custom Column" dialog, enter the following formula:


5. Click "OK" to create the new column.

That's it! Power Query will generate a random number for each row in the selected column.

The Number.Random function is a simple yet powerful tool in Power Query. With just a single line of M code, we can generate random numbers to use in our data transformations and analysis. By understanding the syntax and functionality of this function, we can use it to its fullest potential in our work.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us


    Your Name (required)


    Email (required)


    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)