0
0
Power-biHow-ToBeginner ยท 4 min read

How to Add Custom Column in Power Query in Power BI

In Power BI, to add a custom column in Power Query, open the Power Query Editor and select Add Column > Custom Column. Then, write your formula using the M language in the dialog box and click OK to create the new column.
๐Ÿ“

Syntax

The basic syntax for adding a custom column in Power Query uses the Table.AddColumn function in M language.

  • Table: The table you want to add the column to.
  • NewColumnName: The name of the new column as text.
  • each Expression: A function that defines the value for each row in the new column.
m
Table.AddColumn(Source, "NewColumn", each [ExistingColumn] * 2)
๐Ÿ’ป

Example

This example shows how to add a custom column that doubles the values of an existing column named Sales.

m
let
    Source = Table.FromRecords({
        [Product="A", Sales=100],
        [Product="B", Sales=200],
        [Product="C", Sales=150]
    }),
    AddedCustom = Table.AddColumn(Source, "DoubleSales", each [Sales] * 2)
in
    AddedCustom
Output
Product | Sales | DoubleSales --------|-------|------------ A | 100 | 200 B | 200 | 400 C | 150 | 300
โš ๏ธ

Common Pitfalls

Common mistakes when adding custom columns include:

  • Using incorrect column names that do not exist in the table.
  • Writing formulas without the each keyword, which is required to apply the expression row by row.
  • Not handling data types properly, causing errors in calculations.

Always check column names and use each before your expression.

m
/* Wrong: Missing 'each' keyword */
Table.AddColumn(Source, "NewCol", [Sales] * 2)

/* Correct: With 'each' keyword */
Table.AddColumn(Source, "NewCol", each [Sales] * 2)
๐Ÿ“Š

Quick Reference

StepAction
Open Power Query EditorClick 'Transform Data' in Power BI Desktop
Select Add Column tabChoose 'Custom Column' option
Enter column nameType the name for your new column
Write formulaUse M language with 'each' for row-wise calculation
Click OKApply and see the new column in your table
โœ…

Key Takeaways

Use the 'Add Column' > 'Custom Column' option in Power Query Editor to create new columns.
Always start your formula with 'each' to apply it to every row.
Check column names carefully to avoid errors.
Use M language expressions for flexible custom calculations.
Preview results immediately to verify your custom column.