To keep specific rows in Power Query, you either filter the rows you want (Home > Keep Rows > Keep Top Rows, Keep Bottom Rows, Keep Range of Rows, or Keep Duplicates) or remove the ones you don't want with Home > Remove Rows (Remove Top Rows, Remove Bottom Rows, Remove Alternate Rows, Remove Blank Rows, Remove Duplicates, or Remove Errors). The most flexible method is a filter on one or more columns, because it stays correct when the data refreshes and changes order. Everything below assumes you already have a query open in the Power Query Editor, which you reach from Excel via Data > Get Data, or in Power BI via Transform Data. Menu labels can differ slightly between Excel, Power BI and versions or regions, so look for the closest matching name if something is not where you expect.
What You'll Need
- A workbook with a query or a table loaded into Power Query (Excel) or a Power BI Desktop file with a query in the Power Query Editor.
- At least one column you can use as the basis for deciding which rows to keep, such as a date, category, status, or numeric value.
- A clear rule in mind: for example “keep the newest 50 orders” or “keep only rows where Status is Active.” Writing the rule down first saves rework.
- Optional but useful: a column of index numbers if you want to keep specific positions. You can add one with Add Column > Index Column.
Step-by-Step
- Open the query in the Power Query Editor. In Excel, click any cell inside your data and go to Data > Get Data, or use Data > Queries & Connections and double-click a query to edit it. In Power BI, click Transform Data on the Home ribbon.
- Decide whether you are keeping by condition or by position. By condition means filtering on a column value; by position means taking the first N, last N, or a numbered range. Filtering is better when the source order may change between refreshes.
- To keep rows by condition, click the filter arrow in the column header, untick the values you don’t want, and click OK. For text columns you can use Text Filters > Equals or Contains. For numbers use Number Filters, and for dates use Date Filters > Between or After. Select several columns in turn if you need a combined condition.
- To keep the first rows, make sure the data is sorted the way you want first (click the column header arrow and choose Sort Ascending or Sort Descending), then go to Home > Keep Rows > Keep Top Rows and enter how many rows to keep.
- To keep the last rows, sort appropriately and use Home > Keep Rows > Keep Bottom Rows, entering the count. This is handy for “latest records” when the source is not in date order, but only if you have sorted by a date column first.
- To keep a middle block, use Home > Keep Rows > Keep Range of Rows. You specify the starting row number and how many rows to keep. Row numbering here starts at zero, so the first row of the table is number 0, not 1. This is a common source of off-by-one errors.
- To keep only duplicate rows, use Home > Keep Rows > Keep Duplicates. Power Query compares entire rows, so identical values across all columns are treated as a duplicate. If you only want to compare certain columns, select those columns first, then use Keep Duplicates.
- If it is easier to describe what to remove, use Home > Remove Rows instead. Remove Top Rows and Remove Bottom Rows work the same way as the keep equivalents. Remove Alternate Rows lets you delete every Nth row starting at a chosen row, which is useful for rows that repeat in pairs. Remove Blank Rows clears completely empty rows. Remove Duplicates keeps the first occurrence and drops later identical rows.
- Check the result. The status bar at the bottom shows how many rows remain, and you can compare that to the original count by looking at the Applied Steps list and clicking the step before your change. If the number looks wrong, delete the step and try again.
- Confirm the steps read sensibly in the Applied Steps pane on the right. Each action you took is recorded as a step that reruns on every refresh. Rename the steps to something meaningful by right-clicking and choosing Rename if you plan to share the file.
- Close and load. Use Home > Close & Load > Close & Load in Excel, or Home > Close & Apply in Power BI, to write the result back to a worksheet or the data model.
Common Mistakes
- Forgetting that sorting matters for positional keeps. Keep Top Rows takes the top rows of the current order. If you do not sort first, you keep an arbitrary block that changes on refresh. Always sort by a stable column such as a date or ID.
- Using a positional keep when a filter is needed. Top N rows are fragile: a new row added to the source can push an old row out. Filters on a condition remain correct as data grows.
- Off-by-one errors in Keep Range of Rows. The starting row is zero-based, so asking for start 1 skips the first row. Test with a small range and check the visible rows before relying on it.
- Filtering on the wrong data type. A number stored as text will not filter like a number. Check the little type icon in the column header and set the correct data type first with Transform > Data Type.
- Expecting Keep Duplicates to compare selected columns only. It compares whole rows unless you narrow the columns first. Remove the extra columns from your selection or use a grouped approach instead.
- Filtering rows and then wondering why totals are wrong. Rows removed in the query are gone from the loaded table. If you still need the full dataset elsewhere, duplicate the query and filter only one copy.
Tips
- Name your steps clearly, such as “Keep only Active customers” rather than “Filtered Rows1.” Anyone opening the file later can then follow the logic without guessing.
- Use the formula bar to see the underlying function each step generates. Keep Rows and Remove Rows steps map to functions like Table.FirstN, Table.LastN, Table.Range, Table.Distinct and Table.RemoveMatchingRows, which helps when you need to edit a step by hand.
- For “keep the latest row per group,” a simple filter will not do it alone. One practical approach is to sort descending by date, then use Home > Group By with an aggregation that takes the maximum value, or use Keep Duplicates on the grouping column after sorting. Test the output against a few known groups.
- When filtering many values at once, consider using a lookup list or a parameter rather than ticking dozens of boxes. A parameter-driven filter is easier to update than a long manual list.
- Add an Index Column before a positional keep if you need to prove which rows survived. You can then compare index values before and after the step.
- Keep a copy of the original query unfiltered so you can always compare row counts and check that the filter did what you intended.
Troubleshooting
The row count did not change. The filter may have matched everything, or the column type is mismatched so the condition is read as text. Check the data type, then reapply the filter and watch the status bar count.
The keep step errors out. Positional keeps fail if the number you entered is larger than the row count, or if the starting row in a range is beyond the data. Reduce the numbers and try again.
Rows you expected to keep disappeared after refresh. A positional keep is re-evaluated on each refresh against whatever order the source delivers. Replace it with a filter on a condition, such as a date after a fixed point or a status value.
Filtered values are not in the list. Refresh the preview first (Home > Refresh Preview) so the filter list reflects the current data. If the source is a database, the filter list may show only a sample until you load more values.
Duplicates are not being removed as expected. Trailing spaces, inconsistent capitalisation or mixed data types make values look different when they are logically the same. Use Transform > Format > Trim and Transform > Format > Capitalize Each Word on text columns before using Remove Duplicates.
Changes do not appear in the worksheet. The query output needs to load. Use Home > Close & Load and then refresh the connection in Excel. If the query is connection-only, no table is written, so switch it to load to a table in the query properties.
Frequently Asked Questions
What is the difference between Keep Rows and Remove Rows?
They do the opposite job. Keep Rows retains a chosen set, such as the top N or duplicates, while Remove Rows deletes a chosen set, such as blanks or the top N. Both record a step in Applied Steps and both run on refresh.
Does Keep Top Rows sort my data automatically?
No. It takes the top rows in the current order. You must sort the column yourself first if the order matters, otherwise the result depends on how the source delivers the data.
Can I keep rows based on a list of values?
Yes, in effect. Filter the column and tick the specific values, or keep a separate query containing the values and use a merge or a parameter to filter against it. The manual filter is simplest when the list is short and rarely changes.
Why does Keep Range of Rows start at zero?
Power Query numbers rows from zero in this dialog, so the first row is 0. If you want to keep rows two through ten, enter 1 as the starting row and 9 as the count.
Will keeping rows change my source data?
No. Power Query never edits the original source. It builds a set of transformation steps and produces a new result each time it loads or refreshes.
Can I undo a keep or remove step?
Yes. In the Applied Steps pane, either click the X next to the step to delete it, or click the step above it to preview the earlier state and then delete the unwanted step.