Value.Subtract

D

T

The M Code Behind the Power Query M function Value.Subtract

Understanding Value.Subtract

Before we dive into the code, let’s first understand what the Value.Subtract function does. The function takes two values as inputs and subtracts the second value from the first. For example, if we have two values A and B, the function will return A-B.

Here’s an example of how the function is used in Power Query:


= Value.Subtract(10,5)


In this example, the function will return 5, as 5 is subtracted from 10.

The M Code Behind Value.Subtract

Now let’s take a look at the M code behind this function. The Value.Subtract function is defined as follows:


(Value1 as any, Value2 as any) as any =>

Value1 - Value2


Let’s break this code down and understand what it does.

The first line defines the function and its input parameters. In this case, the function takes two input parameters, Value1 and Value2, which can be any data type.

The second line uses the “=>” operator to define what the function does. In this case, the function subtracts Value2 from Value1 and returns the result.

It’s worth noting that the Value.Subtract function is not limited to just numbers. It can be used to subtract any two values, regardless of their data type.

Using Value.Subtract in Power Query

Now that we understand the M code behind the Value.Subtract function, let’s take a look at how it can be used in Power Query.

In Power Query, you can use the Value.Subtract function in a number of different scenarios. For example, you might use it to calculate the difference between two dates, or to subtract a discount from a total price.

Here’s an example of how the function can be used to calculate the difference between two dates:


= Value.Subtract(#date(2022,12,31),#date(2022,1,1))


In this example, the function will return 364, as there are 364 days between December 31st and January 1st.

Here’s another example of how the function can be used to subtract a discount from a total price:


= Value.Subtract(100,20)


In this example, the function will return 80, as 20 is subtracted from 100 to give us the discounted price.

In summary, the Value.Subtract function is a powerful tool in Power Query that allows you to subtract one value from another. The M code behind the function is simple and easy to understand, making it a great starting point for those looking to create their own custom functions in Power Query. Whether you’re working with numbers, dates, or any other data type, the Value.Subtract function can help you to perform calculations and analysis with ease.

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)