Conditional Columns and Custom Columns in Power BI
Power BI provides powerful tools for transforming and manipulating data using Conditional Columns and Custom Columns in Power Query Editor. These features allow users to create new columns based on conditions or custom logic, making data analysis more efficient and insightful.
1. Understanding Conditional Columns in Power BI
A Conditional Column in Power BI is a column created based on specified conditions. It works similarly to an IF
statement in Excel, where values in the new column depend on conditions applied to existing columns.
1.1. Why Use Conditional Columns?
- To classify data into categories (e.g., Low, Medium, High).
- To create calculated flags (e.g., “Pass” or “Fail” based on scores).
- To clean and standardize data (e.g., replacing nulls with a default value).
- To perform conditional calculations based on different criteria.
1.2. Steps to Create a Conditional Column in Power BI
Step 1: Open Power Query Editor
- Open Power BI Desktop.
- Click on Transform Data to open Power Query Editor.
Step 2: Add a Conditional Column
- Click on the Add Column tab in the ribbon.
- Select Conditional Column.
Step 3: Define Conditions
- In the Add Conditional Column window:
- Enter a New Column Name (e.g., “Category”).
- Select a column to apply the condition (e.g., “Sales Amount”).
- Choose a comparison operator (e.g., Greater than, Equals, Contains).
- Enter a value to compare against (e.g., 5000).
- Specify the output value if the condition is met (e.g., “High Sales”).
- Click Add Clause to create multiple conditions.
- Set a default output for values that do not meet any conditions.
Step 4: Apply and Save
- Click OK to create the column.
- Click Close & Apply to save the changes and return to Power BI Desktop.
Example:
If a business wants to classify sales performance into three categories:
- High Sales for orders above $5000.
- Medium Sales for orders between $2000 and $5000.
- Low Sales for orders below $2000.
Sales Amount | Category |
---|---|
6000 | High Sales |
4500 | Medium Sales |
1500 | Low Sales |
This classification makes it easier to analyze sales trends.
2. Understanding Custom Columns in Power BI
A Custom Column is a column where users define calculations using M language (Power Query formula language). It allows advanced transformations and computations beyond simple conditions.
2.1. Why Use Custom Columns?
- To perform mathematical operations (e.g., Total Sales = Quantity × Unit Price).
- To create concatenated text fields (e.g., First Name & Last Name).
- To format data (e.g., converting numbers to percentages).
- To extract specific parts of a string (e.g., extracting the year from a date).
2.2. Steps to Create a Custom Column in Power BI
Step 1: Open Power Query Editor
- In Power BI Desktop, go to Home > Transform Data to open Power Query Editor.
Step 2: Add a Custom Column
- Click on the Add Column tab.
- Select Custom Column.
Step 3: Enter the Formula
- In the Custom Column window:
- Enter a New Column Name (e.g., “Total Revenue”).
- Enter the formula in the editor using M language.
Step 4: Apply the Formula
- Click OK to create the column.
- Click Close & Apply to save changes.
Example 1: Creating a Total Revenue Column
If a business has a dataset with Quantity and Unit Price, they can create a new column for Total Revenue using the formula:
[Quantity] * [Unit Price]
Quantity | Unit Price | Total Revenue |
---|---|---|
5 | 100 | 500 |
10 | 50 | 500 |
3 | 200 | 600 |
Example 2: Concatenating First Name and Last Name
If there are two columns, First Name and Last Name, we can create a new column for Full Name using:
[First Name] & " " & [Last Name]
First Name | Last Name | Full Name |
---|---|---|
John | Doe | John Doe |
Alice | Smith | Alice Smith |
Example 3: Extracting Year from a Date Column
If a dataset has a column Order Date, we can create a new column for Year using:
Date.Year([Order Date])
Order Date | Year |
---|---|
2023-05-10 | 2023 |
2022-07-15 | 2022 |
3. Difference Between Conditional Column and Custom Column
Feature | Conditional Column | Custom Column |
---|---|---|
Based on Conditions | Yes | No |
Uses M Language | No | Yes |
Supports Multiple Conditions | Yes | No |
Supports Advanced Calculations | No | Yes |
Example | “Pass” if score > 50 | Total Revenue = Quantity × Price |
4. Best Practices for Using Conditional and Custom Columns
✅ Use Conditional Columns When:
- Creating simple IF-THEN classifications.
- Assigning categories based on existing values.
- Creating flags (e.g., Active/Inactive).
✅ Use Custom Columns When:
- Performing advanced calculations.
- Concatenating multiple fields.
- Extracting parts of a string or date.
✅ General Best Practices:
- Keep column names descriptive (e.g., “Total Sales” instead of “Column1”).
- Optimize performance by avoiding unnecessary transformations.
- Use the Advanced Editor for complex transformations.
- Test transformations with sample data before applying them to large datasets.
5. Conclusion
Conditional Columns and Custom Columns are essential tools in Power BI’s Power Query Editor. Conditional Columns are great for simple classifications, while Custom Columns allow for more advanced calculations. Understanding when to use each helps in effective data modeling and transformation, leading to better insights in Power BI reports.