Power Query Number.Permutations Function Guide
In this article
The Number.Permutations function in Power Query (M language) calculates the number of ways to choose a specific number of items from a larger set where the order of selection matters. For example, if you have 10 employees and need to determine how many different ways you can assign a President, a Vice President, and a Secretary, you are calculating permutations. This function returns a single numeric value representing the total possible arrangements without actually listing the combinations themselves.
What You'll Need
- Microsoft Excel (with Power Query installed) or Power BI Desktop.
- A dataset containing the total number of items (the pool) and the number of items to be selected.
- Access to the Power Query Editor (Get Data > Launch Power Query Editor).
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.
- Prepare Your Source Data: Ensure you have two columns in your table. One column should represent the total number of available items (n) and the second column should represent the number of items to choose (k). For example, a column named "TotalItems" and a column named "Slots".
- Add a Custom Column: Navigate to the Add Column tab on the top ribbon and select Custom Column. This opens the Custom Column dialog box where you can write your M formula.
- Enter the Permutation Formula: In the Custom column formula box, enter the following syntax:
Number.Permutations([TotalItems], [Slots]). Replace "TotalItems" and "Slots" with the actual names of your columns. Note that Power Query is case-sensitive, so you must capitalize the "N" and "P" exactly as shown. - Verify Data Types: Once the column is created, the resulting values will likely be set to the "Any" data type. Click the data type icon (usually "ABC/123") in the column header and select Whole Number. This ensures that the mathematical result is treated as an integer for further calculations.
- Handle Nulls or Zeros: If your source data contains nulls, the function will return an error. To prevent this, you can wrap the function in an if-statement:
if [TotalItems] <> null and [Slots] <> null then Number.Permutations([TotalItems], [Slots]) else null. - Apply and Close: Once you have verified the results, go to the Home tab and select Close & Apply to load the calculated permutations back into your Excel worksheet or Power BI model.
Common Mistakes
One of the most frequent errors is confusing Permutations with Combinations. In a permutation, the order is critical. If you are picking a gold, silver, and bronze medalist, that is a permutation because the order of the finish determines the medal. If you are simply picking three people to be on a committee where everyone has the same rank, that is a combination. Using Number.Permutations for a committee selection will result in a number that is far too high because it counts the same group of people in different orders as unique events.
Another common mistake is the Argument Order. The function requires the total set (n) as the first argument and the subset (k) as the second. If you accidentally swap these—putting the smaller number first—the function will return 0 or an error because you cannot mathematically choose 10 items from a set of 3.
Case Sensitivity is a recurring hurdle for new Power Query users. Writing number.permutations (lowercase n) or Number.permutation (singular) will result in a "Token Literal expected" or "The name... wasn't recognized" error. Power Query's M language is strictly case-sensitive.
Finally, users often forget to check for negative numbers. Permutations are defined for non-negative integers. If your source data contains negative values due to a calculation error elsewhere in your pipeline, Number.Permutations will throw an error for that specific row.
Tips
To make your queries more robust, use Parameterization. If you are calculating permutations for a fixed set (e.g., always choosing 3 items from a varying pool), you can replace the second argument with a static number: Number.Permutations([TotalItems], 3). This simplifies the table structure by removing the need for a "Slots" column.
For those dealing with extremely large numbers, be aware of Integer Overflow. While Power Query handles large numbers well, if you are calculating permutations of very large sets (e.g., 100 items chosen 20 at a time), the result may exceed the capacity of standard integer types or lead to scientific notation. If you need to perform further math on these results, ensure your data type is set to Decimal Number to maintain precision.
If you need to calculate the actual list of permutations rather than just the count, Number.Permutations cannot do this. You would instead need to create a custom recursive function or use a List.Generate approach to iterate through all possible arrangements. Number.Permutations is strictly a counting function.
Combine this function with Conditional Columns to create "What-If" scenarios. For example, you can create a column that calculates permutations only if the total number of items is below a certain threshold, allowing you to flag datasets that are too large to be computationally viable for actual listing.
Troubleshooting
If the function returns an "Expression.Error: The number of items to choose cannot be greater than the total number of items": This happens when your second argument (k) is larger than your first argument (n). Check your source columns for data entry errors. You can fix this by adding a custom column that uses a Number.Min logic or an if statement to ensure the subset is always less than or equal to the total set.
If the result is "Error" in every cell: First, check the case sensitivity of the function name. Second, check for null values in your input columns. A single null in either the total or the slots column will cause the function to fail for that row. Use the "Replace Values" feature to replace nulls with 0 or a default number before applying the function.
If the result is 0 when you expect a positive number: Ensure that your input columns are actually formatted as numbers. If Power Query perceives the columns as "Text," the function may behave unpredictably or fail. Change the column types to "Whole Number" before adding the custom column.
If the query is running very slowly: While Number.Permutations is a fast mathematical operation, applying it to millions of rows can slow down the refresh. Try to filter your data to only the necessary rows before adding the custom column to reduce the computational load on the Power Query engine.
Frequently Asked Questions
What is the difference between Number.Permutations and Number.Combinations?
Permutations care about the order of the items (ABC is different from CBA), while combinations do not (ABC and CBA are considered the same group). Use permutations for rankings, schedules, or specific roles.
Can I use this function with decimal numbers?
No. Permutations are based on discrete counts of items. The function expects integers. If you provide decimals, Power Query will typically round them or return an error depending on the context.
Does this function list the actual permutations?
No, it only returns the total count of possible permutations. To generate the actual list of arrangements, you would need to write a complex custom M function using lists and recursion.
What happens if the number of items to choose is 0?
Mathematically, the number of ways to choose 0 items from a set is 1 (the empty set). Number.Permutations will return 1 in this scenario.
Is there a limit to how large the input numbers can be?
While there is no hard-coded limit on the input, the resulting number can grow extremely quickly. If the result exceeds the maximum value for a 64-bit integer, you may encounter precision issues or errors.
Can I use this function in a Power BI DAX measure?
No, Number.Permutations is an M function used in the Power Query Editor. For DAX, you would need to use the PERMUT function in a calculated column or measure.