Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
leejuhyeong
Frequent Visitor

To find the sum within the period when there are two date columns

Dear All,

 

There are three dates: the contract start date, the contract end date, and the transaction date
Sales quantity is linked to the transaction day.
But I want to know the Sales quantity only between the contract start date and the contract end date.

 

And there are two relationships.

1) Many to one(Single) : 'TABLE'[TRANSACTION DATE] : 'Date'[Date]

2) Many to one(Single) : 'TABLE'[contract start date] :'Date'[Date]

I made two Dax expressions.

 

1)  CASE1 =

VAR Start_Date = MIN('TABLE'[contract start date])
VAR End_Date = MAX('TABLE'[contract end date])

VAR Result =
CALCULATE(
    SUMX('TABLE','TABLE'[SALES_QTY]),
    USERELATIONSHIP('TABLE'[TRANSACTION DATE], 'Date'[Date]),    
    DATESBETWEEN('Date'[Date], Start_Date, End_Date)
)

RETURN Result

 

CASE 2 =
VAR Start_Date = MIN('TABLE'[contract start date])
VAR End_Date = MAX('TABLE'[contract end date])

VAR Result =
CALCULATE(
    SUMX(
        FILTER(
            'TABLE',
            'TABLE'[TRANSACTION DATE] >= Start_Date && 'TABLE'[TRANSACTION DATE] <= End_Date
        ),
        'TABLE'[SALES_QTY]
    ),
    USERELATIONSHIP('TABLE'[TRANSACTION DATE], 'Date'[Date])
)

RETURN Result

 


When these two expressions are used, each line has accurate data,

 

But the final sum is not equal to the sum of the measures. The final sum is equal to the total term sum, not the sum of the contract term.


What should I do to solve the problem?

3 REPLIES 3
123abc
Community Champion
Community Champion

The issue you're encountering is that the final sum doesn't match the sum of the measures for each line; instead, it equals the total term sum, not the sum of the contract term.

To ensure that your final sum matches the sum of the measures within the contract term, you need to adjust your DAX expressions. Here's how you can modify your DAX expressions to achieve the desired result:

 

Approach 1: Adjusting CASE 1 Expression

 

CASE1 =
VAR Start_Date = MIN('TABLE'[contract start date])
VAR End_Date = MAX('TABLE'[contract end date])

VAR Result =
CALCULATE(
SUMX(
FILTER(
'TABLE',
'TABLE'[TRANSACTION DATE] >= Start_Date && 'TABLE'[TRANSACTION DATE] <= End_Date
),
'TABLE'[SALES_QTY]
),
USERELATIONSHIP('TABLE'[TRANSACTION DATE], 'Date'[Date]),
DATESBETWEEN('Date'[Date], Start_Date, End_Date)
)

RETURN Result

 

Approach 2: Adjusting CASE 2 Expression

 

CASE2 =
VAR Start_Date = MIN('TABLE'[contract start date])
VAR End_Date = MAX('TABLE'[contract end date])

VAR Result =
CALCULATE(
SUMX(
FILTER(
'TABLE',
'TABLE'[TRANSACTION DATE] >= Start_Date && 'TABLE'[TRANSACTION DATE] <= End_Date
),
'TABLE'[SALES_QTY]
),
USERELATIONSHIP('TABLE'[TRANSACTION DATE], 'Date'[Date]),
DATESBETWEEN('Date'[Date], Start_Date, End_Date)
)

RETURN Result

 

Explanation:

Both of these approaches utilize the same logic for calculating the sum of sales quantities within the contract period. They adjust the context within which the sales quantity is calculated to ensure that it only considers transactions within the specified contract start and end dates.

By incorporating the DATESBETWEEN function within the CALCULATE function, you're constraining the calculation to the specified contract period, thus ensuring that the final sum reflects the sum of sales quantities within that period.

Ensure that your relationships between the tables ('TABLE' and 'Date') are correctly defined and that the data types of the date columns match for proper filtering and calculation. With these adjustments, your final sum should accurately reflect the sum of sales quantities within the contract term.

 

I tried as you said, but the problem was not solved.
I wonder if the relationship with the date table is a problem.

 

there are two relationships.

1) Many to one(Single) : 'TABLE'[TRANSACTION DATE] : 'Date'[Date]

2) Many to one(Single) : 'TABLE'[contract start date] :'Date'[Date]

Given your clarification about the relationships, it seems like you have two relationships between your 'TABLE' table and your 'Date' table:

  1. Many-to-one (Single) relationship between 'TABLE'[TRANSACTION DATE] and 'Date'[Date].
  2. Many-to-one (Single) relationship between 'TABLE'[contract start date] and 'Date'[Date].

The presence of multiple relationships between the same tables can sometimes cause unexpected results, especially when using CALCULATE and related functions.

Here are a few steps you can take to troubleshoot and potentially resolve the issue:

  1. Review Relationship Behavior: Make sure that the relationships between your tables are set up correctly and that they reflect the actual relationships in your data model. Confirm that the cross-filter direction is set appropriately for each relationship.

  2. Consider Filter Context: Understand how the filter context is being applied in your DAX calculations. Filters from both relationships may interact with each other, affecting the results.

  3. Evaluate Filter Context: Use DAX tools like DAX Studio to evaluate the filter context and see how it affects your measures. You can analyze the filters applied to each calculation to identify any discrepancies.

  4. Check for Ambiguity: Ensure that there is no ambiguity in your DAX expressions regarding which relationship to use. Explicitly specify the relationship using functions like USERELATIONSHIP or RELATED if necessary.

  5. Test with Simplified Data: Create a simplified dataset if possible and test your DAX expressions with it. This can help isolate the problem and identify any specific data issues causing discrepancies.

  6. Consider Model Changes: If the relationships are causing significant issues, consider revising your data model. This might involve restructuring relationships or creating additional calculated columns to clarify the relationship context.

If you've tried these steps and are still experiencing issues, it might be helpful to provide more specific details about your data model and DAX expressions. This could include sample data, the exact calculations you're trying to perform, and any specific errors or discrepancies you're encountering. With more information, it's easier to diagnose the problem and provide targeted solutions.

 
 
 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

Feb2025 NL Carousel

Fabric Community Update - February 2025

Find out what's new and trending in the Fabric community.