Power Query List.Numbers Function Guide
In this article
To use the List.Numbers function in Power Query, you must enter the formula directly into the formula bar or a custom column dialog using the syntax List.Numbers(start, count, optional step). This function generates a list of sequential numbers, which is essential for creating date tables, generating ID sequences, or creating dummy data for testing models. It is a core M language function that allows you to automate the creation of numeric series without manually typing every value.
What You'll Need
- Microsoft Excel (with Power Query enabled) or Power BI Desktop.
- A basic understanding of the Power Query interface.
- Access to the Formula Bar (if it is not visible, go to the View tab and check the Formula Bar box).
Step-by-Step
- Open the Power Query Editor: In Excel, go to the Data tab, select Get Data, and choose your source. In Power BI, click Transform Data on the Home ribbon. This opens the dedicated Power Query Editor window where M code is executed.
- Initiate a New Query or Custom Column: If you want to create a brand new list as its own query, right-click in the Queries pane on the left and select New Query > Other Sources > Blank Query. If you want to add a list to an existing table, go to the Add Column tab on the top ribbon and select Custom Column.
- Access the Formula Bar: If you chose a Blank Query, click inside the formula bar at the top of the screen. If you chose Custom Column, a dialog box will appear with a space to enter a formula.
- Enter the Basic Formula: To create a simple list, type the following:
= List.Numbers(1, 10). This tells Power Query to start at the number 1 and generate a list containing exactly 10 numbers. Press Enter. You will see a list containing numbers 1 through 10. - Apply an Increment (Step) Value: To create a list that skips numbers (for example, only even numbers), use the third argument. Type
= List.Numbers(0, 11, 2). This starts at 0, generates 11 items (0, 2, 4... up to 20), and increments by 2 each time. - Expand the List to a Table: A list is a single object in a cell. To turn this into a usable column in a table, look at the column header. You will see a small icon with two arrows pointing away from each other. Click this icon and select 'Expand to New Rows'. This converts the list into individual rows in your table.
- Convert to Data Type: Once expanded, the column might be labeled as 'Any'. Right-click the column header, select Change Type, and choose Whole Number or Decimal Number to ensure your data is ready for calculations.
Common Mistakes
One of the most frequent errors occurs with the second argument of the function: the 'count'. Users often mistake the count for the 'end number'. If you want a list from 1 to 100, you cannot write List.Numbers(1, 100) if you actually meant you want the numbers to go up to 100 but only want 10 numbers. Conversely, if you write List.Numbers(1, 100), you will get 100 items. If you actually wanted the list to end at the number 100, you must calculate the count (End Number - Start Number + 1). For example, to get 1 to 100, you use List.Numbers(1, 100). To get 50 to 100, you must use List.Numbers(50, 51).
Another mistake is forgetting the comma between arguments or using incorrect syntax. M language is case-sensitive. Writing list.numbers instead of List.Numbers will result in an error stating that the identifier is not recognized. Always ensure the capital 'L' and capital 'N' are used.
A third mistake involves the 'step' argument. If you provide a step value of 0 or a negative number when you intended to go upwards, Power Query will either return an error or an empty list. If you want to count downwards, your step must be a negative number. For example, List.Numbers(10, 5, -1) will produce 10, 9, 8, 7, 6.
Tips
Creating Date Tables: While List.Numbers generates integers, it is often used in conjunction with other functions to build calendar tables. You can generate a list of numbers representing the days in a year and then use Date.AddDays to transform those numbers into actual date objects. This is much more efficient than manual entry.
Dynamic Counts: Instead of hardcoding a number like 10, you can use a calculation for the count. For instance, if you have a table that tells you how many items are in a stockroom, you can reference that value: List.Numbers(1, Table.RowCount(PreviousStepName)). This makes your query dynamic so it updates automatically when your data changes.
Memory Management: Be cautious when generating extremely large lists. While Power Query is powerful, asking for List.Numbers(1, 10000000) can consume significant system memory and slow down your refresh times. Always aim for the minimum count necessary for your specific business logic.
Troubleshooting
If you receive an error message, follow these steps in order:
- Check Case Sensitivity: Ensure the function is written exactly as
List.Numbers. Check that all arguments are separated by commas. - Verify Argument Types: The first two arguments (start and count) must be numbers. If you are referencing a column or another query to provide these values, ensure that the source value is actually a numeric type and not a text string. You can use
Number.From()to wrap a value if you suspect it is being treated as text. - Check for Zero or Negative Counts: If your count argument results in 0 or a negative number, the function will return an empty list. Check your math, especially if you are using a formula like
[EndValue] - [StartValue]to determine the count. - Review the 'Step' Value: If you are getting an error regarding an infinite loop or an invalid step, ensure your step value is not zero. If you are trying to go up, the step must be positive. If you are trying to go down, the step must be negative.
- Test with Hardcoded Values: If your dynamic formula is failing, replace the variables with simple numbers like
1and10. IfList.Numbers(1, 10)works, then the issue lies in the logic of your dynamic arguments, not the function itself.
Frequently Asked Questions
How do I make the list go backwards?
To make the list count down, set the third argument (the step) to a negative number. For example, List.Numbers(10, 5, -1) starts at 10 and subtracts 1 for each of the 5 items.
Can I use decimal numbers in the list?
Yes, the function supports decimals. You can start at 0, have a count of 11, and a step of 0.5 to get 0, 0.5, 1.0, etc. Just ensure your column data type is set to 'Decimal Number' after expanding.
What is the difference between a List and a Table in Power Query?
A List is a single column of values (a one-dimensional array). A Table is a two-dimensional structure with rows and columns. List.Numbers creates a List, which you must then 'Expand to New Rows' to turn into a Table column.
Why does my list show 'List' in the cell instead of numbers?
This happens because the result of the function is a List object. To see the actual numbers, you must click the expand icon in the column header to convert the list into individual rows.
Can I use List.Numbers to create a sequence of dates?
Not directly, as it only generates numbers. However, you can generate a list of numbers and then use the Date.AddDays function in a custom column to turn those numbers into a sequence of dates starting from a specific date.
Does the count argument include the start number?
Yes. If you start at 1 and the count is 5, the list will be 1, 2, 3, 4, 5. The start number is always the first item in the list.