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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

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
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

Check out the June 2024 Power BI update to learn about new features.

PBI_Carousel_NL_June

Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.