Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Conditional Sum between 2 dates between two tables with 2 conditions

Hi All,

 

New user to PBI and DAX here, I'm trying to evaluate promotion programs based on daily sales data and promotion information, with 2 simplified tables below

Table 1: Daily Sales Data

Customer Invoice Date
(dd/mm/yy format)
Sales Value
A01/01/202110
A15/01/202115
B20/01/202120
A25/01/202140
B02/02/202130

 

Table 2: Promotion Information

Customer Promo StartPromo End
A01/01/202115/01/2021
A20/01/202126/01/2021
B01/02/202115/02/2021

 

For expected results, I want to create a calculated column in Table 2 summing sales value from Table 1 based on 2 conditions: (1) Invoice Date between Promo Start and Promo End Date & (2) Conditional sum based on customer (A/ B). It will look like below table, and the conditions must be variable (cannot be hard-coding) as well (e.g not fixed as "A" and "B" in the formula for customer and specific dates for the dates), as the real datasets are more complex with lots of customer and promotion timings.

 

Expected Result

Customer Promo StartPromo EndPromo Sales
(calculated column)
A01/01/202115/01/202125
(=10+15)
A20/01/202126/01/202140
(=40)
B01/02/202115/02/202130
(=30)

 

On relationships between 2 tables, I haven't set yet as it's a many-to-many relationship (a customer can have multiple promotions and appear in many invoices). The closest I've got to the result is using the DATES BETWEEN formula, however, it still with some errors and cannot include the customer condition.

 

I'm new to DAX and have been struggling with this for more than 9 hours, appreciate any help!

 

Many thanks, guys!