Forum Discussion

Will_740's avatar
Will_740
New Member
6 years ago
Solved

Average Time Measure

Hi,   Im looking for a measure to calculate an average time that my clients fall asleep at night. I have used the following DAX formula for wake up time which is satisfactory:   Sleep Onset Time...
  • mahoneypat's avatar
    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