Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query List.Last: Step-by-Step Guide

· 4 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

List.Last returns the last item in a Power Query list. The syntax is List.Last(list as list, optional defaultValue as any) as any. If the list is empty, it throws an error unless you supply a default value. The function works in both Power Query in Excel and Power BI Desktop, and the steps below are written for the Power Query Editor; menu labels can differ slightly by version, but the function itself behaves the same.

What You'll Need

  • Excel (with Power Query, available in Excel 2016 and later) or Power BI Desktop.
  • A query that produces a list, or a table column you can convert to a list.
  • Basic familiarity with the Power Query Editor and the formula bar.

Step-by-Step

  1. Open Power Query Editor. In Excel, select a cell in your data and go to Data > Get & Transform Data > From Table/Range. In Power BI Desktop, use Home > Transform data.
  2. Create or load the list you want to work with. You can use an existing query that returns a list, or convert a table column: select the column, then Transform > Convert to List (or right-click the column header and choose Convert to List).
  3. Open the formula bar if it is hidden: View > Formula Bar.
  4. Add a step that calls List.Last. In the formula bar, edit the current step or add a new step using the Advanced Editor (Home > Advanced Editor). For example, if your previous step is named Source, add a step like LastItem = List.Last(Source). If you are working with a column named "Names" from a table, first convert it to a list, then apply List.Last to that list step.
  5. If the list might be empty, provide a default value: List.Last(Source, null) or List.Last(Source, "None"). This prevents an error when the list has no items.
  6. Click Done in the Advanced Editor, or press Enter in the formula bar, to commit the step. The result appears as a single value in the query output.
  7. To use the result in a table, you can add it as a custom column. For example, if you have a table with a column of lists, use Transform > Custom Column and enter a formula like List.Last([ColumnName]). Note that this returns the last item of each list in that row.
  8. Check the result. If the list is empty and you did not provide a default, you will see an error. Use the default parameter to handle that case.

Common Mistakes

  • Forgetting that List.Last throws an error on an empty list. Always supply a default value if the list might be empty.
  • Passing a table instead of a list. List.Last expects a list; if you have a table, convert the column to a list first or use Table.LastColumn or similar functions.
  • Using List.Last on a list of records and expecting a specific field. It returns the whole record; you need to expand it or use field access.
  • Assuming List.Last works on a column directly in a custom column without converting. In a custom column, [ColumnName] refers to the value in that row; if that value is a list, List.Last works, but if it is a table, it does not.
  • Not handling nulls. If the list contains nulls, List.Last returns the last item even if it is null; that is correct, but be aware.

Tips

  • Use List.Last with a default value to make your query robust against empty lists.
  • Combine with List.Count to check if the list has items before calling List.Last, though the default parameter is simpler.
  • List.Last is not the same as List.LastN, which returns a list of the last N items. Use List.Last when you need a single value.
  • For performance, List.Last is efficient on lists; it does not need to scan the entire list if the list is already materialized, but in Power Query it generally evaluates the list.
  • Remember that Power Query is case-sensitive for function names: use List.Last, not list.last.

Troubleshooting

If you get an error saying "Expression.Error: The list is empty", add a default value as the second argument. If you get "We cannot convert a value of type Table to type List", you are passing a table; convert the column to a list first. If the function is not recognized, check the spelling and capitalization. If the result is not what you expect, verify that the list is in the order you think; Power Query lists are ordered, but some operations (like sorting) may change the order. Use the Advanced Editor to inspect the step and ensure the input is a list. If you are using a custom column, ensure the column contains lists, not text or numbers.

Frequently Asked Questions

What does List.Last do in Power Query?

It returns the last item of a list. If the list is empty, it errors unless you provide a default value.

How do I get the last item of a column?

Convert the column to a list (Transform > Convert to List), then apply List.Last to that list step.

Can List.Last handle empty lists?

Yes, if you provide the optional second argument, e.g., List.Last(myList, null).

What is the difference between List.Last and List.LastN?

List.Last returns a single value (the last item). List.LastN returns a list of the last N items.

Does List.Last work on a list of records?

Yes, it returns the last record. You can then access fields using the field name.

Is List.Last case-sensitive?

Function names in Power Query are case-sensitive, so use List.Last exactly.