Smart Systems for Home

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

Power Query Value.Subtract Function: Usage and 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 Value.Subtract function in Power Query (M language) is used to calculate the difference between two numeric values by subtracting the second number from the first. To use it, you simply write Value.Subtract(number1, number2) within a custom column formula or a let-statement. While most users prefer the standard minus sign (-) for simplicity, Value.Subtract is the formal function call that ensures the operation is handled as a numeric subtraction within the M engine.

What You'll Need

  • A data source loaded into Power BI Desktop or Excel Power Query Editor.
  • Two columns containing numeric data types (Decimal Number, Whole Number, or Fixed Decimal Number).
  • Access to the "Add Column" tab to create a Custom Column.

Step-by-Step

  1. Open the Power Query Editor by selecting Data > Get Data > Launch Power Query Editor in Excel, or by clicking Transform Data in Power BI.
  2. Navigate to the Add Column tab on the top ribbon. Labels and menu positions may differ slightly depending on your version of Excel or Power BI.
  3. Select Custom Column from the ribbon. This opens a dialog box where you can define your new calculation.
  4. In the New column name field, enter a descriptive name, such as "Difference" or "Net Value".
  5. In the Custom column formula box, enter the function using the following syntax: Value.Subtract([ColumnA], [ColumnB]). Replace [ColumnA] and [ColumnB] by double-clicking the actual column names from the list provided in the dialog box.
  6. Click OK to apply the formula. Power Query will generate a new column containing the results of the subtraction for every row in the table.
  7. Check the data type of the new column. If it shows as "Any", click the data type icon (the small square next to the column header) and select Decimal Number or Whole Number to ensure subsequent calculations work correctly.

Common Mistakes

One of the most frequent errors is attempting to use Value.Subtract on columns that are formatted as Text. If your numbers were imported as text, the function will return an error for every row. You must change the column type to a numeric format before applying the function.

Another common mistake is reversing the order of the arguments. In Value.Subtract(x, y), the value y is subtracted from x. If you intend to find the difference between a total and a cost, the total must be the first argument. Reversing them will result in negative numbers where you expected positive ones.

Users also sometimes confuse the M language case sensitivity. Power Query is strictly case-sensitive. Writing value.subtract or Value.subtract with a lowercase 's' will result in a "The name 'value.subtract' wasn't recognized" error. The function must be written exactly as Value.Subtract.

Finally, some users try to use this function to subtract dates. While Value.Subtract is designed for numeric values, subtracting dates in Power Query is typically handled by subtracting one date from another using the minus sign, which returns a Duration type. Using Value.Subtract on dates may lead to unexpected type errors or incorrect results depending on the underlying numeric representation of the date.

Tips

If you are performing a simple subtraction across thousands of rows, using the operator - (e.g., [ColumnA] - [ColumnB]) is functionally identical to Value.Subtract and is generally easier to read and write. Use the formal function when you are building complex dynamic functions or when you want your code to strictly follow the M library naming conventions.

To handle null values, wrap your columns in a coalesce logic or use an if statement. For example, Value.Subtract(if [ColumnA] = null then 0 else [ColumnA], if [ColumnB] = null then 0 else [ColumnB]). This prevents the entire result from becoming null if just one of the two values is missing.

For better performance in very large datasets, ensure that the columns you are subtracting are already typed as numbers before the subtraction step. This prevents Power Query from having to perform an implicit type conversion for every single row during the subtraction process.

If you need to subtract a constant value from a column (e.g., subtracting a standard tax rate of 5 from every row), you can simply write Value.Subtract([ColumnA], 5). You do not need to create a separate column for the constant value.

Troubleshooting

If the function returns an Error in the cell, the first step is to click the whitespace next to the word "Error" in the cell. This will reveal a detailed error message at the bottom of the screen. If it says "We cannot apply operator - to types Number and Text," you have a data type mismatch. Go back to the previous step in the Applied Steps pane and ensure both columns are set to a numeric type.

If the result is null, check your source data. In Power Query, any numeric operation involving a null value results in null. To fix this, right-click the column and select Replace Values to replace null with 0 before performing the subtraction.

If the formula is not being accepted in the Custom Column dialog, check for trailing commas or missing parentheses. Ensure the formula starts with the equals sign (=) if you are editing the formula bar directly, though the Custom Column dialog handles the equals sign for you.

If the calculation is taking an unusually long time to load, check if you have "Buffer" issues. For extremely large tables, try using Table.AddColumn with a transformation that minimizes the number of times the table is scanned. However, for most users, the standard Value.Subtract approach is efficient enough.

Frequently Asked Questions

Can I use Value.Subtract for dates?

It is not recommended. For dates, use the minus operator (Date1 - Date2), which returns a Duration object. Value.Subtract is intended for numeric types.

What is the difference between Value.Subtract and the minus (-) sign?

There is no functional difference in the result. The minus sign is a shorthand operator, while Value.Subtract is the formal function. The operator is more common and easier to read.

Why does my subtraction result in a null value?

This happens because one or both of the input cells contain a null. In M, Number - null = null. You must replace nulls with zeros to get a numeric result.

Does Value.Subtract work with currency types?

Yes, it works with the Fixed Decimal Number type, which is the standard for currency in Power Query, as well as standard Decimal and Whole numbers.

Can I subtract more than two columns using this function?

The function only accepts two arguments. To subtract multiple columns, you must nest the functions, such as Value.Subtract(Value.Subtract([Col1], [Col2]), [Col3]), or simply use [Col1] - [Col2] - [Col3].

Is Value.Subtract case-sensitive?

Yes. You must capitalize the 'V' in Value and the 'S' in Subtract. Using value.subtract will result in an error.