Power Query course · Lesson 6 of 17

Filtering, sorting and removing rows and columns

Remove header junk and blank rows, fill down group labels, filter text, numbers and dates, remove duplicates and sort, in a way that survives refresh.

Beginner 15 min read

Exports rarely start with clean headers in row 1. They come with report titles, print dates, blank lines, subtotal rows and a grand total at the bottom. In a worksheet you delete those rows by hand each time. In Power Query you describe which kind of row to remove, and the description keeps working when the file has 300 rows this month and 4,000 next month. This lesson covers removing rows and columns, filtering, de-duplicating and sorting.

A report-style export

ABCD
1Regional sales report
2Period: March 2026
3
4RegionRepProductUnits
5NorthK. OlsenRouter X214
6K. OlsenSwitch 8P6
7L. WeberRouter X29
8SouthP. RossiAccess Point21

Before: three junk lines, headers in row 4, and the Region label printed only on the first row of each group.

Removing top rows and promoting headers

  1. Choose Home › Remove Rows › Remove Top Rows, enter 3, and click OK. The header line is now the first row.
  2. Choose Home › Use First Row as Headers (also on Transform). The values become column names.
  3. Power Query adds a Changed Type step; check the icons as you learned in The Power Query Editor: steps, formula bar and data types.

Remove Top Rows is safe only if the number of junk lines never changes. If it does, a more robust method is to filter on content instead, for example to skip rows until the one whose first column is Region. That needs a little M, which you will meet in The M language: let, steps, lists, records, tables and each:

Power Query M
= Table.Skip(Source, each [Column1] <> "Region")
Table.Skip with a condition skips rows while the condition is true, so it stops at the header line wherever it is.

Fill Down: repeat group labels

The Region column has a value only on the first row of each group. Select it and choose Transform › Fill › Down. Every null is replaced by the value above it. Fill › Up does the opposite, which is useful when a label is printed at the end of a group, as in some accounting reports.

Watch out. Fill Down only fills null cells, not empty text. If the blank region cells came from a CSV as empty strings, first replace the empty value with null: Transform › Replace Values, leave Value To Find empty and type null in Replace With.
ABCD
1RegionRepProductUnits
2NorthK. OlsenRouter X214
3NorthK. OlsenSwitch 8P6
4NorthL. WeberRouter X29
5SouthP. RossiAccess Point21

After: junk removed, headers promoted, Region filled down. Each row now stands on its own and can be filtered or pivoted.

Removing blank rows, bottom rows and errors

Home › Remove Rows has six options:

OptionRemoves
Remove Top RowsA fixed number of rows at the start
Remove Bottom RowsA fixed number of rows at the end, such as a Total line
Remove Alternate RowsA repeating pattern: first row to remove, number to remove, number to keep
Remove DuplicatesRows whose values in the selected columns appeared before
Remove Blank RowsRows where every column is null or empty
Remove ErrorsRows with an error in the selected columns

The mirror image is Home › Keep Rows: Keep Top Rows, Keep Bottom Rows, Keep Range of Rows, Keep Duplicates and Keep Errors. Keep Duplicates is a great auditing tool: run it on an invoice number column to see which invoices were exported twice.

Tip. Instead of Remove Bottom Rows for a Total line, filter it out by content: untick Total in the first column's filter. If the export one day comes without a total, a fixed Remove Bottom Rows would delete a real sale.

Filtering with the header drop-down

Click the arrow in a column header. You get a sort section, a list of values with tick boxes and a type-specific filter menu:

  • Text Filters: Equals, Does Not Equal, Begins With, Does Not Begin With, Ends With, Does Not End With, Contains, Does Not Contain.
  • Number Filters: Equals, Does Not Equal, Greater Than, Greater Than Or Equal To, Less Than, Less Than Or Equal To, Between.
  • Date Filters: Equals, Before, After, Between, In the Next…, In the Previous…, Is Earliest, Is Latest, Is Not Earliest, Is Not Latest, and calendar filters such as Year, Quarter, Month, Week and Day.
  • Advanced filter dialogs (choose Advanced in the filter window) combine several conditions with And / Or, even across columns.
Watch out. The value list in the drop-down is built from the rows loaded in the preview. If you see List may be incomplete, click Load more. Worse, depending on how many boxes you tick, Power Query records either the values you removed or the values you kept. A new value that appears next month is kept in the first case and dropped in the second. Check the formula bar and, if needed, rewrite the condition with a text filter.

For example, to keep only shipped and invoiced orders from an ERP export, a deliberate condition is clearer than ticking boxes:

Power Query M
= Table.SelectRows(#"Changed Type", each [Status] = "Shipped" or [Status] = "Invoiced")
or, and and not combine conditions in M; text comparison is case-sensitive, so 'shipped' would not match.

Relative date filters

Date Filters › In the Previous… with 3 months keeps rows from the last three complete months relative to today, every time the query refreshes. Use it for rolling reports. Note that Power Query uses the clock of the computer that refreshes, and that the generated condition (Date.IsInPreviousNMonths) excludes the current, incomplete month.

Removing and choosing columns

  • Home › Remove Columns deletes the selected columns.
  • Home › Remove Columns › Remove Other Columns keeps only the selected ones.
  • Home › Choose Columns opens a tick list of all columns, handy for wide exports with 60 columns.

Which is safer? Remove Other Columns and Choose Columns list the columns to keep, so a new column added to the source later is ignored. Remove Columns lists the columns to drop, so a new source column flows through to your output. Pick the behaviour you want; for stable reports, keeping a defined list is usually better. Either way, a step fails if a column it names disappears from the source.

Removing duplicates

Select the column or columns that define a duplicate, then choose Home › Remove Rows › Remove Duplicates. Power Query keeps the first occurrence and removes the later ones. To de-duplicate whole rows, select all columns first (Ctrl+A [Cmd+A on Mac] in the preview).

ABC
1EmailNameSigned up
2ana@example.comAna Silva2026-01-12
3ANA@example.comAna Silva2026-02-03
4ben@example.comBen Ito2026-01-20
5ana@example.comAna Silva2026-03-01

Before: a newsletter list with the same person three times, once in capitals.

ABC
1EmailNameSigned up
2ana@example.comAna Silva2026-01-12
3ben@example.comBen Ito2026-01-20

After: Format › lowercase on Email, then Remove Duplicates on Email. Without the lowercase step, the capitalised row would survive.

Watch out. Power Query's Remove Duplicates is case-sensitive, unlike Excel's Data › Remove Duplicates (see Removing duplicates and cleaning data). Normalise case and trim spaces first.
Tip. Which duplicate survives depends on the row order at that moment. If you want the latest sign-up per email, sort by Signed up descending first. Because Power Query may not keep the sort order when it removes duplicates on large sources, add Table.Buffer around the sorted step or use Group By with a Max (Group By: totals, counts and the All Rows trick) when the choice matters.

Sorting

Click a header arrow and choose Sort Ascending or Sort Descending, or use the buttons on Home. To sort by several columns, sort the first column, then the second: Power Query adds them to the same step, in that order of priority. The formula looks like this:

Power Query M
= Table.Sort(#"Filled Down", {{"Region", Order.Ascending}, {"Units", Order.Descending}})
Region A to Z, then within each region the highest units first.
Note. Sorting inside the query costs time on every refresh and is lost anyway if you load to a PivotTable or the Data Model. Sort in the query only when the order of the output table matters, and do it as one of the last steps.

Check yourself

1. The Region label appears only on the first row of each group. Which command fixes it?

2. Why is filtering out 'Total' safer than Remove Bottom Rows 1?

3. Power Query's Remove Duplicates treats ANA@example.com and ana@example.com as…

4. Which column command ignores new columns that appear in the source later?

5. What does Fill Down do with empty text values?