RAND

RAND

How to Use the Power BI DAX function RAND

Syntax of the RAND Function

The syntax of the RAND function is quite simple. It does not require any arguments or parameters. The syntax is as follows:


RAND()


The function simply returns a random number between 0 and 1.

Generating Random Numbers

The RAND function can be used to generate random numbers for a variety of purposes. For example, you may need to generate a random number for a simulation or to select a random sample from a large dataset. The following example shows how to use the RAND function to generate a random number in Power BI:


Random Number = RAND()


This DAX formula will create a new column called "Random Number" in the dataset that will contain a random number between 0 and 1 for each row.

Using RAND to Shuffle Data

Another interesting application of the RAND function is to shuffle the data in a table randomly. This can be useful when you want to randomize the order of records in a table. The following DAX formula shows how to shuffle the data in a table using the RAND function:


Shuffled Table =

FILTER(

ADDCOLUMNS(

OriginalTable,

“Random”, RAND()

),

[Random] ❰❱ BLANK()

)


This formula creates a new table called "Shuffled Table" that contains all the columns from the original table along with a new column called "Random" that contains a random number between 0 and 1 for each row. The FILTER function is used to remove any rows that have a blank value in the "Random" column. Finally, the table is sorted by the "Random" column to shuffle the data randomly.

Using RAND to Simulate Data

The RAND function can also be used to simulate data in Power BI. For example, you may want to simulate the results of a dice roll or a coin flip. The following DAX formula shows how to use the RAND function to simulate a dice roll:


Dice Roll = ROUNDUP(RAND() * 6, 0)


This formula generates a random number between 0 and 1 using the RAND function and multiplies it by 6 to get a number between 0 and 6. The ROUNDUP function is used to round the number up to the nearest integer, which simulates the result of a dice roll.

Similarly, the following DAX formula shows how to simulate a coin flip:


Coin Flip = IF(RAND() ❱ 0.5, “Heads”, “Tails”)


This formula generates a random number between 0 and 1 using the RAND function. If the number is greater than 0.5, the result is "Heads", otherwise, it is "Tails". This simulates the result of a coin flip.

The RAND function is a powerful tool that can be used for a variety of purposes in Power BI. It is a simple function that generates a random number between 0 and 1. We have explored some of the ways in which the RAND function can be used to generate random numbers, shuffle data, and simulate data. By using the techniques described in this article, you can add a new dimension to your data analysis in Power BI.

Power BI DAX 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)

Copyright, G Com Solutions Ltd, 2024.
Tower Bridge Business Centre, 46-48 East Smithfield, London E1W 1AW
0800 998 9248   |   9:00 a.m. till 5:30 p.m.

Connect With Me: