Sub topic 3
Combining and Summarizing Power Query Data
Assume you can import Excel and CSV data, use the Power Query Editor, and recognize text, number, date, and null values. This chapter focuses on combining queries, adding logic, and making refreshes reusable.
Append, merge, and summarize
Append stacks tables vertically. Use it when tables have the same kind of columns, such as Sales2024 and Sales2025. Matching column names are combined; missing columns produce nulls.
Merge joins tables horizontally. Select a key column, such as CustomerID, in both queries. A left outer merge keeps every order and adds matching customer details.
| Operation | Result | Typical use |
|---|---|---|
| Append | More rows | Combine yearly sales tables |
| Merge | More columns | Look up customer names |
After a merge, expand the nested table column to select fields such as Name.
Use Group By to summarize rows. Grouping by Region and calculating Sum on Amount creates one total per region. Other operations include Count Rows, Average, Minimum, and Maximum.
M code and custom columns
Power Query records each action as M code. In the formula bar or Advanced Editor, edit a step by changing its expression. Each step usually refers to the previous step.
let
Source = Excel.CurrentWorkbook(){[Name="Sales"]}[Content],
Typed = Table.TransformColumnTypes(Source, {{"Amount", type number}}),
WithClass = Table.AddColumn(
Typed,
"Size",
each if [Amount] >= 1000 then "Large" else "Small",
type text
),
SafeAmount = Table.AddColumn(
WithClass,
"CheckedAmount",
each try Number.From([Amount]) otherwise null,
type nullable number
)
in
SafeAmountA custom column uses an expression for each row. A conditional column provides a graphical form for common if...then...else rules. The try...otherwise expression converts invalid values into null instead of stopping the query. Inspect the error details when you need to correct the source rather than hide the problem.
Folder imports and performance
Use From Folder to combine files with the same structure. Filter out temporary files, then apply the transformation function to each remaining file. Keep headers and column types consistent across files.
Query folding occurs when Power Query translates steps into operations performed by the source system, such as a database filter. Folding can reduce data transferred and improve refresh time. Apply filters and column selection early, and remember that some steps, such as custom functions, may stop folding.
Reusable parameters
A parameter stores a value such as a folder path, reporting date, or server name. Replace a hard-coded value with the parameter in the Source step. You can then change one value without editing the query, and reuse the same query across workbooks or environments.
Exercise
Which operation should combine monthly tables with the same columns, and which should add customer details by CustomerID?
Show answer
Append combines the monthly tables. Merge adds the customer details.