Forum Discussion
DAX Help - Calculate sum if between two dates in another table
Hi, I have two fact tables; Spend and Contracts.
I am trying to make a forumla for 'Spend under Contract', but not getting the desired result.
SPEND
| Date | Supplier | Category | Amount |
| 1/1/20 | XYZ | A | $100 |
CONTRACTS
| Supplier | Category | Start | End | Status |
| XYZ | A | 1/1/19 | 31/12/19 | Active |
I need to SUM the Amount from SPEND, only if it occurs betweem the Start & End Dates from CONTRACTS, with the exception that if it occurs after the End Date, as long as the status is "Active" it is sill included.
My thoughts were something like this, but does not work;
Contracted Spend = CALCULATE( SUM(SPEND[Amount]) ,
FILTER( CONTRACTS, SPEND[Date] >= CONTRACTS[Start Date] && SPEND[Date] >= CONTRACTS[Start Date] || CONTRACTS[Status] = "Active" )
I will be filtering by Supplier & Category, which have their own joining tables, hence cannot use "ALL" functions.
I have tried variations with SUMX too, but my DAX is failing me...
Sample file here if it helps.
Thank you in advance.
1 Reply
- amitchandak
Super User
PBIX1234 , try like
Contracted Spend = Sumx( Contract, CALCULATE( SUM(SPEND[Amount]) ,
FILTER( SPEND, SPEND[Date] >= Min(CONTRACTS[Start Date]) && SPEND[Date] >= max(CONTRACTS[Start Date])
&& SPEND[Supplier] >= max(CONTRACTS[Supplier])
&& SPEND[category] >= max(CONTRACTS[category])
|| CONTRACTS[Status] = "Active" ))