Forum Discussion
Average Time Measure
- 6 years ago
This was an interesting challenge, and the expression below should get your desired result. It takes each row (onset time) and converts it to minutes. If it is < 12 PM, it adds 24 hrs (in minutes) to it, before taking the average. Once the average is calculated, it subtracts the 24 hrs back off (if >midnight), and converts the minutes back to hours and minutes.
Correct Average =
VAR avgtimeinminutes =
AVERAGEX (
Sleep,
VAR timeinminutes =
HOUR ( Sleep[Onset] ) * 60
+ MINUTE ( Sleep[Onset] )
VAR adjminutes =
IF ( timeinminutes < 720, timeinminutes + 1440, timeinminutes )
RETURN
adjminutes
)
VAR avgtimecorrection =
IF ( avgtimeinminutes < 1440, avgtimeinminutes, avgtimeinminutes - 1440 )
VAR avghour =
QUOTIENT ( avgtimecorrection, 60 )
VAR avgmin =
MOD ( avgtimecorrection, 60 )
RETURN
FORMAT ( TIME ( avghour, avgmin, 0 ), "h:mm am/pm" )If this works for you, please mark it as the solution. Kudos are appreciated too. Please let me know if not.
Regards,
Pat
This was an interesting challenge, and the expression below should get your desired result. It takes each row (onset time) and converts it to minutes. If it is < 12 PM, it adds 24 hrs (in minutes) to it, before taking the average. Once the average is calculated, it subtracts the 24 hrs back off (if >midnight), and converts the minutes back to hours and minutes.
Correct Average =
VAR avgtimeinminutes =
AVERAGEX (
Sleep,
VAR timeinminutes =
HOUR ( Sleep[Onset] ) * 60
+ MINUTE ( Sleep[Onset] )
VAR adjminutes =
IF ( timeinminutes < 720, timeinminutes + 1440, timeinminutes )
RETURN
adjminutes
)
VAR avgtimecorrection =
IF ( avgtimeinminutes < 1440, avgtimeinminutes, avgtimeinminutes - 1440 )
VAR avghour =
QUOTIENT ( avgtimecorrection, 60 )
VAR avgmin =
MOD ( avgtimecorrection, 60 )
RETURN
FORMAT ( TIME ( avghour, avgmin, 0 ), "h:mm am/pm" )
If this works for you, please mark it as the solution. Kudos are appreciated too. Please let me know if not.
Regards,
Pat