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
joulethrex
Frequent Visitor

Need help creating measure to count distinct status by groupkey and maximum date

I am trying to write a couple DAX expressions to count the number of [Status] by [GroupKey] and maximum [TxDate]. For example, the end result should be counting the below records (in red) to show that [CountOfFailures] = 3 and [CountOfSuccess] = 2. 

 

Any help would be greatly appreciated.

 

IdentityTxDateStatusGroupKey
000019/29/2020 0:00FailureABC
000029/29/2020 0:00FailureABC
000039/29/2020 0:00FailureABC
000049/30/2020 0:00SuccessABC
000059/25/2020 0:00FailureXYZ
000069/27/2020 0:00FailureXYZ
000079/28/2020 0:00FailureXYZ
000089/29/2020 10:00FailureXYZ
000099/29/2020 13:00FailureXYZ
000109/29/2020 15:00FailureXYZ
000119/26/2020 0:00SuccessEFG
000129/27/2020 0:00SuccessEFG
000139/28/2020 0:00SuccessEFG
000149/28/2020 0:00FailureLMN
000159/29/2020 0:00FailureLMN
000169/30/2020 22:00FailureLMN
000159/26/2020 0:00FailureQRS
000189/30/2020 00:00FailureQRS

 

Currently my DAX expression counts all the failures but my requirement is to group by the max date and GroupKey. 

 

NumberOfFailures =

VAR noOfFailureTransactions=

CALCULATE(COUNTROWS(MyTable), FILTER(MyTable, MyTable[Status] = "Failure"))
RETURN

IF ( ISBLANK( noOfFailureTransactions), 0, noOfFailureTransactions
)
1 ACCEPTED SOLUTION
Anonymous
Not applicable

[# Failures] =
CALCULATE(
    COUNTROWS( T ),
    TREATAS(
        ADDCOLUMNS(
            DISTINCT( T[GroupKey] ),
            "@MaxDate",
                calculate( max( T[TxDate]) )
        ),
        T[GroupKey],
        T[TxDate]
    ),
    // If you want "Sucess", change
    // "Failure" to "Success" in the
    // filter.
    KEEPFILTERS( T[Status] = "Failure" )
)

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

[# Failures] =
CALCULATE(
    COUNTROWS( T ),
    TREATAS(
        ADDCOLUMNS(
            DISTINCT( T[GroupKey] ),
            "@MaxDate",
                calculate( max( T[TxDate]) )
        ),
        T[GroupKey],
        T[TxDate]
    ),
    // If you want "Sucess", change
    // "Failure" to "Success" in the
    // filter.
    KEEPFILTERS( T[Status] = "Failure" )
)
amitchandak
Super User
Super User

@joulethrex , Please find the file attached after signature.

A measure like

Measure = CALCULATE(COUNT('Table'[Identity]) ,FILTER(('Table'),'Table'[Identity] =CALCULATE(max('Table'[Identity]),ALLEXCEPT('Table','Table'[GroupKey]))))
Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hello @amitchandak 

 

Thanks for responding! I should have clarified that the max identity field isn't always going to be the same record as the one with the most recent TxDate. In fact, the [Identity] column should actually be ignored. I wasn't able to revise the sample because of some HTML issues but essentially the Identity column is not reliable in this scenario.

 

[CountOfOutstandingFailures] = 3

 

[CountOfOutstandingSuccess] = 2 

 

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.