ホーム 暮らし Power Query M: Conditional Branching with if Statements and Adding Columns (Part 1)

Power Query M: Conditional Branching with if Statements and Adding Columns (Part 1)

八木 麻衣 八木 麻衣 • 更新: • 公開: • 読了約 6 分
目次
nfba995760e0d

When using Power Query to analyze data, one of the features that significantly enhances Power Query's capabilities is implementing if statements to perform conditional branching when adding columns to a table.

So, in what cases would you implement conditional logic in Power Query?
A common case is when you want to create a new column in a dataset based on specific logic, which is particularly useful for categorizing data or calculating new values. Conditional columns evaluate one or more conditions, return true or false values, and create a new data column. Adding a column is clearer and easier than transforming the values of an existing column without adding a new one.

For example, suppose you have a dataset of customer information and you want to categorize it based on conditions. If a customer's [TotalChildren] > 0, label them as "Yes"; otherwise, label them as "No". [TotalChildren] is the number of children.
In this way, you can categorize data and conduct effective marketing campaigns for each customer group.

How to use custom columns with if statements

1. In the Power Query Editor, go to "Add Column" from the top menu.
2. Click the "Custom Column" option from the menu at the top of the editor.
3. The "Custom Column" dialog box will appear, where you can create a formula for the custom column using an if statement.
In a Power Query custom column formula, you can create the following formula:
if [TotalChildren] > 0 then "Yes" else "No"
4. Result: When you press the "OK" button in step 3, a column named "Custom" will be created.

In this article, we learned about using Power Query's if statements and column addition features.
If statements are very flexible, and you can embed more complex logic into the statements. Next time, I will introduce advanced cases that can handle more complex conditions.

Reference:
https://learn.microsoft.com/en-us/powerquery-m/m-spec-conditionals
https://learn.microsoft.com/en-us/power-bi/create-reports/desktop-add-custom-column

Author

Phong
Hello! My name is Phong.
I am an engineer from Vietnam in charge of developing Power BI-related products at Media Fusion Co., Ltd.
I would like to share my accumulated experience with those who are interested in or use Power BI.

同じカテゴリの記事

八木 麻衣
トレンドリサーチャー 八木 麻衣
プロフィールを見る →

全記事の事実確認を担当。一次情報・公式発表・公開資料の三点を確認できない場合は公開を見送る判断をします。

おすすめの関連記事