0
votes

I have the following PowerBI DAX Measure. It has to be this complex for a number of reasons due to the structure of the data. The measure works fine, and it returns me the number of active contracts for each customer. This displays nicely on a visual grid and gives a total of all the active contracts for all the customers.

ActiveContracts = CALCULATE(COUNTROWS(RELATEDTABLE(Contracts)), 
FILTER(Contracts,
        (
        Contracts[StartDate] >= CALCULATE(MIN(DateRange[Date]), ALLEXCEPT(DateRange, DateRange[Date])) && 
        Contracts[StartDate] <= CALCULATE(MAX(DateRange[Date]), ALLEXCEPT(DateRange, DateRange[Date]))
        )       
        ||
        (
        Contracts[End Date] >= CALCULATE(MIN(DateRange[Date]), ALLEXCEPT(DateRange, DateRange[Date])) && 
        Contracts[End Date] <= CALCULATE(MAX(DateRange[Date]), ALLEXCEPT(DateRange, DateRange[Date]))
       )
       ||
       (
       Contracts[StartDate] <= CALCULATE(MIN(DateRange[Date]), ALLEXCEPT(DateRange, DateRange[Date])) && 
       Contracts[End Date] >= CALCULATE(MAX(DateRange[Date]), ALLEXCEPT(DateRange, DateRange[Date]))
       )
   )     
)

However, I only need the first result, or even more simply, just the fact that there is a result (i.e. an active contract for a customer).

I have tried a number of things and the closest I can get is another measure on top of the one above:

HasContract = CALCULATE(IF(ISBLANK([CCActiveContract]), 0, 1))

This then works on the row level, but when I total the column with this new measure, it gives a total of 1, whereas I want a count of all customers with an active contract?

What am I missing?

1

1 Answers

1
votes

Solved by Tom Martens over on the PowerBI forum (link) using:  

ContractPerCustomer = SUMX(Customer, IF([ActiveContracts] > 0, 1, 0))