Forum Discussion

nk123's avatar
nk123
New Member
1 year ago
Solved

Finding duration between two time rows grouped based on two other columns on Power BI

I have data in the below format, sorted by User asc, Session ID asc and Time asc. I need to find the duration each user spends on each Event within each session.

 

For example, using the below data:

EventUserSession IDTime
a1x10/29/2024 9:37
a1x10/29/2024 9:43
a1x10/29/2024 9:43
b1x10/29/2024 9:43
b1x10/29/2024 9:44
a1x10/29/2024 9:44
a2y11/5/2024 6:53
a2y11/5/2024 6:53
a2y11/5/2024 6:53
c2y11/5/2024 6:55
c2y11/5/2024 6:55
c2y11/5/2024 6:55
b2y11/5/2024 6:55
b2y11/5/2024 6:55
a2y11/5/2024 6:55
a2y11/5/2024 6:55
a3z10/28/2024 6:34

 

I need help to create a summarised table in terms of the below:

Eventsession IDAverage Duration spent in minutes
ax7
ay1
az1
bx1
by1
cy1

 

The duration spent is to be considered per User, per session.

 

I was able to get this easily on excel using formulas but wanted to replicate on powerBI, which I am newly exploring. I understand the excel formula logic doesnt work the same way on powerbi. Thank you!

  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi nk123 ,

     

    As far as I know, if you want to get duration, you just need to get min and max date for each Event/User/SessionID.

    Then the new table should looks like as below.

    Summarized Table = 
    VAR _T1 = SUMMARIZE('Table','Table'[Event],'Table'[User],'Table'[Session ID],"Duration",
    VAR _MIN =
    CALCULATE(MIN('Table'[Time]))
    VAR _MAX =
    CALCULATE(MAX('Table'[Time]))
    RETURN
    DATEDIFF(_MIN,_MAX,MINUTE)
    )
    RETURN
    _T1

    Here I am confused about how you get the average. I can see some duplicate data in your sample. Does these data make scense? 

    And I think based on your sample, some duration will return 0 and the average will return 0 as well. 

    The result you want is incorrect. Please show more details for us.

     

    Best Regards,
    Rico Zhou

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

     

     

3 Replies

  • nk123 

    Add a Calculated Column:

    Duration (Minutes) = 
    VAR PrevTime =
    CALCULATE(
    MAX('Table'[Time]),
    FILTER(
    'Table',
    'Table'[User] = EARLIER('Table'[User]) &&
    'Table'[Session ID] = EARLIER('Table'[Session ID]) &&
    'Table'[Event] = EARLIER('Table'[Event]) &&
    'Table'[Time] < EARLIER('Table'[Time])
    )
    )
    RETURN
    IF(
    ISBLANK(PrevTime),
    0,
    DATEDIFF(PrevTime, 'Table'[Time], MINUTE)
    )

    Create a new Table:

    Summarized Table = 
    SUMMARIZE(
    'Table',
    'Table'[Event],
    'Table'[Session ID],
    "Average Duration (Minutes)",
    AVERAGEX(
    FILTER(
    'Table',
    'Table'[Event] = EARLIER('Table'[Event]) &&
    'Table'[Session ID] = EARLIER('Table'[Session ID])
    ),
    'Table'[Duration (Minutes)]
    )
    )

    This table will contain Event, Session ID, and the average duration (in minutes) spent on each event per sessio

     

    💌 If this helped, a Kudos 👍 or Solution mark would be great! 🎉
    Cheers,
    Kedar
    Connect on LinkedIn

  • Hi nk123, Please try the below DAX to create a new table

    Summarized Table =
    SUMMARIZE(
    YourTable,
    YourTable[Event],
    YourTable[Session ID],
    "Average Duration (Minutes)",
    AVERAGEX(
    SUMMARIZE(
    YourTable,
    YourTable[User],
    YourTable[Event],
    YourTable[Session ID],
    "Duration Per User",
    SUM(YourTable[Duration])
    ),
    [Duration Per User]
    )
    )

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi nk123 ,

     

    As far as I know, if you want to get duration, you just need to get min and max date for each Event/User/SessionID.

    Then the new table should looks like as below.

    Summarized Table = 
    VAR _T1 = SUMMARIZE('Table','Table'[Event],'Table'[User],'Table'[Session ID],"Duration",
    VAR _MIN =
    CALCULATE(MIN('Table'[Time]))
    VAR _MAX =
    CALCULATE(MAX('Table'[Time]))
    RETURN
    DATEDIFF(_MIN,_MAX,MINUTE)
    )
    RETURN
    _T1

    Here I am confused about how you get the average. I can see some duplicate data in your sample. Does these data make scense? 

    And I think based on your sample, some duration will return 0 and the average will return 0 as well. 

    The result you want is incorrect. Please show more details for us.

     

    Best Regards,
    Rico Zhou

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.