Power Query List.LastN Function: How to Use It
In this article
The List.LastN function in Power Query retrieves the last N items from a list, table, or column. It requires a list or table as input and an integer specifying how many items to return from the end. If N is greater than the number of items, it returns all items; if N is zero or negative, it returns an empty list. No special setup is needed beyond having Power Query enabled in Excel or Power BI.
What You'll Need
- A working list or table in Power Query (e.g., from Excel, a database, or a CSV file)
- Power Query Editor open (available in Excel under Data > Get Data > Launch Power Query Editor, or in Power BI via Home > Transform data)
- Basic familiarity with Power Query’s interface and M language syntax (optional but helpful)
Step-by-Step
- Open Power Query Editor by navigating to Data > Get Data > Launch Power Query Editor in Excel, or Home > Transform data in Power BI Desktop. Ensure your data is loaded as a query (e.g., a table or list).
- Create or identify a list to apply List.LastN to. If your data is in a table, you’ll first need to extract a column as a list. Right-click the query in the Queries pane, select Reference, or duplicate it if you want to preserve the original. Then, click the column header of the column you want to process, right-click, and choose Convert to > List. Alternatively, use the UI to expand a column to a list in a new query.
- In the Power Query Editor, go to the Home tab and click Advanced Editor. This opens the M code editor where you’ll modify or write the formula.
- Locate or create a variable containing your list. For example, if you have a list named SourceList, your code might start with
let Source = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, in Source. - Insert the List.LastN function. Replace or append the final expression with
List.LastN(SourceList, 3), where 3 is the number of items to retrieve from the end. For instance,List.LastN({1,2,3,4,5,6,7,8,9,10}, 3)returns {8,9,10}. - Click Done to apply the change. The query preview will update to show only the last N items. If using a table, you can instead reference a column inside List.LastN using the table’s column syntax, like
List.LastN(#"Changed Type"[Amount], 5). - Close & Load to return the result to Excel or Power BI. The output will be a new table or list containing only the last N rows/items.
Common Mistakes
- Using non-integer values for N (e.g., 2.5 or "3")—this causes an error. The second argument must be a whole number (Int64.Type).
- Applying List.LastN directly to a table without converting a column to a list first. List.LastN expects a list, not a table. To use it on a table, extract the column as a list or use Table.LastN instead.
- Assuming it returns items in reverse order. List.LastN preserves the original order of the last N items. For example,
List.LastN({1,2,3,4,5}, 3)yields {3,4,5}, not {5,4,3}. - Forgetting that N=0 returns an empty list, and negative numbers (e.g., -1) also return {}—not an error, but often unintended.
- Not checking for null or blank inputs. If the source list is null, List.LastN(null, N) returns null, which may break downstream steps.
Tips
- Combine with List.Buffer if your list comes from a large query or external source. This improves performance by caching the list in memory before extraction:
List.LastN(List.Buffer(MyList), 5). - Use List.LastN dynamically with parameters. Create a parameter (e.g., LastNCount = 5) and reference it:
List.LastN(Source, LastNCount). This lets users adjust N without editing code. - Combine with List.FirstN for sliding windows. To get the last 3 and first 2 items:
List.Combine({List.FirstN(Source, 2), List.LastN(Source, 3)}). - When working with dates, sort your list first (e.g.,
List.Sort(Source, Order.Descending)) so that "last" means most recent before applying List.LastN. - Use Table.LastN for tables instead of List.LastN. It’s more intuitive for multi-column data:
Table.LastN(#"Previous Step", 4)returns the final 4 rows.
Troubleshooting
When List.LastN doesn’t work as expected, follow these steps:
- Check the data type of the second argument. If it’s text (e.g., "5"), wrap it with Number.FromText or convert to a number using
Number.From(N). Verify by hovering over the argument in Advanced Editor to see its inferred type. - Confirm the input is a list. If referencing a table, ensure you’ve used column extraction syntax:
[ColumnName]orTable.Column(#"Step", "ColumnName"). If unsure, useValue.Is(Source, Type.List)to test. - Handle null or empty lists. Wrap your call with
if List.IsEmpty(Source) or Source = null then {} else List.LastN(Source, N)to avoid null propagation. - Review step dependencies. If you see "Expression.Error: We cannot convert value ... to type List", go back to the previous step and ensure the prior output is a list—not a table, record, or scalar value.
- Check for hidden spaces or special characters in list items. If the list comes from Excel, leading/trailing spaces in text entries won’t affect List.LastN, but they might cause downstream errors—use Table.TransformColumns or Text.Trim during data cleaning.
- If using Power BI and the query fails after refresh, ensure the underlying data source hasn’t changed structure. List.LastN is resilient, but if the column referenced no longer exists, the query breaks.
Frequently Asked Questions
Can List.LastN be used on a table directly?
No. List.LastN only works on lists. For tables, use Table.LastN instead. If you need the last N rows of a table with multiple columns, stick with Table.LastN—it’s simpler and preserves all columns.
What happens if I ask for more items than the list contains?
List.LastN returns all items in the list. For example, List.LastN({1,2}, 10) returns {1,2}, not an error. This makes it safe for variable-length datasets where you want all items if fewer than N exist.
Does List.LastN preserve row order?
Yes. It preserves the original order of the last N items. If your list is sorted ascending, the returned items are in ascending order (e.g., {8,9,10} for the last 3 of ten items). To reverse them, wrap with List.Reverse: List.Reverse(List.LastN(Source, 3)).
Can I use List.LastN with custom sorting?
Yes, but sort first. List.LastN operates on the current order. To get the last three *largest* numbers, sort descending: List.LastN(List.Sort(Source, Order.Descending), 3).
Is there a performance impact with large lists?
Yes, especially with remote data sources. List.LastN requires loading the entire list into memory before extracting the tail. For very large datasets, consider filtering earlier (e.g., using Table.SelectRows) or using Table.LastN on a table with indexed columns, if supported by the data source.
Why does List.LastN return null instead of an empty list?
When the input list is null, List.LastN(null, N) returns null—not {}. If your source may be null (e.g., from a failed query step), explicitly handle it with a conditional: if Source = null then {} else List.LastN(Source, N) to ensure a list is always returned.