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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

Most recent time value for specific ID and metric, formatted hh:mm:ss

Hi!

 

Been struggling to get this to work...

 

I have a database of physical fitness tests done on random days over the years for a large number of people. For each person, I want to be return the most recent score for each test on a dashboard. Several of the tests are time based, such as a 3 mile run time, which is extra tricky in PBI. Ultimately, I want to be able to determine overall averages for each test, for each person, and compared to groups based on other variables/demographics, as well as show their most recent test score. Some tests for the same person were done on the same day and, of course, some people have blanks or zeros for scores, adding some complexity.

 

I've tried using group by, max, and other filters, creating measures and columns, but can't get it right. 

 

Example data below...

 

IDGENDERTEST DATE3 MILE (mm:ss)PUSHUPS (reps)
1

M

1/15/22 115
2F2/2/2211:01 
3M1/4/229:10 
1M10/12/2110:15 
2F9/8/2111:30 
3M3/15/219:20 
1M1/15/2210:30 
2F2/2/22 105
3M1/1/22 100
1M

7/10/21

 95
2F8/3/21 113
3M11/8/21 102

 

I'd like to have a column added for each test returning the most recent score and then be able to create measures determining the averages, formatted as appropriate.

 

Thank you!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous ,

Since you have multiple testing columns, you can just select these testing columns as @lbendlin suggests and then unpivot these selected columns them:

Unpivot the selected columnsUnpivot the selected columns

yingyinr_1-1644909260141.png

Then create the measures as below to get the recent score and the average score for per ID:

 

Recent score = 
VAR _selid =
    SELECTEDVALUE ( 'Table'[ID] )
VAR _selttype =
    SELECTEDVALUE ( 'Table'[Testing Type] )
VAR _maxdate =
    CALCULATE (
        MAX ( 'Table'[TEST DATE] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[ID] = _selid
                && 'Table'[Testing Type] = _selttype
        )
    )
RETURN
    CALCULATE (
        MAX ( 'Table'[Score] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[ID] = _selid
                && 'Table'[Testing Type] = _selttype
                && 'Table'[TEST DATE] = _maxdate
        )
    )
Average score = 
VAR _seltype =
    SELECTEDVALUE ( 'Table'[Testing Type] )
VAR avgtimeinminutes =
    IF (
        _seltype = "3 MILE (mm:ss)",
        AVERAGEX (
            'Table',
            VAR timeinminutes =
                HOUR ( [Score] ) * 60
                    + MINUTE ( [Score] )
            VAR adjminutes =
                IF ( timeinminutes < 720, timeinminutes + 1440, timeinminutes )
            RETURN
                adjminutes
        ),
        AVERAGEX ( 'Table', VALUE ( 'Table'[Score] ) )
    )
VAR avgtimecorrection =
    IF ( avgtimeinminutes < 1440, avgtimeinminutes, avgtimeinminutes - 1440 )
VAR avghour =
    QUOTIENT ( avgtimecorrection, 60 )
VAR avgmin =
    MOD ( avgtimecorrection, 60 )
RETURN
    IF (
        ISINSCOPE ( 'Table'[ID] ),
        [Recent score],
        IF (
            _seltype = "3 MILE (mm:ss)",
            FORMAT ( TIME ( avghour, avgmin, 0 ), "hh:mm:ss AM/PM" ),
            avgtimeinminutes
        )
    )

 

yingyinr_2-1644911651964.png

Best Regards

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi @Anonymous ,

Since you have multiple testing columns, you can just select these testing columns as @lbendlin suggests and then unpivot these selected columns them:

Unpivot the selected columnsUnpivot the selected columns

yingyinr_1-1644909260141.png

Then create the measures as below to get the recent score and the average score for per ID:

 

Recent score = 
VAR _selid =
    SELECTEDVALUE ( 'Table'[ID] )
VAR _selttype =
    SELECTEDVALUE ( 'Table'[Testing Type] )
VAR _maxdate =
    CALCULATE (
        MAX ( 'Table'[TEST DATE] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[ID] = _selid
                && 'Table'[Testing Type] = _selttype
        )
    )
RETURN
    CALCULATE (
        MAX ( 'Table'[Score] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[ID] = _selid
                && 'Table'[Testing Type] = _selttype
                && 'Table'[TEST DATE] = _maxdate
        )
    )
Average score = 
VAR _seltype =
    SELECTEDVALUE ( 'Table'[Testing Type] )
VAR avgtimeinminutes =
    IF (
        _seltype = "3 MILE (mm:ss)",
        AVERAGEX (
            'Table',
            VAR timeinminutes =
                HOUR ( [Score] ) * 60
                    + MINUTE ( [Score] )
            VAR adjminutes =
                IF ( timeinminutes < 720, timeinminutes + 1440, timeinminutes )
            RETURN
                adjminutes
        ),
        AVERAGEX ( 'Table', VALUE ( 'Table'[Score] ) )
    )
VAR avgtimecorrection =
    IF ( avgtimeinminutes < 1440, avgtimeinminutes, avgtimeinminutes - 1440 )
VAR avghour =
    QUOTIENT ( avgtimecorrection, 60 )
VAR avgmin =
    MOD ( avgtimecorrection, 60 )
RETURN
    IF (
        ISINSCOPE ( 'Table'[ID] ),
        [Recent score],
        IF (
            _seltype = "3 MILE (mm:ss)",
            FORMAT ( TIME ( avghour, avgmin, 0 ), "hh:mm:ss AM/PM" ),
            avgtimeinminutes
        )
    )

 

yingyinr_2-1644911651964.png

Best Regards

Anonymous
Not applicable

Thanks so much, this is exceptional!

lbendlin
Super User
Super User

First thing would be to unpivot and normalize your data.  You want to arrive at a star schema structure like 

 

Facts:

ID   Date  Activity Type  Value  

 

Then have another table for users with

 

Users:

ID  Gender

 

another table for activities

 

Activities:

Activity Type   Unit of Measure.

 

Calendar:

Date   Month  Year  etc.

 

and so on.  Once that is in place the visualizations will be easier.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

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.

Top Solution Authors