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
ttseng
Helper III
Helper III

Measure to return the most recent known value

Hi,

 

I'm struggling to get a measure that returns the most recent known value. 

 

I have a dataset of Schools that take enrollment. Sometimes the enrollment is taken and sometimes it is not. I want to be able to return the the most recent known value of students enrolled for each school. If enrollment was never taken then return a null. 

 

Here's the sample of the raw data. 

 

School_IDDateTake_EnrollmentStudents
10153/19/2020Nonull
10163/11/2020Yes288
10172/12/2020Nonull
10173/11/2020Yes423
10173/17/2020Nonull
10183/18/2020Yes410
10193/17/2020Nonull
10203/11/2020Yes103
10212/12/2020Nonull
10214/14/2020Yes262
10222/14/2020Nonull
10224/14/2020Yes241
10232/12/2020Nonull
10234/14/2020Nonull

 

Here is what I'd like to be the final output:

 

School_IDStudents
1015null
1016288
1017423
1018410
1019null
1020103
1021262
1022241
1023null

 

note:

What is causing me trouble is when I encounter something like school_id 1017 where the latest value is null but I actually want it to return 423 when enrollment was last taken.

 

Thanks!

1 ACCEPTED SOLUTION
edhans
Super User
Super User

This will work.

 

 

Enrollment =
VAR LatestDate =
    MAX( 'Table'[Date] )
VAR EnrollmentTakenTable =
    FILTER(
        'Table',
        'Table'[Take_Enrollment] = "Yes"
    )
VAR EnrollmentTaken =
    COUNTROWS( EnrollmentTakenTable ) >= 1
VAR LatestEnrollmentTakenDate =
    MAXX(
        EnrollmentTakenTable,
        'Table'[Date]
    )
VAR LatestValue =
    IF(
        EnrollmentTaken
            = TRUE(),
        MAXX(
            EnrollmentTakenTable,
            'Table'[Students]
        ),
        MAXX(
            FILTER(
                'Table',
                'Table'[Date] = LatestDate
            ),
            'Table'[Students]
        )
    )
RETURN
    LatestValue

 

 

It returns this table.

2020-06-22 13_57_24-Untitled - Power BI Desktop.png

In order to get the blanks to show up though you need to select the School ID in the visual fields and tell it to show items with no data, otherwise, 1015, 1019, and 1023 will not show up.

2020-06-22 13_58_03-Untitled - Power BI Desktop.png

 

Edit: @Tahreem24's solution is much simpler, but I don't think it will work if your database has multiple "Yes" enrollments taken for the same school ID. I assumed you would actually have school 1017, for example, in there many times and you only wanted the latest enrollment taken if any ever had been, or null if it never had been. If your records are not going to work like that but will be representative of your sample data, you should definitely use @Tahreem24's solution.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

5 REPLIES 5
edhans
Super User
Super User

This will work.

 

 

Enrollment =
VAR LatestDate =
    MAX( 'Table'[Date] )
VAR EnrollmentTakenTable =
    FILTER(
        'Table',
        'Table'[Take_Enrollment] = "Yes"
    )
VAR EnrollmentTaken =
    COUNTROWS( EnrollmentTakenTable ) >= 1
VAR LatestEnrollmentTakenDate =
    MAXX(
        EnrollmentTakenTable,
        'Table'[Date]
    )
VAR LatestValue =
    IF(
        EnrollmentTaken
            = TRUE(),
        MAXX(
            EnrollmentTakenTable,
            'Table'[Students]
        ),
        MAXX(
            FILTER(
                'Table',
                'Table'[Date] = LatestDate
            ),
            'Table'[Students]
        )
    )
RETURN
    LatestValue

 

 

It returns this table.

2020-06-22 13_57_24-Untitled - Power BI Desktop.png

In order to get the blanks to show up though you need to select the School ID in the visual fields and tell it to show items with no data, otherwise, 1015, 1019, and 1023 will not show up.

2020-06-22 13_58_03-Untitled - Power BI Desktop.png

 

Edit: @Tahreem24's solution is much simpler, but I don't think it will work if your database has multiple "Yes" enrollments taken for the same school ID. I assumed you would actually have school 1017, for example, in there many times and you only wanted the latest enrollment taken if any ever had been, or null if it never had been. If your records are not going to work like that but will be representative of your sample data, you should definitely use @Tahreem24's solution.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Thanks @edhans for the quick work. The solution worked perfectly!

Great @ttseng - glad it worked for you.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Hi @edhans -

 

Thanks again for the solution. I have a new use case based on this need for most recent known value and am wodnering if you could help.

 

I have targets and actuals and targets are monthly based on number of students registered multiplied by number of school days that month. I have a table with the schools days per month and can query that no problem. However, I'm having trouble trying to get the targets per month per school based on the dynamic student enrollment as some months they are entered and other not. 

 

Here's the same dataset.

 

School_IDDateTake_EnrollmentStudents
10153/19/2020Nonull
10163/11/2020Yes288
10172/12/2020Nonull
10173/11/2020Yes423
10173/17/2020Nonull
10183/18/2020Yes410
10193/17/2020Nonull
10203/11/2020Yes103
10212/12/2020Nonull
10214/14/2020Yes262
10222/14/2020Nonull
10224/14/2020Yes241
10232/12/2020Nonull
10234/14/2020Nonull

 

I would like to return a table with unique schools and enrollment for each month based on the most recent known value of enrollment. This is what i'm hoping to achieve as a seperate table to track my targets.

 

School_IDFebruaryMarchApril
1015nullnullnull
1016null288288
1017null323423
1018null410410
1019nullnullnull
1020null103103
1021nullnull262
1022nullnull241
1023nullnullnull

 

I would like each month to have the most recent known value of student enrollment unless data was entered that month. 

 

for example: 

School 1016 only has data from March and it will use that value and fill April, June, July and so forth until new enrollment is entered that would take its place for future months

School 1017 has data for both March and April and will reutrn the corresponding numbers. If enrollment data is not entered in June it would use April's enrollment.

 

The end result would then take enrollment for each month that's selected in a filter and multiply by schools days for each respective months and added together to get targets for that date range. 

 

Is this this right approach in thinking about these targets? If so, how would one go about achieving this?

 

This data can be bound through December 2020 if that's helpful.

 

Thanks again!

 

Tahreem24
Super User
Super User

Try something like below:

measure= If(Table[TakeEnrollment]="Yes", Sum(Table[Student]), Sum(Table[Student]))

 

Don't forget to give thumbs up and accept this as a solution if it helped you!!!

Please take a quick glance at newly created dashboards : Restaurant Management Dashboard , HR Analytics Report , Hotel Management Report, Sales Analysis Report , Fortune 500 Companies Analysis , Revenue Tracking Dashboard

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.