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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Counting rows, multiples criteria - CALCULATE, FILTER & MAXA, FILTER

Hey,

 

I am new to Power BI and currently having some troubles with the DAX function CALCULATE(COUNTROWS;

 

So i have a table of data with three columns, ID, Status & consent date.

 

Consents
IDStatusConsent Date
1Active22/01/2019
1Active28/01/2019
1Expired28/12/2018
2Revoked09/01/2019
2Revoked06/03/2019
3Active26/03/2019
4Expired08/02/2019
4Expired17/12/2018
5Active18/03/2019
5Active25/02/2019
5Revoked13/12/2018
5Expired24/03/2019
5Active06/03/2019
6Revoked12/02/2019
6Expired08/01/2019
6Revoked29/03/2019

 

What I need to do is create a table which counts the number of statuses for each unique ID like below:

 

Status
IDFinal StatusRevocationExpiredDateActiveRevokedExpired
1Active 201
2Revoked06/03/2019020
3Active 100
4Expired08/02/2019002
5Active 311
6Revoked29/03/2019021

 

Logic for this table is as follows:

RevocationExpiredDate: if Active > 0,"", if not then look up the latest consent date from 'Consent'[Consent Date]

Final Status: if Active > 0, "Active", if not then look up [Status] using [RevocationExpiredDate] & [ID] as criteria

Active: Count "Active" statuses for each ID

 

I have tried the below which should work for the status counts;

Active =
CALCULATE (
    COUNTROWS ( 'Consents' ),
    FILTER (
        'Consents',
        FIND ( "Active", 'Consents'[Status],, 0 )
            && FIND ( 'Status'[ID], 'Consents'[ID],, 0 )
    )
)

but due to the size of data (200,000+rows) the formula never finishes loading, so is not a viable option

WORKING.JPG

 

I also need a column to display the latest revocation/expiration date, where i would normally use a nested MAXIF excel formula, from some reading it would appear i need a MAXA nested with FILTER logic?

 

**Excel format

=IF([@Active]>0,"",
MAX(
MAXIFS(CONSENT[Consent Date],CONSENT[ID],Status[@ID],CONSENT[STATUS],"Revoked"),
MAXIFS(CONSENT[Consent Date],CONSENT[ID],Status[@ID],CONSENT[STATUS],"Expired")
)
)

^basically if ID has an active consent status, blank/null, if not find the latest date matching to that ID.

 

 

Finally i would need to display the Final Status which would use the following excel logic:

 

**Excel Format

=IF([@Active]>0,"Active",
INDEX('CONSENT'[STATUS],
MATCH(1,INDEX(('STATUS'[@ID]='CONSENT'[ID])*([@RevocationExpiredDate]='CONSEN'T[Consent Date]),0,1),0)))

^basically if ID has an active consent status, "Active", if not return the status matching the ID and latest revocation/expiration date.

 

I know this is a lot of requests in one, but worth asking the community.

 

Thanks

 

Sam

 

 

1 ACCEPTED SOLUTION
v-cherch-msft
Employee
Employee

Hi @Anonymous 

You may refer below measures:

Active = CALCULATE(COUNTROWS(Consents),Consents[Status]="Active")+0
RevocationExpiredDate = IF([Active]=0,CALCULATE(MAX(Consents[Consent Date])))
Final Status =
IF (
    [Active] > 0,
    "Active",
    CALCULATE (
        MAX ( Consents[Status] ),
        FILTER ( Consents, Consents[Consent Date] = [RevocationExpiredDate] )
    )
)

Regards,

Community Support Team _ Cherie Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

You may refer below measures:

Active = CALCULATE(COUNTROWS(Consents),Consents[Status]="Active")+0
RevocationExpiredDate = IF([Active]=0,CALCULATE(MAX(Consents[Consent Date])))
Final Status =
IF (
    [Active] > 0,
    "Active",
    CALCULATE (
        MAX ( Consents[Status] ),
        FILTER ( Consents, Consents[Consent Date] = [RevocationExpiredDate] )
    )
)
Anonymous
Not applicable

@v-cherch-msft 

 

How would i go about changing the active measure to return just a 1 if positive, instead of the count? 

Active = CALCULATE(COUNTROWS(Consents),Consents[Status]="Active")+0

I have tried logic against the final status measure:

Active = IF([Final Status]="active",1,0)

however this figure is not aggregated in my tables:

Aggreagated count.JPG

 

Thanks

 

Sam

v-cherch-msft
Employee
Employee

Hi @Anonymous 

You may refer below measures:

Active = CALCULATE(COUNTROWS(Consents),Consents[Status]="Active")+0
RevocationExpiredDate = IF([Active]=0,CALCULATE(MAX(Consents[Consent Date])))
Final Status =
IF (
    [Active] > 0,
    "Active",
    CALCULATE (
        MAX ( Consents[Status] ),
        FILTER ( Consents, Consents[Consent Date] = [RevocationExpiredDate] )
    )
)

Regards,

Community Support Team _ Cherie Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Hey @v-cherch-msft ,

 

Thanks for replying, although i need the stats returned to be ID specific. currently this counts all Active status in the entire table, same goes for the RevocationExpiredDate.

 

thanks

 

sam

 

Hi @Anonymous 

If it is not your case,could you explain more about your expected output?Please use the sample data as the example.Attached the file for your reference.

1.png

Regards,

 

Community Support Team _ Cherie Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Wow, im an idiot, i was creating a table in the data tab. just discovered measures. Your answer has solved all my problems.

 

thanks!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.