The M Code Behind the Power Query M function Number.Factorial

In this article, we will explore the M code behind the Number.Factorial function, its syntax, parameters, and usage. So, let’s dive in!


The syntax of the Number.Factorial function is as follows:


Here, number is the input for which the factorial needs to be calculated. It must be a non-negative integer.


The Number.Factorial function has only one parameter, which is the input number.


The Number.Factorial function is used to calculate the factorial of a given number. A factorial of a number is the product of all positive integers from 1 to that number. For example, the factorial of 5 (written as 5!) is calculated as follows:

5! = 5 x 4 x 3 x 2 x 1 = 120

Here, the factorial of 5 is calculated by multiplying all positive integers from 1 to 5.

The Number.Factorial function can be used in Power Query to calculate the factorial of a column of integers. Let's see an example.

Suppose we have a table named 'Numbers' with a column named 'Num' that contains integers. We want to calculate the factorial of each number in the 'Num' column and create a new column named 'Factorial' with the calculated values. To achieve this, we can use the following M code:


Source = Numbers,

#”Added Custom” = Table.AddColumn(Source, “Factorial”, each Number.Factorial([Num]))


#”Added Custom”

In the above M code, we first create a variable named 'Source' that refers to the 'Numbers' table. Then we add a custom column named 'Factorial' to the table using the 'Table.AddColumn' function. The 'each' keyword is used to apply the Number.Factorial function to each value in the 'Num' column. Finally, we return the updated table with the new 'Factorial' column.

The Number.Factorial function is a powerful tool in Power Query that allows us to calculate the factorial of a given number. This function is extensively used in data transformations and data cleansing activities in both Power Query and Power BI. By understanding the syntax, parameters, and usage of this function, we can leverage its power to solve complex data problems and gain insights from our data.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us


    Your Name (required)


    Email (required)


    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)