Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
8 years ago
Solved

PowerBI DAX - Calculate, CountRows, RelatedTable, get TOP 1

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?

 

Thanks

  • Anonymous's avatar
    Anonymous
    8 years ago

    Thanks SuperTom, 

     

    I am on my way using similar: 

     

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

     

    Cheers

2 Replies

  • Hey,

     

    without sample data it's a litte difficult, but I would give this a try by creating a new measure

    SUMX(
    VALUES('tableWithCustomer'[Customer],
    IF([AcitveContracts] > 0
    ,1
    ,BLANK()
    )
    )

    Hopefully this gets you started

     

    Regards

    Tom

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks SuperTom, 

       

      I am on my way using similar: 

       

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

       

      Cheers