Smart Systems for Home

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

Power Query Number.Combinations Function Guide

· 6 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

The Number.Combinations 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 does not matter. For example, if you have a list of 10 employees and need to know how many different 3-person committees can be formed, this function provides that mathematical result. It is a fundamental tool for statistical analysis, probability calculations, and data validation within Power BI and Excel.

What You'll Need

  • Microsoft Excel (with Power Query/Get & Transform) or Power BI Desktop.
  • A basic understanding of how to enter a Custom Column formula.
  • Two numeric values: the total number of items (n) and the number of items to choose (k).

Step-by-Step

  1. 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.
  2. Prepare Your Data: Ensure your table has two columns containing the numbers you wish to use. For instance, Column A might be "Total Population" and Column B might be "Sample Size". Ensure these columns are set to a Whole Number data type.
  3. 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.
  4. Enter the Function: In the Custom column formula box, type the following: Number.Combinations([Total Population], [Sample Size]). Replace the bracketed names with the actual names of your columns.
  5. Verify the Formula: Ensure there are no syntax errors. The function requires two arguments: the first is the total number of items (n), and the second is the number of items to be chosen (k).
  6. Confirm and Apply: Click OK. Power Query will generate a new column containing the result of the combination formula for every row in your dataset.
  7. Set Data Type: Right-click the header of the new column, select Change Type, and choose Whole Number to ensure the result is treated as an integer for further calculations.
  8. Close and Apply: Go to the Home tab and select Close & Apply to load the calculated combinations back into your Excel worksheet or Power BI model.

Common Mistakes

One of the most frequent errors is confusing Number.Combinations with Number.Permutations. In a combination, the order does not matter (picking Alice and Bob is the same as picking Bob and Alice). In a permutation, order does matter. Using the wrong function will lead to significantly inflated numbers in your reports.

Another common mistake is attempting to use the function with non-integer values. Because you cannot choose 2.5 items from a set of 10, providing a decimal or a text string will result in an Expression.Error. Always verify that your source columns are explicitly cast as Whole Number before applying the formula.

Users also frequently forget that the first argument (n) must be greater than or equal to the second argument (k). If your data contains rows where the sample size exceeds the total population, the function will return an error for those specific rows, which can break the entire data refresh process if not handled.

Tips

To handle potential errors caused by invalid inputs (like k being larger than n), wrap your function in a try...otherwise block. For example: try Number.Combinations([n], [k]) otherwise 0. This ensures your query completes successfully even if some rows contain illogical data.

If you are working with extremely large numbers, be aware of the limits of the 64-bit integer. While Power Query can handle very large numbers, if the result exceeds the capacity of the data type, you may see scientific notation or overflow errors. Always check the scale of your expected output when dealing with populations in the thousands.

For better performance in large datasets, perform the combination calculation as the very last step of your transformation process. This prevents Power Query from having to recalculate complex combinations every time you filter or sort the data in earlier steps.

Troubleshooting

If the function returns an "Expression.Error: The number of items to choose must be less than or equal to the total number of items," check your source data for anomalies. Sort your columns in descending order to quickly identify any rows where the second column is larger than the first. Correct these values in the source or use a conditional column to filter them out before the calculation.

If you see "Error" in every cell of the new column, check the case sensitivity of the function. Power Query is case-sensitive; number.combinations (lowercase) will not work. It must be exactly Number.Combinations.

If the result seems mathematically incorrect, double-check that you haven't accidentally used a column that contains duplicates or hidden characters. If your "Total Population" column is actually a text field that looks like a number, the function may fail or produce unexpected results. Use the Transform > Data Type > Whole Number option to be certain.

If the query is running extremely slowly, check if you have applied other complex transformations (like merges or grouping) after the combination step. Moving the Number.Combinations step to the end of the query often improves refresh speeds because it reduces the number of times the engine must re-evaluate the formula during data streaming.

Frequently Asked Questions

What is the difference between combinations and permutations in Power Query?

Combinations are used when the order of selection is irrelevant. Permutations are used when the order is important. For example, choosing 2 people for a team is a combination; choosing 2 people for the roles of President and Vice President is a permutation.

Can I use this function with a list instead of a number?

No, Number.Combinations requires two numeric arguments. If you have a list of items and want to know how many combinations are possible, you must first use List.Count to get the total number of items in that list, and then pass that number into the function.

Does this function support negative numbers?

No. Combinations are based on set theory and counting, which requires non-negative integers. Providing a negative number will result in an error.

Is there a limit to how large the numbers can be?

The function is limited by the maximum value of a 64-bit integer. If the resulting number of combinations is astronomically high, Power Query may return an error or round the number, depending on the context of the calculation.

Can I use this function in a Power BI DAX measure instead?

While Number.Combinations is an M function for Power Query, DAX has a similar function called COMBIN. Use Number.Combinations during the data loading phase (Power Query) and COMBIN for dynamic calculations in reports (DAX).

Why is my column showing as 'Any' type after the calculation?

Power Query often assigns the 'Any' type to custom columns by default. You must manually change the data type to 'Whole Number' using the column header menu to ensure it behaves correctly in pivots or charts.