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

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
v-yiruan-msft
Community Support
Community Support

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

Community Support Team _ Rena
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

3 REPLIES 3
v-yiruan-msft
Community Support
Community Support

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

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
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
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.