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

calculate average of seconds difference between 2 dates and format the average

hi,

I need to implement the below logic in power bi:

 

 If (JMS_DELIVERED_DATE_TIME is not NULL) and (FIRST_TOUCH_DATE_TIME is not NULL) and (LW_RECEIVE_START_DATE_TIME  is not NULL ) then AVG (JMS_DELIVERED_DATE_TIME  - FIRST_TOUCH_DATE_TIME )

 

So to implement the above logic I am using below expression in dax

1.First, calculate the seconds difference between 2 dates:

 

Gateway_Time_Diff = IF(Test[JMS_DELIVERED_DATE_TIME]<>BLANK() && Test[FIRST_TOUCH_DATE_TIME]<>BLANK() && Test[LW_RECEIVE_START_DATE_TIME]<>BLANK(), DATEDIFF(Test[FIRST_TOUCH_DATE_TIME],Test[JMS_DELIVERED_DATE_TIME],SECOND))

 

2.Second: calculate the average

GatewayTimeAvg = averagex(Test,Test[Gateway_Time_Diff])

 

3.Third: formatting sends in hh:minutes:secods format

GatewayTime =
var DAXDay   = INT(Test[GatewayTimeAvg]/(24*60*60))
var DAXHours = MOD(INT(Test[GatewayTimeAvg]/(60*60)),24)
var DAXMin   = MOD(INT(Test[GatewayTimeAvg]/60),60)
var DAXSec   = MOD(Test[GatewayTimeAvg],60)
return
FORMAT(DAXHours,"#00")&":"&FORMAT(DAXMin,"#00")&":"&FORMAT(DAXSec,"#00")

 

so please advise me if I am correct in this case? as because the average time is shwing as 9:30:37 only.

1 ACCEPTED SOLUTION

Hi @Anonymous,

 

To use the combined one as below.

 

Column =
VAR a =
    CALCULATE (
        SUMX (
            FILTER (
                ALL ( Test ),
                Test[JMS_DELIVERED_DATE_TIME] <> BLANK ()
                    && Test[FIRST_TOUCH_DATE_TIME] <> BLANK ()
                    && Test[LW_RECEIVE_START_DATE_TIME] <> BLANK ()
            ),
            DATEDIFF ( Test[FIRST_TOUCH_DATE_TIME], Test[JMS_DELIVERED_DATE_TIME], SECOND )
        )
    )
VAR b =
    a
        / CALCULATE (
            COUNTROWS ( Test ),
            FILTER (
                ALL ( Test ),
                Test[JMS_DELIVERED_DATE_TIME] <> BLANK ()
                    && Test[FIRST_TOUCH_DATE_TIME] <> BLANK ()
                    && Test[LW_RECEIVE_START_DATE_TIME] <> BLANK ()
            )
        )
VAR DAXDay =
    INT ( b / ( 24 * 60 * 60 ) )
VAR DAXHours =
    MOD ( INT ( b / ( 60 * 60 ) ), 24 )
VAR DAXMin =
    MOD ( INT ( b / 60 ), 60 )
VAR DAXSec =
    MOD ( b, 60 )
RETURN
    DAXDay & "day "
        & FORMAT ( DAXHours, "#00" )
        & ":"
        & FORMAT ( DAXMin, "#00" )
        & ":"
        & FORMAT ( DAXSec, "#00" )

123.PNG

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

View solution in original post

4 REPLIES 4
v-frfei-msft
Community Support
Community Support

Hi @Anonymous,

 

Please update your third formula as below.

 

GatewayTime = 
var DAXDay   = INT(Test[GatewayTimeAvg]/(24*60*60))
var DAXHours = MOD(INT(Test[GatewayTimeAvg]/(60*60)),24)
var DAXMin   = MOD(INT(Test[GatewayTimeAvg]/60),60)
var DAXSec   = MOD(Test[GatewayTimeAvg],60)
return
DAXDay &"day "& FORMAT(DAXHours,"#00")&":"&FORMAT(DAXMin,"#00")&":"&FORMAT(DAXSec,"#00")

 

Capture.PNG

 

For more details, please check the pbix as attached.

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Anonymous
Not applicable

Hi,

As I have seen that you only have included the 'Day' in my existing format, but my question was "Is my DAX expression was correct?" and Can it be done in a single expression without creating 3 custom column?

 

Thanks,

Sandip

Hi @Anonymous,

 

To use the combined one as below.

 

Column =
VAR a =
    CALCULATE (
        SUMX (
            FILTER (
                ALL ( Test ),
                Test[JMS_DELIVERED_DATE_TIME] <> BLANK ()
                    && Test[FIRST_TOUCH_DATE_TIME] <> BLANK ()
                    && Test[LW_RECEIVE_START_DATE_TIME] <> BLANK ()
            ),
            DATEDIFF ( Test[FIRST_TOUCH_DATE_TIME], Test[JMS_DELIVERED_DATE_TIME], SECOND )
        )
    )
VAR b =
    a
        / CALCULATE (
            COUNTROWS ( Test ),
            FILTER (
                ALL ( Test ),
                Test[JMS_DELIVERED_DATE_TIME] <> BLANK ()
                    && Test[FIRST_TOUCH_DATE_TIME] <> BLANK ()
                    && Test[LW_RECEIVE_START_DATE_TIME] <> BLANK ()
            )
        )
VAR DAXDay =
    INT ( b / ( 24 * 60 * 60 ) )
VAR DAXHours =
    MOD ( INT ( b / ( 60 * 60 ) ), 24 )
VAR DAXMin =
    MOD ( INT ( b / 60 ), 60 )
VAR DAXSec =
    MOD ( b, 60 )
RETURN
    DAXDay & "day "
        & FORMAT ( DAXHours, "#00" )
        & ":"
        & FORMAT ( DAXMin, "#00" )
        & ":"
        & FORMAT ( DAXSec, "#00" )

123.PNG

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

Hi @Anonymous,

 

Does that make sense? If so, kindly mark my answer as a solution to close the case.


Regards,
Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

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.