Power Query Table.AlternateRows Function Guide
In this article
The Table.AlternateRows function in Power Query is used to remove or keep rows in a table based on a repeating pattern. It allows you to specify a starting row, the number of rows to keep, and the number of rows to remove, effectively letting you "strip" your data of every second, third, or Nth row. This is particularly useful when dealing with legacy reports where data is exported with blank spacer rows or where a single record is split across two physical lines in the source file.
What You'll Need
- Microsoft Excel or Power BI Desktop.
- A dataset loaded into the Power Query Editor.
- Basic familiarity with the Power Query Formula Bar (as this function is not available via a standard button in the ribbon).
Step-by-Step
- Open the Power Query Editor: In Excel, go to the Data tab and select Get Data > Launch Power Query Editor. In Power BI, click Transform Data on the Home ribbon.
- Identify Your Table Name: Look at the Applied Steps pane on the right. The last step listed (e.g., "Changed Type" or "Promoted Headers") is the name of the table you will reference in your function. If the step name has spaces, you must wrap it in quotes and precede it with a hashtag (e.g.,
#"Changed Type"). - Enable the Formula Bar: If you cannot see the formula bar at the top of the screen, go to the View tab and check the box for Formula Bar. This is essential because
Table.AlternateRowsmust be entered manually. - Insert a Custom Step: Click the
fxicon to the left of the formula bar. This creates a new step that defaults to= PreviousStepName. - Enter the Function Syntax: Replace the default text in the formula bar with the following syntax:
= Table.AlternateRows(PreviousStepName, offset, keep, remove)For example, if your previous step was#"Changed Type"and you want to remove every other row starting from the second row, you would enter:= Table.AlternateRows(#"Changed Type", 1, 1, 1) - Define the Parameters:
- Table: The name of the table/step you are modifying.
- Offset: The zero-based index of the first row to be affected. An offset of 0 starts at the first row; an offset of 1 starts at the second row.
- Keep: The number of rows to keep in each cycle.
- Remove: The number of rows to remove in each cycle.
- Apply and Verify: Press Enter. Power Query will immediately process the table. Scroll through your data to ensure the correct rows were removed.
- Rename the Step: Right-click the new step in the Applied Steps pane and select Rename. Call it something descriptive like "Removed Alternate Rows" so you remember why this logic was applied during future audits.
Common Mistakes
One of the most frequent errors is forgetting that the offset is zero-based. Users often enter "1" thinking it refers to the first row of the table, but in Power Query logic, 0 is the first row and 1 is the second. If you want to keep the first row and remove the second, your offset must be 1.
Another common mistake is confusing the Keep and Remove arguments. If you set Keep to 2 and Remove to 1, Power Query will keep two rows and then delete one, repeating this 2-1-2-1 pattern. If your goal is to remove every other row, both values must be 1. If you accidentally swap them, you may find yourself deleting the data you intended to save.
Users also frequently forget to handle step names with spaces. If you simply type Table.AlternateRows(Changed Type, 1, 1, 1), Power Query will throw a syntax error because it sees "Changed" and "Type" as two separate entities. You must use the #"Step Name" format to tell the engine that the phrase is a single identifier.
Finally, applying this function before cleaning headers can lead to issues. If you use Table.AlternateRows before promoting headers, the header row is treated as Row 0. If your offset is 0, you might accidentally delete your column titles.
Tips
To handle complex patterns, such as keeping one row and removing three, simply adjust the Remove parameter to 3. This is incredibly helpful for reports that have multiple empty lines or decorative separators between data entries.
If you are unsure about the offset, create a temporary Index Column (Add Column > Index Column > From 0). This allows you to see exactly which row number corresponds to the data you want to target. Once you have determined the correct offset, you can delete the Index Column and apply the Table.AlternateRows function.
For dynamic datasets where the pattern might change, consider using a Conditional Column instead. While Table.AlternateRows is faster to write, a conditional column based on a modulo operation (e.g., Number.Mod([Index], 2) = 0) allows you to filter rows more flexibly using the standard filter dropdown menus.
Combine this function with Table.SelectRows if you need to remove specific values after you have handled the structural alternating rows. This two-step cleaning process ensures that structural noise is removed first, and data-specific noise is removed second.
Troubleshooting
Error: "The name 'PreviousStepName' wasn't recognized."
This happens when the table name inside the parentheses does not exactly match the name of the preceding step in the Applied Steps list. Check for typos, case sensitivity, and ensure you have used the #" " notation for names containing spaces.
Problem: The wrong rows are being deleted.
Check your offset. If you wanted to remove the 2nd, 4th, and 6th rows, your offset should be 1. If you are removing the 1st, 3rd, and 5th rows, your offset should be 0. If the pattern is shifted, increment or decrement the offset by 1 and press Enter to see the result.
Problem: The function is slowing down the query.
Table.AlternateRows is generally efficient, but on massive datasets (millions of rows), any step that alters the row structure can trigger a full table scan. If performance drops, try to perform your filtering and column removals before applying the alternating row logic to reduce the volume of data the function has to process.
Problem: Data is missing that shouldn't be.
Verify that your source data is consistent. If the source file occasionally has two blank rows instead of one, a fixed 1-keep/1-remove pattern will eventually get "out of sync," and you will start deleting actual data rows. In this case, avoid Table.AlternateRows and instead use a filter on a specific column to remove nulls or blanks.
Frequently Asked Questions
Can I use Table.AlternateRows to remove only the first 5 rows?
No. This function is designed for repeating patterns. To remove a specific number of rows from the top, use Table.Skip instead.
Is there a button in the ribbon for this function?
No, Table.AlternateRows is an M-code function. You must enter it manually into the formula bar or via the Advanced Editor.
Does this function permanently delete data from the source file?
No. Power Query is a transformation layer. It only changes how the data is viewed and loaded into your report; the original source file remains untouched.
Can I keep 3 rows and remove 2?
Yes. By setting the Keep parameter to 3 and the Remove parameter to 2, Power Query will maintain a repeating cycle of 3 rows kept followed by 2 rows deleted.
What happens if the table ends in the middle of a "Remove" cycle?
Power Query simply stops. If the function is told to remove 2 rows but only 1 row remains at the end of the table, it will remove that final row and complete the operation without error.