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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

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

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Thanks SuperTom, 

 

I am on my way using similar: 

 

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

 

Cheers

View solution in original post

2 REPLIES 2
TomMartens
Super User
Super User

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



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
Anonymous
Not applicable

Thanks SuperTom, 

 

I am on my way using similar: 

 

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

 

Cheers

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.