Power Query List.Alternate: Step-by-Step Guide
In this article
List.Alternate returns a new list containing every Nth item from a source list, starting at a given offset, and optionally skipping a number of items between each pick. It is the function to reach for when you need to keep every second row, every third value, or alternate blocks of rows in Power Query. You do not need any add-ins or special permissions, just Power Query in Excel, Power BI Desktop, or another tool that hosts it.
What You'll Need
- Access to Power Query (Excel: Data tab > Get & Transform Data; Power BI Desktop: Transform data).
- A list to work on. This can be a column from a table, a manually entered list, or the result of another Power Query step.
- Basic familiarity with the Power Query Editor and the formula bar. Labels in the ribbon can differ slightly by version, device, or region.
Step-by-Step
- Load your data into Power Query. For example, select a table in Excel and choose Data > From Table/Range. In Power BI Desktop, use Home > Transform data to open the editor.
- Make sure the data you want to work with is a list. If it is a column in a table, you can convert it to a list by right-clicking the column header and choosing Drill Down (or using the context menu). The result is a list value shown in the formula bar as something like
{1, 2, 3, 4}. - Open the Advanced Editor (Home > Advanced Editor) or add a new step in the formula bar. You will write a formula that calls List.Alternate.
- Understand the function signature:
List.Alternate(list, count, repeat, offset). The arguments are:- list: the source list.
- count: how many items to skip each time. Use 1 to keep every other item, 2 to keep every third item, and so on.
- repeat: how many items to keep after each skip. Usually 1.
- offset: how many items to skip at the very start before the pattern begins. Use 0 to start with the first item.
- Write the formula. For example, to keep every second item from a list named
MyList, starting with the first item, use:List.Alternate(MyList, 1, 1, 0). This returns items at positions 1, 3, 5, and so on. - To keep every second item but start with the second item, use an offset of 1:
List.Alternate(MyList, 1, 1, 1). This returns items at positions 2, 4, 6, and so on. - To keep blocks of two items and skip one, use:
List.Alternate(MyList, 1, 2, 0). This returns items 1, 2, 4, 5, 7, 8, and so on. - Press Done in the Advanced Editor (or Enter in the formula bar) to apply the step. The result is a new list. You can then convert it back to a table if needed: right-click the list and choose To Table.
- Rename the step for clarity and continue building your query.
Common Mistakes
- Confusing count and repeat. Count is how many items to skip, repeat is how many to keep. Swapping them gives unexpected results.
- Forgetting the offset. If you want to start on the second item, you must set offset to 1. The default offset is 0, which starts at the first item.
- Using a table instead of a list. List.Alternate expects a list. If you pass a table, you will get an error. Drill down to a list first.
- Assuming count means "keep every Nth". Count is the number of items to skip, not the interval. To keep every second item, count is 1 (skip one, keep one).
- Not handling nulls or errors. List.Alternate works on the list as-is. If your list contains nulls or errors, they are treated as items and can shift the pattern.
Tips
- Use List.Alternate to split a list into two alternating lists: one with
List.Alternate(MyList, 1, 1, 0)and one withList.Alternate(MyList, 1, 1, 1). - Combine with List.Transform to modify the selected items before returning them.
- Test with a small list first to confirm the pattern before applying to large data.
- Remember that the function is zero-based for the offset: offset 0 means start at the first item.
- If you need to keep every Nth item but the list length is not a multiple of N, the last group may be shorter. That is normal.
Troubleshooting
- Error: "We cannot convert a value of type Table to type List." You passed a table. Drill down to a list first.
- Unexpected items returned. Double-check the order of arguments: list, count, repeat, offset. A common mistake is to put offset before repeat.
- Function not recognized. Ensure you are using Power Query, not Excel worksheet functions. List.Alternate is only available in Power Query.
- Result is empty. Check if count is larger than the list length, or if offset is beyond the list length. Adjust the arguments.
- Performance issues with very large lists. List.Alternate is generally efficient, but if you are working with millions of items, consider filtering earlier in the query.
Frequently Asked Questions
What does List.Alternate do in Power Query?
It returns a new list by skipping a specified number of items and then keeping a specified number of items, starting at a given offset. It is commonly used to select every Nth item.
How do I keep every second row using List.Alternate?
Use List.Alternate(MyList, 1, 1, 0) to keep items at positions 1, 3, 5, etc. Use offset 1 to keep items at positions 2, 4, 6, etc.
Can I use List.Alternate on a table column?
Yes, but first convert the column to a list by drilling down. Right-click the column header and choose Drill Down, then apply List.Alternate to the resulting list.
What is the difference between count and repeat?
Count is the number of items to skip each cycle. Repeat is the number of items to keep after each skip. For example, count=1, repeat=1 keeps every other item.
Does List.Alternate work in Excel and Power BI?
Yes, it is part of the Power Query M formula language, which is available in Excel, Power BI Desktop, and other Microsoft products that include Power Query.
What happens if the list has fewer items than count plus repeat?
The function returns as many items as it can according to the pattern. If the list ends mid-cycle, the result may be shorter than expected.