
You're working with a sales dataset that contains raw transaction data, but you need to categorize customers based on their purchase amounts, calculate commissions based on different product types, and create meaningful business labels from cryptic codes. This is exactly the kind of data transformation challenge that Power Query's custom columns and conditional logic are designed to solve.
Most data professionals spend significant time manually categorizing data in Excel or writing complex formulas that break when data changes. Power Query's custom column functionality lets you automate these transformations with reusable, maintainable logic that adapts as your data grows.
What you'll learn:
You should be comfortable with basic Power Query operations like importing data, filtering rows, and navigating the Power Query Editor interface. If you're new to Power Query, complete the "Getting Started with Power Query" lesson first.
Custom columns are new columns you add to your dataset that contain calculated values, transformed data, or business logic. Unlike simply renaming or reordering existing columns, custom columns create entirely new information based on the data you already have.
Think of custom columns as your data transformation workshop. You can take raw materials (existing columns) and craft them into exactly what your analysis needs. Whether you're categorizing customers, calculating derived metrics, or applying business rules, custom columns give you the flexibility to shape your data precisely.
Power Query offers two primary approaches for creating custom columns:
Let's start with a practical example to see both approaches in action.
Before diving into custom columns, let's create a realistic dataset to work with. Imagine you're analyzing sales data for a retail company with the following information:
Customer_ID | Product_Category | Sale_Amount | Region | Sales_Rep
1001 | Electronics | 2500 | North | Johnson
1002 | Clothing | 450 | South | Smith
1003 | Electronics | 8900 | East | Davis
1004 | Home_Goods | 1200 | West | Johnson
1005 | Clothing | 250 | North | Brown
You can create this dataset in Excel and import it into Power Query, or connect to any similar sales data you have available. The key is having multiple columns with different data types (text, numbers) so you can practice various conditional scenarios.
To import this data into Power Query:
Let's start with the GUI approach by creating a customer tier based on purchase amounts. We want to categorize customers as "Premium" (over $5000), "Standard" ($1000-$5000), or "Basic" (under $1000).
In the Power Query Editor:
Power Query evaluates these conditions in order from top to bottom. When a condition is true, it assigns that output and stops checking the remaining conditions. This is why we put the highest threshold first—if a sale amount is $6000, it matches "greater than or equal to 5000" and gets labeled "Premium" without checking the $1000 threshold.
Important: The order of conditions matters! Power Query uses the first matching condition, so always arrange them from most specific to least specific.
You'll see your new Customer_Tier column appear in the data preview. Each row now has an appropriate tier label based on the sale amount. This single transformation just replaced what might have been multiple manual categorization steps.
When you create a conditional column through the GUI, Power Query generates M code behind the scenes. You can see this code by looking at the Applied Steps panel on the right side of the Power Query Editor. Click on the "Added Conditional Column" step to see the generated formula.
The M code for our customer tier example looks like this:
= Table.AddColumn(#"Previous Step", "Customer_Tier", each if [Sale_Amount] >= 5000 then "Premium" else if [Sale_Amount] >= 1000 then "Standard" else "Basic")
This formula introduces several key M concepts:
Table.AddColumn: The function that creates new columnseach: A keyword that applies the logic to each rowif...then...else: The conditional logic structure[Column_Name]: How to reference existing columnsUnderstanding this generated code helps you transition to writing custom formulas when the GUI limitations become restrictive.
While the Conditional Column GUI works well for simple scenarios, many real-world transformations require more sophisticated logic. The Custom Column feature gives you direct access to Power Query's M formula language for unlimited flexibility.
Let's create a commission calculation that considers both the product category and sale amount:
Click Add Column tab → Custom Column and name it "Commission_Rate". Here's the M formula:
if [Product_Category] = "Electronics" then
if [Sale_Amount] > 5000 then 0.08 else 0.05
else if [Product_Category] = "Clothing" then 0.06
else if [Product_Category] = "Home_Goods" then
if [Sale_Amount] > 2000 then 0.07 else 0.04
else 0.03
This nested conditional logic demonstrates several important patterns:
else 0.03 provides a fallback commission rateTip: Use proper indentation in your M formulas. While Power Query doesn't require it, indented code is much easier to read and debug, especially with nested conditions.
Now let's calculate the actual commission amount by creating another custom column called "Commission_Amount":
[Sale_Amount] * [Commission_Rate]
This simple multiplication shows how custom columns can reference other custom columns you've created. Power Query maintains the dependency chain automatically.
Real business logic often requires checking multiple conditions simultaneously. Power Query provides logical operators to combine conditions:
and: Both conditions must be trueor: Either condition can be truenot: Reverses the condition (true becomes false, false becomes true)Let's create a "Priority_Follow_Up" flag for customers who meet specific criteria. We want to flag customers who either:
Create a new custom column called "Priority_Follow_Up":
if ([Sale_Amount] > 3000 and ([Region] = "North" or [Region] = "East")) or
([Product_Category] = "Electronics" and [Sale_Amount] > 1500)
then "Yes"
else "No"
This formula demonstrates:
The logical operators follow standard precedence rules, but using parentheses makes your intentions explicit and prevents confusion.
Real-world data often contains missing values (nulls) or unexpected data types that can break conditional logic. Power Query provides several functions to handle these scenarios gracefully.
The most common null-handling functions are:
if [Column] = null: Checks if a value is null[Column] <> null: Checks if a value is not nullif [Column] = null then DefaultValue else [Column]: Provides a default for null valuesLet's create a "Sale_Category" column that handles potential null values in the Sale_Amount field:
if [Sale_Amount] = null then "Unknown"
else if [Sale_Amount] >= 5000 then "Large Sale"
else if [Sale_Amount] >= 1000 then "Medium Sale"
else "Small Sale"
For more robust error handling, you can use the try...otherwise construct:
try (
if [Sale_Amount] >= 5000 then "Large Sale"
else if [Sale_Amount] >= 1000 then "Medium Sale"
else "Small Sale"
) otherwise "Data Error"
This approach catches any errors that occur during evaluation (such as comparing text to numbers) and provides a fallback value.
Best Practice: Always consider what should happen when your data contains unexpected values. Robust conditional logic handles null values and data type mismatches gracefully.
As your conditional logic becomes more sophisticated, several patterns emerge that solve common business scenarios:
When you need to categorize continuous values into ranges, create a systematic approach:
if [Sale_Amount] >= 10000 then "Enterprise"
else if [Sale_Amount] >= 5000 then "Corporate"
else if [Sale_Amount] >= 1000 then "Professional"
else if [Sale_Amount] >= 100 then "Small Business"
else "Individual"
For scenarios requiring multiple factors in decision-making:
let
CategoryScore = if [Product_Category] = "Electronics" then 3
else if [Product_Category] = "Clothing" then 2
else 1,
AmountScore = if [Sale_Amount] > 5000 then 3
else if [Sale_Amount] > 1000 then 2
else 1,
RegionScore = if [Region] = "North" then 2 else 1,
TotalScore = CategoryScore + AmountScore + RegionScore
in
if TotalScore >= 7 then "High Priority"
else if TotalScore >= 5 then "Medium Priority"
else "Standard Priority"
This pattern uses the let...in construct to break complex logic into manageable pieces, making it easier to understand and maintain.
When working with text data that follows patterns:
if Text.Contains([Sales_Rep], "Johnson") then "Team Alpha"
else if Text.StartsWith([Sales_Rep], "S") then "Team Beta"
else if Text.Length([Sales_Rep]) > 6 then "Team Gamma"
else "Team Delta"
Now it's time to apply what you've learned with a comprehensive exercise. You'll create multiple custom columns that work together to analyze the sales data.
Starting with your practice dataset, create these custom columns in order:
1. Performance_Tier (using Conditional Column GUI)
2. Region_Multiplier (using Custom Column formula) Create a multiplier based on regional market conditions:
if [Region] = "North" then 1.1
else if [Region] = "East" then 1.05
else if [Region] = "South" then 0.95
else 1.0
3. Adjusted_Sale_Value (using Custom Column formula) Multiply the sale amount by the region multiplier:
[Sale_Amount] * [Region_Multiplier]
4. Strategic_Account (using Custom Column with complex logic) Flag accounts that meet strategic criteria:
if ([Performance_Tier] = "Top Performer" and [Product_Category] = "Electronics") or
([Adjusted_Sale_Value] > 3000 and ([Region] = "North" or [Region] = "East"))
then "Yes"
else "No"
5. Follow_Up_Priority (using Custom Column with null handling) Create follow-up priorities with error handling:
try (
if [Strategic_Account] = "Yes" then "Immediate"
else if [Performance_Tier] = "Good Performer" then "This Week"
else "Standard Schedule"
) otherwise "Review Required"
After creating these columns, examine how they work together. Notice how later columns reference earlier ones, creating a data transformation pipeline that turns raw sales data into actionable business intelligence.
Even experienced data professionals encounter challenges when working with conditional logic in Power Query. Here are the most common issues and their solutions:
Problem: Conditions evaluated in wrong order, causing unexpected results.
Example:
if [Sale_Amount] >= 1000 then "Standard"
else if [Sale_Amount] >= 5000 then "Premium"
else "Basic"
Solution: Always arrange conditions from most specific to most general:
if [Sale_Amount] >= 5000 then "Premium"
else if [Sale_Amount] >= 1000 then "Standard"
else "Basic"
Problem: Comparing text values to numbers or forgetting that imported data might be stored as text.
Symptoms: Conditions that should work return unexpected results or errors.
Solution: Use Number.From() to convert text to numbers when needed:
if Number.From([Sale_Amount]) > 1000 then "Large" else "Small"
Problem: Conditional logic breaks when encountering null values.
Solution: Always check for nulls first:
if [Sale_Amount] = null then "No Data"
else if [Sale_Amount] > 1000 then "Large"
else "Small"
Problem: Trying to do everything in one massive conditional statement.
Solution: Break complex logic into multiple custom columns or use the let...in construct:
let
BaseCategory = if [Sale_Amount] > 5000 then "High" else "Standard",
RegionAdjustment = if [Region] = "North" then " - Priority" else "",
FinalCategory = BaseCategory & RegionAdjustment
in
FinalCategory
When your conditional logic isn't working as expected:
Pro Tip: When debugging complex conditional logic, temporarily replace your final output with the raw values you're testing. For example, instead of outputting "Premium", output the actual
[Sale_Amount]value to verify your conditions are capturing the right data.
Custom columns and conditional logic transform Power Query from a simple data import tool into a powerful data transformation engine. You've learned to create business rules that automatically categorize data, calculate derived values, and implement complex decision trees that adapt as your data changes.
Key takeaways from this lesson:
The patterns you've learned here—from simple if-then logic to complex multi-criteria scoring—form the foundation for sophisticated data transformations. You can now take raw business data and automatically apply the categorization and business rules that previously required manual effort.
Your next steps should focus on applying these techniques to your own datasets. Start with simple conditional columns to build confidence, then gradually incorporate more complex patterns as your needs grow. Consider exploring Power Query's other transformation functions like grouping, pivoting, and merging, which often work hand-in-hand with the conditional logic you've mastered.
The investment you've made in understanding conditional logic will pay dividends across all your future data projects. Every business has categorization rules, derived calculations, and decision trees—now you have the skills to automate them all.
Learning Path: Power Query Essentials