top of page

You are learning Power Query in MS Excel

How to use conditional logic to create new columns in Power Query?

There are two main ways to use conditional logic to create new columns in Power Query:

1. Using the Conditional Column Feature:

This is the most user-friendly method for simple conditions. Here's how to do it:

* Select the "Add Column" tab and choose "Conditional Column."
* Give your new column a descriptive name.
* Select the existing column you want to base the condition on.
* Choose the comparison operator (e.g., equal to, greater than).
* Enter the value to compare with.
* In the "Output" field, enter the value to assign if the condition is true.
* You can add multiple conditions ("Else If") or a final output for all unmatched values ("Else").
* Click "OK" to create the new column.

2. Using a Custom Column with Conditional Logic:

This method offers more flexibility for complex logic. Here's the basic structure:

* Select the "Add Column" tab and choose "Custom Column."
* Give your new column a descriptive name.
* In the formula bar, enter an expression using conditional logic.
* Power Query uses the M language, which has built-in functions for comparisons ('>', '<', '=') and logic operators (IF, THEN, ELSE).

Here's an example formula to create a new column named "Price Category" based on price values:

`Price Category =
IF([Price] > 100, "Expensive",
IF([Price] > 50, "Moderate", "Cheap"))`

This formula checks the "Price" column. If the price is greater than 100, it assigns "Expensive" to the new column. Otherwise, it checks if the price is greater than 50 and assigns "Moderate" if true. Any price below 50 gets categorized as "Cheap."

Tips:

* Use clear and concise names for your conditions and new columns.
* Start with simple conditions and gradually build more complex logic.
* Test your formulas with different data samples to ensure they work as expected.
* Consider using nested IF statements for intricate conditions.

By using conditional logic in Power Query, you can automate data categorization, create new insights, and enrich your data for further analysis.

bottom of page