You are learning Power Query in MS Excel
How to filter and transform data based on conditional statements in Power Query?
Filtering and transforming data based on conditional statements in Power Query allows you to selectively process and manipulate data according to specific criteria. Here’s how you can filter and transform data using conditional statements step-by-step in Power Query:
Filtering Data Based on Conditional Statements
1. Open Power Query Editor:
- Load your dataset into Excel.
- Go to the `Data` tab and click on `Get & Transform Data` > `From Table/Range` to open the Power Query Editor.
2. Filter Rows Based on Condition:
- Select the column you want to filter by.
- Go to `Home` > `Keep Rows` > `Keep Rows...` > `By Condition`.
![Filter Rows](https://i.imgur.com/Hr2cRf9.png)
3. Set Condition:
- In the "Keep Rows" dialog box, choose a condition (e.g., equals, greater than, contains) and specify the value or formula.
![Set Condition](https://i.imgur.com/UgWKtbF.png)
4. Apply Filter:
- Click `OK` to apply the filter based on the condition.
Transforming Data Based on Conditional Statements
1. Open Power Query Editor:
- Load your dataset into Excel.
- Go to the `Data` tab and click on `Get & Transform Data` > `From Table/Range` to open the Power Query Editor.
2. Add Conditional Column:
- Go to `Add Column` > `Conditional Column`.
![Conditional Column](https://i.imgur.com/cI9WeO3.png)
3. Configure Conditional Column:
- In the "Add Conditional Column" dialog box, specify:
- New Column Name: Enter a name for the new column.
- If: Define the condition using logical expressions or functions.
- Then: Specify the value or formula to apply when the condition is true.
- Else: Optionally, specify a default value or formula for when the condition is false.
![Configure Conditional Column](https://i.imgur.com/O6h2ncE.png)
4. Apply Transformation:
- Click `OK` to create the conditional column based on your specified conditions.
Example: Filtering and Transforming Data Based on Conditions
Suppose you have a dataset `SalesData` with columns `Product`, `Category`, `Quantity`, and `Revenue`. To filter rows where `Quantity` is greater than 100 and create a new column `RevenueStatus` based on conditional revenue thresholds:
Filtering Example
1. Open Power Query Editor:
- Load `SalesData` into Power Query.
2. Filter Rows:
- Select the `Quantity` column.
- Go to `Home` > `Keep Rows` > `Keep Rows...` > `By Condition`.
- Choose `Greater Than` and enter `100` as the value.
3. Apply Filter:
- Click `OK` to filter rows where `Quantity` is greater than 100.
Transforming Example
1. Open Power Query Editor:
- Load `SalesData` into Power Query.
2. Add Conditional Column:
- Go to `Add Column` > `Conditional Column`.
3. Configure Conditional Column:
- Enter `RevenueStatus` as the new column name.
- Define the condition: If `Revenue` is greater than 1000, then `"High"`, else `"Low"`.
4. Apply Transformation:
- Click `OK` to add the `RevenueStatus` column based on the conditional revenue thresholds.
Advanced Tips
- Complex Conditions: Use logical operators (`and`, `or`) and functions (e.g., `Text.Contains`, `Date.IsInCurrentDay`) in conditional statements for more complex filtering and transformations.
- Custom Functions: Utilize custom M language functions in the `Advanced Editor` for intricate data processing needs.
By following these steps, you can effectively filter and transform data based on conditional statements in Power Query, enabling you to refine and prepare your dataset for analysis or reporting tasks in Excel.