Wicked Smart Data
LearnArticlesAbout
Sign InSign Up
LearnArticlesAboutContact
Sign InSign Up
Wicked Smart Data

The go-to platform for professionals who want to master data, automation, and AI — from Excel fundamentals to cutting-edge machine learning.

Platform

  • Learning Paths
  • Articles
  • About
  • Contact

Connect

  • Contact Us
  • RSS Feed

© 2026 Wicked Smart Data. All rights reserved.

Privacy PolicyTerms of Service
All Articles
Master Custom Columns and Conditional Logic in Power Query

Master Custom Columns and Conditional Logic in Power Query

Power Query🌱 Foundation13 min readApr 1, 2026Updated Apr 1, 2026
Table of Contents
  • Prerequisites
  • Understanding Custom Columns in Power Query
  • Setting Up Your Practice Dataset
  • Creating Your First Conditional Column
  • Understanding the M Formula Behind Conditional Columns
  • Writing Custom Column Formulas
  • Working with Logical Operators
  • Handling Null Values and Errors
  • Advanced Conditional Patterns
  • Range-Based Categories
  • Multi-Criteria Scoring
  • Text Pattern Matching
  • Hands-On Exercise

Custom Columns and Conditional Logic in Power Query

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:

  • How to create custom columns using Power Query's formula language (M)
  • Build conditional logic using if-then-else statements and nested conditions
  • Apply multiple conditions using logical operators (and, or, not)
  • Use conditional columns through the GUI for simple scenarios
  • Handle null values and errors in conditional logic
  • Create complex business rules that combine multiple data points

Prerequisites

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.

Understanding Custom Columns in Power Query

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:

  1. Conditional Column (GUI): A point-and-click interface for simple if-then logic
  2. Custom Column (Formula): Full access to Power Query's M formula language for complex scenarios

Let's start with a practical example to see both approaches in action.

Setting Up Your Practice Dataset

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:

  1. Open Excel and create your sample data
  2. Click Data tab → Get Data → From Other Sources → From Table/Range
  3. Select your data range and click OK
  4. The Power Query Editor opens with your data loaded

Creating Your First Conditional Column

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:

  1. Click Add Column tab → Conditional Column
  2. The Conditional Column dialog opens
  3. Name your new column "Customer_Tier"
  4. Set up your first condition:
    • Column Name: Sale_Amount
    • Operator: is greater than or equal to
    • Value: 5000
    • Output: Premium
  5. Click Add Clause to add the second condition:
    • Column Name: Sale_Amount
    • Operator: is greater than or equal to
    • Value: 1000
    • Output: Standard
  6. In the "Else" field, enter: Basic
  7. Click OK

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.

Understanding the M Formula Behind Conditional Columns

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 columns
  • each: A keyword that applies the logic to each row
  • if...then...else: The conditional logic structure
  • [Column_Name]: How to reference existing columns

Understanding this generated code helps you transition to writing custom formulas when the GUI limitations become restrictive.

Writing Custom Column Formulas

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:

  • Electronics: 8% commission for sales over $5000, 5% for smaller sales
  • Clothing: 6% commission for all sales
  • Home_Goods: 7% commission for sales over $2000, 4% for smaller sales

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:

  1. Nested if statements: You can embed if-then-else logic inside other conditions
  2. Multiple criteria evaluation: Combining product category and sale amount checks
  3. Default handling: The final else 0.03 provides a fallback commission rate

Tip: 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.

Working with Logical Operators

Real business logic often requires checking multiple conditions simultaneously. Power Query provides logical operators to combine conditions:

  • and: Both conditions must be true
  • or: Either condition can be true
  • not: 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:

  1. Made a large purchase (over $3000) in the North or East regions, OR
  2. Are Electronics customers with any purchase amount over $1500

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:

  • Parentheses for grouping: Essential for controlling the order of logical operations
  • Combining and/or operators: Creating complex business rules
  • Multiple evaluation paths: Different criteria leading to the same outcome

The logical operators follow standard precedence rules, but using parentheses makes your intentions explicit and prevents confusion.

Handling Null Values and Errors

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 null
  • if [Column] = null then DefaultValue else [Column]: Provides a default for null values

Let'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.

Advanced Conditional Patterns

As your conditional logic becomes more sophisticated, several patterns emerge that solve common business scenarios:

Range-Based Categories

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"

Multi-Criteria Scoring

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.

Text Pattern Matching

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"

Hands-On Exercise

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)

  • "Top Performer": Sale_Amount >= 5000
  • "Good Performer": Sale_Amount >= 2000
  • "Standard Performer": Sale_Amount < 2000

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.

Common Mistakes & Troubleshooting

Even experienced data professionals encounter challenges when working with conditional logic in Power Query. Here are the most common issues and their solutions:

Mistake: Incorrect Condition Order

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"

Mistake: Data Type Mismatches

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"

Mistake: Not Handling Null Values

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"

Mistake: Overly Complex Single Formulas

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

Troubleshooting Techniques

When your conditional logic isn't working as expected:

  1. Test with simple data: Create a small test dataset with known values to verify your logic
  2. Add intermediate columns: Break complex conditions into steps to see where the logic fails
  3. Check data types: Use the data type indicators in column headers to verify your assumptions
  4. Use the formula bar: The formula bar shows the M code for each step—review it for syntax errors
  5. Preview intermediate results: Click on different Applied Steps to see how your data transforms at each stage

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.

Summary & Next Steps

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:

  • Conditional Columns provide GUI-based logic for simple scenarios
  • Custom Columns offer unlimited flexibility through M formulas
  • Nested conditions handle complex multi-criteria scenarios
  • Logical operators (and, or, not) combine multiple conditions
  • Error handling ensures your transformations work with imperfect data
  • Systematic approaches make complex logic maintainable and understandable

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

Previous

Cleaning Messy Data with Power Query: Nulls, Errors, and Type Conversions

Next

Working with Dates and Text in Power Query: Advanced Transformation Techniques

Related Articles

Power Query🌱 Foundation

Advanced M: Iterators, Accumulators, and Recursive Patterns

13 min
Power Query🔥 Expert

Building a Reusable Function Library in Power Query

30 min
Power Query⚡ Practitioner

M Language Performance Patterns and Anti-Patterns: Optimize Power Query for Speed

15 min

On this page

  • Prerequisites
  • Understanding Custom Columns in Power Query
  • Setting Up Your Practice Dataset
  • Creating Your First Conditional Column
  • Understanding the M Formula Behind Conditional Columns
  • Writing Custom Column Formulas
  • Working with Logical Operators
  • Handling Null Values and Errors
  • Advanced Conditional Patterns
  • Range-Based Categories
  • Common Mistakes & Troubleshooting
  • Mistake: Incorrect Condition Order
  • Mistake: Data Type Mismatches
  • Mistake: Not Handling Null Values
  • Mistake: Overly Complex Single Formulas
  • Troubleshooting Techniques
  • Summary & Next Steps
  • Multi-Criteria Scoring
  • Text Pattern Matching
  • Hands-On Exercise
  • Common Mistakes & Troubleshooting
  • Mistake: Incorrect Condition Order
  • Mistake: Data Type Mismatches
  • Mistake: Not Handling Null Values
  • Mistake: Overly Complex Single Formulas
  • Troubleshooting Techniques
  • Summary & Next Steps