Code Generator | DAX

VALUE_DIFFERENCE

Calculates the difference between two values present in different tables that both have a relationship with another table.


Empty image or helper icon

Prompt

calculate the difference between two values, present in two different tables which are both in a relationship with another table

Answer

/*
Function: VALUE_DIFFERENCE
Purpose: This function calculates the difference between two values present in different tables that both have a relationship with another table.
Parameters: 
- @table1_value: The value from the first table.
- @table2_value: The value from the second table.
Return: The difference between @table1_value and @table2_value.
Exceptions: This function will not work if there's not a relationship between the four tables involved.
*/

VALUE_DIFFERENCE:=
CALCULATE (
    VAR Table1_Total = SUM ( Table1[@table1_value] )
    VAR Table2_Total = SUM ( Table2[@table2_value] )
RETURN
    Table1_Total - Table2_Total,
    RELATECOLUMN (
        'CommonTable',
        RELATED ( Table1[@table1_value] ),
        RELATED ( Table2[@table2_value] )
    )
)

This code will return the difference between the sums of the two designated columns (@table1_value and @table2_value) from Table1 and Table2 respectively. It assumes that there's a CommonTable in the data model that is in a relationship with both Table1 and Table2.

The RELATECOLUMN function is used to create a new column in the CommonTable containing the corresponding values from the designated columns in Table1 and Table2.

Code Usage Example:

Let's say you have three tables: Sales, Budget and Product. The Sales and Budget tables both have a 'ProductId' column, and record 'Revenue' and 'BudgetAmount' respectively. Both are in a relationship with the Product table via the 'ProductId'. The VALUE_DIFFERENCE could be used as follows:

Revenue_Budget_Difference:= VALUE_DIFFERENCE('Sales'[Revenue], 'Budget'[BudgetAmount])

This will return the difference between actual Revenue and BudgetAmount for each product. Please note that this code should be used directly in measures.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

The "VALUE_DIFFERENCE" function calculates the difference between two values from different tables that are related to a common table. This function is useful when you need to compare and analyze the difference between two related values. It assumes that there is a common table in the data model that is connected to both tables. The function takes two parameters: @table1_value representing the value from the first table, and @table2_value representing the value from the second table. This function works by summing the values from each table and subtracting them to find the difference. It uses the RELATECOLUMN function to create a new column in the common table that contains the corresponding values from the designated columns in the first and second tables. To use the VALUE_DIFFERENCE function, you need to provide the columns representing the values you want to compare, and apply it within a measure.

EXAMPLE USAGE:

Revenue_Budget_Difference:= VALUE_DIFFERENCE('Sales'[Revenue], 'Budget'[BudgetAmount])

This example calculates the difference between the actual revenue in the 'Sales' table and the budgeted amount in the 'Budget' table for each product. It assumes that both tables are related to the 'Product' table via the 'ProductId' column. The result will be a measure that shows the variance between the revenue and the budget amount for each product.