Skip to content
>GLB_
Go back

Creating a Custom Column with a Random String in Power BI Using DAX

Introduction

In Power BI, customizing your dataset by adding calculated columns can significantly enhance your data analysis capabilities. One common need is to generate random strings or categories for testing purposes, simulating scenarios, or assigning values like branch names, IDs, or categories to your data. In this post, we’ll explore how to create a custom column with a random string using DAX (Data Analysis Expressions) in Power BI.

Goal

We will create a column that generates a random string for each row of your data. The string could be a random category like branch names, product codes, or user roles, all of which are assigned dynamically using a custom formula.

Steps to Create a Custom Column with Random Strings in Power BI

Step 1: Open the Data View

Start by opening your Power BI project and switching to the Data view by clicking on the table icon on the left. This is where you will be able to manage and modify the data in your tables.

Step 2: Select the Table

Next, select the table in which you want to create the custom column. We will add a new calculated column that generates random strings based on your specific needs.

Step 3: Create a New Column

In the ribbon, click on New Column to create a new calculated column. This will open the formula bar where you can input the DAX expression to generate random strings.

Step 4: Use the DAX Expression for Random Strings

In the formula bar, enter the following DAX code to create a custom column with random strings. For this example, we’ll generate random branch names prefixed with "BRANCH_".

CustomString = 
"BRANCH_" & 
SWITCH(
    MOD(ROUND(RAND() * 1000, 0), 5),
    0, "North",
    1, "South",
    2, "East",
    3, "West",
    4, "Central"
)

Explanation:

Step 5: Customize the Strings

You can replace the branch names with any strings that suit your needs. For example, you could generate random product codes, user roles, or any other categories. Here’s how the code would look for random user roles:

CustomString = 
"ROLE_" & 
SWITCH(
    MOD(ROUND(RAND() * 1000, 0), 4),
    0, "Admin",
    1, "Editor",
    2, "Viewer",
    3, "Guest"
)

Step 6: Verify the Output

Once you’ve entered the formula, press Enter. Your custom column will now be filled with random strings for each row in your dataset.

Example of Expected Output

Here’s an example of what your dataset might look like with the random branch names:

EmployeeCustomString
AliceBRANCH_North
BobBRANCH_South
CharlieBRANCH_East
DavidBRANCH_West
EvaBRANCH_Central

Or with random roles:

UserCustomString
AliceROLE_Admin
BobROLE_Editor
CharlieROLE_Viewer
DavidROLE_Guest

Use Cases

Conclusion

Creating custom columns with random strings in Power BI is a simple but powerful technique that can enhance your data preparation process. Whether you are simulating test data, generating random categories, or assigning identifiers, the flexibility of DAX allows you to tailor the solution to your needs.


Share this post:

Previous Post
Best Practices: Using Direct SQL Queries in CodeIgniter
Next Post
How to Implement MVC in CodeIgniter to Clean Up Your Views