Power Query Each: How to Transform Lists Step by Step
In this article
Power Query's each keyword is a shorthand for writing a function that operates on every item in a list, table, or record. It lets you apply a transformation to each row or element without manually defining a named function. You use it most often inside functions like Table.AddColumn, List.Transform, or Table.TransformColumns to create custom calculations. To use it, you need Power Query available in Excel or Power BI, and a basic understanding of the formula bar. The steps below walk you through writing and testing an each expression safely.
What You'll Need
- Excel (Microsoft 365 or 2016+) with Power Query, or Power BI Desktop.
- A data source loaded into Power Query (a table, list, or record).
- Access to the formula bar (enable it from the View tab if hidden).
- Basic familiarity with Power Query's M language, especially lists and records.
Step-by-Step
- Load your data into Power Query. In Excel, select your table and go to Data > From Table/Range. In Power BI, use Get Data. The Power Query Editor opens with your data as a query.
- Decide where you need an
eachexpression. Common places: adding a custom column to a table, transforming every item in a list, or updating every field in a record. For a table, you'll typically useTable.AddColumn. - Open the Advanced Editor or the formula bar. For a custom column, go to Add Column > Custom Column. The formula bar shows the M code. You can also open the Advanced Editor from the Home tab to see the full query.
- Write the
eachexpression. The syntax iseach [ColumnName]oreach _for list items. For example, to double a column called Sales, typeeach [Sales] * 2. The underscore_represents the current item when working with lists. - Understand the scope. Inside
each, field names like[Sales]refer to the current row's record. For a list, use_to refer to the current element. You cannot mix table and list contexts in oneeachwithout converting. - Test with a small sample. Before applying to the whole dataset, filter your query to a few rows or use the first step preview. Check that the expression returns the expected type (number, text, etc.).
- Apply the step. Click OK in the Custom Column dialog or confirm the formula. Power Query adds a new step. Review the column for errors (shown as
Errorin cells). - Handle errors if needed. Wrap the expression in
try ... otherwise .... For example:each try [Sales] * 2 otherwise null. This prevents a single bad row from breaking the whole column. - Use
eachwith list functions. For a list, useList.Transform(MyList, each _ * 2). Theeachhere is equivalent to(x) => x * 2. You can also useeach Text.Upper(_)to uppercase every string in a list. - Use
eachwith records. To update every field in a record, useRecord.TransformFields(MyRecord, each _ * 2)if all fields are numeric. Be careful: mixed types will cause errors. - Check the applied steps. Each transformation appears in the Applied Steps pane. You can click any step to review the result. If something looks wrong, delete the step and try again.
- Close and load. Once satisfied, go to Home > Close & Load (Excel) or Close & Apply (Power BI) to output the transformed data.
Common Mistakes
- Using
eachwithout a function context.eachis only valid inside a function that expects a function argument, such asTable.AddColumnorList.Transform. Writingeach [Sales]as a standalone step will error. - Confusing
_and[Column]. In a table row,_refers to the whole record, so_[Sales]works, but_ * 2fails because a record can't be multiplied. In a list,[Column]is invalid because list items have no field names. - Forgetting type conversion. If a column contains text that looks like numbers,
each [Sales] * 2will error. UseNumber.From([Sales])first. - Not handling nulls. Null values propagate. Use
try ... otherwiseorif [Sales] = null then null else [Sales] * 2. - Overcomplicating with custom functions.
eachis shorthand for(x) => .... If you need multiple statements, define a proper function instead.
Tips
- Use the formula bar to inspect the generated M code after adding a custom column; it shows exactly how
eachis used. - For simple row-level calculations,
eachis faster to write than a fullletexpression. - You can nest each inside Table.TransformColumns to apply different transformations per column: