Number.RandomBetween

D

T

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

What is M Code?

M code is the language used in Power Query to create custom functions and queries. It is a functional language that is used to manipulate data. M code is similar to Excel formulas, but it is more powerful and can handle larger data sets.

How to Use the Number.RandomBetween Function

The Number.RandomBetween function is used to generate a random number between two specified numbers. The syntax for the function is as follows:


Number.RandomBetween(lower as number, upper as number) as number


The function takes two arguments, the lower bound and the upper bound, and returns a random number between those two numbers. For example, if you want to generate a random number between 1 and 10, you would use the following formula:


Number.RandomBetween(1, 10)


The M Code Behind Number.RandomBetween

The M code behind the Number.RandomBetween function is quite simple. The function is defined as follows:


let

Number.RandomBetween = (lower as number, upper as number) =>

lower + Number.Random() (upper – lower),

result = Number.RandomBetween

in

result


Let's break down the code line by line.

Line 1: Defining the Function

The first line of the code defines the function and gives it a name. The name of the function is Number.RandomBetween. The function takes two arguments, lower and upper, which are both numbers.

Line 2: Calculating the Random Number

The second line of the code calculates the random number between the lower and upper bounds. The formula used to calculate the random number is as follows:


lower + Number.Random() (upper – lower)


This formula takes the lower bound, adds a random number between 0 and 1 (generated using the Number.Random function), and multiplies it by the difference between the upper and lower bounds. This will give us a random number between the two bounds.

Line 3: Defining the Result

The third line of the code defines the result of the function. In this case, the result is the Number.RandomBetween function.

Line 4: Returning the Result

The final line of the code returns the result of the function.

The M code behind the Power Query M function Number.RandomBetween is quite simple. The function takes two arguments, the lower and upper bounds, and returns a random number between those two numbers. The formula used to calculate the random number is straightforward, and it is generated using the Number.Random function. If you are using Power Query and need to generate a random number between two specified numbers, then the Number.RandomBetween function is the perfect tool for the job.

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

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)