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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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]))))

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
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors