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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
sfernamer
Helper III
Helper III

Average time grouped by column

Hi everyone!

 

I'm working with time (minutes and seconds played) and would like to get the average time for every distinct pair of players. I tried the following formula but didn't work because it's said that the variable can't get an unique value.

 

How could I get the average value? I add a Drive link to the Excel file with the Raw Data and the expected result, to try to help to get the result.

 

Link with the Raw Data and Expected Result: Link with file 

 

Thank you in advance.

 

Tested measure (not working):

 

 Average Time = 
 var IntCourt = 'Table'[Int_OnCourt]

Return
AVERAGEX( 
    FILTER(ALL('Table'), 'Table'[Int_OnCourt] = IntCourt),
          'Table'[TimeOnCourt])

 

6 REPLIES 6
Ahmedx
Super User
Super User

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.
https://1drv.ms/u/s!AiUZ0Ws7G26Rh3ONItvQtPlETpMW?e=dW9liP
Screen Capture #1045.png

Hi @Ahmedx 

Your answer worked for almost all cases but there are cases where the result is "00:00:60" instead of "00:01:00", Idk why is not rounding this number but, if I wanna convert the text result into time, it's not working for these cases. For the other ones, it's working perfectly. 

Pls try this
https://onedrive.live.com/?authkey=%21AI0i29C0%2DUROkxY&cid=916E1B3B6BD11925&id=916E1B3B6BD11925%211...

 

Average TimeOnCourt2 = 
VAR _Count = COUNTROWS('RawData')
VAR _time = ADDCOLUMNS('RawData',"@time",
VAR _H = HOUR('RawData'[TimeOnCourt]) *60*60
VAR _M = MINUTE('RawData'[TimeOnCourt])*60
VAR _S = SECOND('RawData'[TimeOnCourt])
RETURN
_H+_M+_S)
VAR _Result = DIVIDE(SUMX(_time,[@time]),_Count)
RETURN  
   VAR Hours =
    INT (_Result/ 3600)
VAR Minutes = INT ( MOD( _Result - ( Hours * 3600 ),3600 ) / 60)

VAR Seconds =
    ROUNDUP(MOD ( MOD( _Result - ( Hours * 3600 ),3600 ), 60 ),0) 
VAR H =
    IF ( LEN ( Hours ) = 1, 
        CONCATENATE ( "0", Hours ),
        CONCATENATE ( "", Hours )
      )
VAR M =
    IF (
        LEN ( Minutes ) = 1,
        CONCATENATE ( "0", Minutes ),
        CONCATENATE ( "", Minutes )
    )
VAR S =
    IF (
        LEN ( Seconds ) = 1,
        CONCATENATE ( "0", Seconds ),
        CONCATENATE ( "", Seconds )
    )
RETURN
IF(_Result,
    TIME(H,M,S))
------------ or ------------
Average TimeOnCourt3 = 
VAR _Count = COUNTROWS('RawData')
VAR _time = ADDCOLUMNS('RawData',"@time",
VAR _H = HOUR('RawData'[TimeOnCourt]) *60*60
VAR _M = MINUTE('RawData'[TimeOnCourt])*60
VAR _S = SECOND('RawData'[TimeOnCourt])
RETURN
_H+_M+_S)
VAR _Result = DIVIDE(SUMX(_time,[@time]),_Count)
RETURN  
   VAR Hours =
    INT (_Result/ 3600)
VAR Minutes = INT ( MOD( _Result - ( Hours * 3600 ),3600 ) / 60)

VAR Seconds =
    ROUNDUP(MOD ( MOD( _Result - ( Hours * 3600 ),3600 ), 60 ),0) 
RETURN
IF(_Result,
    TIME(Hours,Minutes,Seconds))

 

 

Screen Capture #1123.png 

you can create an example where the measure counts incorrectly?

amitchandak
Super User
Super User

@sfernamer , Try like

 

Avg time =
var _time = AVERAGEX(Table,HOUR(Table[TimeOnCourt])*3600+Minute(Table[TimeOnCourt])*60 +SECOND(Table[TimeOnCourt]))
return
time(QUOTIENT(_time,3600) ,QUOTIENT(Mod(_time,3600),60),mod(Mod(_time,3600),60))

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Thank you for your time, @amitchandak , but this formula didn't work for my case. The one taht worked more or less is the one posted for the other user. Thank you anyway for your help and your time. 🙂

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 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
Top Kudoed Authors