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
sperry
Resolver I
Resolver I

Generate MaxDate within a group and assess a condition

I have a problem I thought was easy to solve but having real issues in PowerBI.

 

I have a table of which contains many measures for instances of care. (See sample table below).

 

I want to answer the simple question of how many are active in the service now. I can ascertain this by taking the MaxDate of the MesDate for each EncryptedNHI and if the Discharged column is null then the individual is active in the service. I know what the sql would kinda look like but I cannot create a measure or even a table in query editor that will generate what I need.

 

Any suggestions?

 

MESDATEENCRYPTEDNHIDISCHARGED29
2/07/2018 15:20A9999null
3/07/2018 10:16A27775null
3/07/2018 11:10A15554null
4/07/2018 10:24A15554null
5/07/2018 11:20A14443null
9/07/2018 10:21A7777null
9/07/2018 12:20A21109null
10/07/2018 15:01A33330null
10/07/2018 15:28A36663null
11/07/2018 9:08A12221null
11/07/2018 8:45A33330null
11/07/2018 9:04A12221null
27/06/2018 9:04A12221null
11/07/2018 9:59A5555null
11/07/2018 16:24A25553null
16/07/2018 11:18A28886null
16/07/2018 15:02A13332null
16/07/2018 16:28A16665null
19/06/2018 16:29A16665null
12/07/2018 16:32A16665null
16/07/2018 16:36A16665Ticked
17/07/2018 11:06A12221null
17/07/2018 11:46A18887null
17/07/2018 14:34A4444null
9/07/2018 9:08A14443null
17/07/2018 9:15A14443null
18/07/2018 10:24A29997null
18/07/2018 10:32A26664null
19/07/2018 10:50A23331null
19/07/2018 11:30A14443null
19/07/2018 11:33A25553null
19/07/2018 15:30A31108null
19/07/2018 16:00A9999null
23/07/2018 11:33A19998null
1 ACCEPTED SOLUTION

My bad, I missed a paren, that's the danger of doing this stuff on the fly versus testing it!

 

IsActive =
VAR __maxDate = MAX(MMNOutcome[MESDATE])
VAR __status = MAXX(FILTER(MMNOutcome,MMNOutcome[MESDATE]=__maxDate),MMNOutcome[DISCHARGED29])
RETURN
IF(ISBLANK(_status),TRUE,FALSE)

Hmmm, if you want a count, then you will need to do something like:

 

# Active = 
VAR __tmpTable = ADDCOLUMNS(MMNOutcome,"__IsLatest",IF([MESDATE]=MAXX(FILTER(ALL(MMNOutcome),[ENCRYPTEDNHI]=EARLIER(MMNOutcome[ENCRYPTEDNHI])),[MESDATE]),TRUE,FALSE))
RETURN COUNTROWS(FILTER(__tmpTable,[__IsLatest] && MMNOutcome[DISCHARGED29]="null"))

Not sure about your data. When I entered it via Enter Data query, I got "null" in for the DISCHARGED29. If your data is actual nulls, as in nothing then you will have to change the last condition in the RETURN statement to something like ISBLANK(MMNOutcome[DISCHARGED29]) instead of MMNOutcome[DISCHARGED29]="null"

 

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

4 REPLIES 4
Greg_Deckler
Super User
Super User

Put ENCRYPTEDNHI in a table visualization.

 

Create this measure:

 

IsActive =
VAR __maxDate = MAX([MESDATE])
VAR __status = MAXX(FILTER('Table',[MESDATE]=__maxDate),[DISCHARGE29]
RETURN
IF(ISBLANK(__status),TRUE,FALSE)

Put that in your table visual as well.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Wow thatnks for getting back so quickly..

 

A couple of things;

- I get the following error message when generating a measure

IsActive =
VAR __maxDate = MAX(MMNOutcome[MESDATE])
VAR __status = MAXX(FILTER(MMNOutcome,MMNOutcome[MESDATE]=__maxDate),MMNOutcome[DISCHARGED29]
RETURN
IF(ISBLANK(_status),TRUE,FALSE)

 

The syntax for 'RETURN' is incorrect. (DAX(VAR __maxDate = MAX(MMNOutcome[MESDATE])VAR __status = MAXX(FILTER(MMNOutcome,MMNOutcome[MESDATE]=__maxDate),MMNOutcome[DISCHARGED29]RETURNIF(ISBLANK(__status),TRUE,FALSE))).

 

- The second is that I would like to generate a measure that just counts the active status. Or maybe do I need to generate a table in the query editor then generate a measure over that?

My bad, I missed a paren, that's the danger of doing this stuff on the fly versus testing it!

 

IsActive =
VAR __maxDate = MAX(MMNOutcome[MESDATE])
VAR __status = MAXX(FILTER(MMNOutcome,MMNOutcome[MESDATE]=__maxDate),MMNOutcome[DISCHARGED29])
RETURN
IF(ISBLANK(_status),TRUE,FALSE)

Hmmm, if you want a count, then you will need to do something like:

 

# Active = 
VAR __tmpTable = ADDCOLUMNS(MMNOutcome,"__IsLatest",IF([MESDATE]=MAXX(FILTER(ALL(MMNOutcome),[ENCRYPTEDNHI]=EARLIER(MMNOutcome[ENCRYPTEDNHI])),[MESDATE]),TRUE,FALSE))
RETURN COUNTROWS(FILTER(__tmpTable,[__IsLatest] && MMNOutcome[DISCHARGED29]="null"))

Not sure about your data. When I entered it via Enter Data query, I got "null" in for the DISCHARGED29. If your data is actual nulls, as in nothing then you will have to change the last condition in the RETURN statement to something like ISBLANK(MMNOutcome[DISCHARGED29]) instead of MMNOutcome[DISCHARGED29]="null"

 

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Perfect! This will come in super handy for many things..thanks so much for your support

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.