Smart Systems for Home

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

Power Query Number.Factorial: How-To Guide

· 5 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.Factorial function in Power Query returns the factorial of a given non-negative integer. For example, Number.Factorial(5) returns 120 (5 × 4 × 3 × 2 × 1). It is a simple mathematical function you can use in custom columns, M code, or when transforming data in Power BI, Excel Power Query, or Dataflows. You need nothing special installed—it works in any environment where Power Query is available.

What You'll Need

  • A Power Query environment (Excel, Power BI, or Dataflows).
  • Basic familiarity with the Power Query editor and the formula bar.
  • Data or a manual entry where you want to apply the factorial.

Step-by-Step

  1. Open Power Query Editor: In Excel, go to the Data tab and click From Table/Range (or From Other Sources). In Power BI, click Transform Data from the Home tab. This opens the Power Query editor.
  2. Add a custom column: Go to the Add Column tab and click Custom Column. This opens a dialog where you can write M code.
  3. Write the formula: In the Custom Column dialog, give your column a name (e.g., "Factorial"). In the formula box, enter Number.Factorial([YourColumnName])—replace [YourColumnName] with the actual column that contains your numbers. For example, if your column is named "Value", enter Number.Factorial([Value]).
  4. Click OK: Power Query will create a new column with the factorial of each number in the original column.
  5. Use it in M code directly: If you prefer writing M code, you can use the function in the formula bar. For instance, in a new step, type = Table.AddColumn(#"Previous Step", "Factorial", each Number.Factorial([Value])).
  6. Test with a literal value: To see how it works, you can also create a blank query (right-click in the Queries pane, select New Query > Other Sources > Blank Query). In the formula bar, type = Number.Factorial(5) and press Enter. The result will be 120.
  7. Handle non-integer or negative numbers: The function expects a non-negative integer. If you have decimals or negatives, you can round first using Number.Round or filter them out. For example, Number.Factorial(Number.Round([Value])) will round to the nearest integer before computing.

Common Mistakes

  • Using negative numbers: Number.Factorial(-1) returns an error. The factorial is only defined for non-negative integers.
  • Using decimals: Number.Factorial(4.5) returns an error because the function expects an integer. Always round or truncate first.
  • Using text or null values: If your column contains text or blanks, the function will return an error. Use try or filter out non-numeric values.
  • Forgetting the column reference: In a custom column, you must reference the column with square brackets, like [Value]. If you forget the brackets, you'll get a syntax error.
  • Confusing with other functions: Number.Factorial is different from Number.Permutations or Number.Combinations. Make sure you use the right one for your calculation.

Tips

  • Use for combinations and permutations: The factorial is key in combinatorics. For example, the number of ways to arrange 5 items is Number.Factorial(5) = 120.
  • Combine with other functions: You can nest it inside other M functions. For instance, to calculate combinations, use Number.Factorial(n) / (Number.Factorial(k) * Number.Factorial(n - k)).
  • Handle large numbers carefully: Factorials grow extremely fast. Number.Factorial(20) is already 2.43 quadrillion. Power Query uses 64-bit integers, so anything above 20 will overflow and return an error or a wrong result. For larger values, consider using a different approach or a decimal type.
  • Use try for error handling: Wrap the function in try to catch errors on invalid inputs. For example, try Number.Factorial([Value]) otherwise null will return null instead of an error.
  • Check data types: Ensure your column is set to Whole Number (integer) before applying the function. You can change the data type in the column header.

Troubleshooting

If you get an error saying "Expression.Error: The argument 'number' is invalid": This usually means the input is not a non-negative integer. Check your data for decimals, negatives, or text. You can add a step to filter or transform the data before applying the factorial.

If the result is too large and shows as an error: As mentioned, factorials above 20 exceed the 64-bit integer limit. You can change the column data type to Decimal or use a custom function that returns a decimal. However, note that even decimal types have limits. For very large numbers, you might need to use a different method, such as logarithms.

If the custom column dialog doesn't accept the formula: Double-check the syntax. The function name is case-sensitive: Number.Factorial with a capital N and F. Also, ensure you are referencing the column correctly with square brackets.

If the function isn't available: The Number.Factorial function has been available since Power Query was introduced. If you are using an older version or a limited environment, update your software or check if the function is listed in the M reference. In rare cases, you may need to enable the function library.

If you see a different error like "Token unexpected": This is a syntax error. Check for missing commas, parentheses, or brackets. A common mistake is writing Number.Factorial [Value] instead of Number.Factorial([Value]).

If the column returns null for all rows: Check if your column has headers or if the data type is set to Text. Convert the column to Whole Number first.

Frequently Asked Questions

Can I use Number.Factorial with a decimal number?

No, the function only accepts non-negative integers. If you have a decimal, you must round it first, for example using Number.Round or Number.Truncate.

What is the largest number I can calculate the factorial of?

With the default 64-bit integer type, you can go up to 20. For 21 and above, you'll get an overflow error. You can try using a decimal type, but it will still have precision limits.

Does Number.Factorial work with zero?

Yes, Number.Factorial(0) returns 1. This is mathematically correct because 0! = 1.

Can I use Number.Factorial in a calculated column in Power BI?

Yes, you can use it in calculated columns, but note that calculated columns in Power BI use DAX, not M. In DAX, the equivalent is FACT(). If you are in Power Query, you use M code. In Power BI, you can add a custom column in Power Query, or use DAX's FACT function.

How do I calculate combinations in Power Query?

You can use the formula Number.Factorial(n) / (Number.Factorial(k) * Number.Factorial(n - k)) where n is the total number of items and k is the number chosen. Alternatively, use Number.Combinations(n, k) which is a built-in function.

Why does Number.Factorial return an error for negative numbers?

The factorial is defined only for non-negative integers. For negative numbers, the factorial is undefined in standard mathematics, so Power Query returns an error. If you need to handle negatives, you can use Number.Abs to take the absolute value first, but note that this changes the mathematical meaning.