Percentage.From

D

T

The M Code Behind the Power Query M function Percentage.From

Understanding the Percentage.From Function

The Percentage.From function is used to calculate the percentage of a number relative to another number. For example, if you want to know what percentage of sales came from a particular region, you can use the Percentage.From function.

The syntax for the Percentage.From function is as follows:


Percentage.From(numerator as any, denominator as any) as any


The numerator is the number you want to calculate the percentage for, and the denominator is the total number you are comparing it to. The function returns the percentage as a decimal.

The M Code Behind the Percentage.From Function

Now that we understand the basics of the Percentage.From function, let's take a look at the M code behind it.

The M code for the Percentage.From function is as follows:


let

percentage = (numerator, denominator) =>

if denominator = 0 then null else numerator / denominator,

result = percentage

in

result


Let's break this down line by line.

The first line starts the M code with the `let` keyword. This is used to define variables.

The next line defines the `percentage` variable as a function. The function takes in two parameters - `numerator` and `denominator`.

The next line is an `if` statement that checks if the denominator is equal to 0. If it is, the function returns null, otherwise it calculates the percentage by dividing the numerator by the denominator.

The fourth line defines the `result` variable as the `percentage` function.

The final line of the M code returns the `result` variable.

Using the Percentage.From Function

Now that we understand the M code behind the Percentage.From function, let's see how we can use it in Power Query.

To use the Percentage.From function, you need to open Power Query and create a new query. From there, you can go to the "Add Column" tab and select "Custom Column". In the formula bar, you can enter the following formula:


Percentage.From([numerator], [denominator])


In this formula, `[numerator]` and `[denominator]` are the names of the columns you want to calculate the percentage for. Once you hit enter, Power Query will create a new column with the percentage values.

In conclusion, the Percentage.From function is a powerful tool in Power Query for calculating percentages. By understanding the M code behind this function, you can better understand how it works and how to use it in your own projects.

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

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)