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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Zosy
Helper II
Helper II

Time spent in a session doing various activities

Hi,

I am trying to calculate 2 separate things:
1. how much time in average does a customer spend doing certain activities
2. what proportion of a session was spent doing an activity


Desired results:

1. For activity A in average the user spent 49 seconds this year doing it
2. Activity B in Session 8 was 0.81% out of the entire session 8.



Data: There are 2 separate tables and no Date table is used.

Activity Table

StartTimeEndTimeActivityNameSessionId
15/01/2025 15:3015/01/2025 15:31A1
09/01/2025 15:2909/01/2025 15:30A2
03/01/2025 14:2303/01/2025 14:25A3
27/01/2025 09:1627/01/2025 09:17A4
23/01/2025 10:1523/01/2025 10:15A5
15/01/2025 10:1315/01/2025 10:14A6
13/01/2025 12:4913/01/2025 12:50A7
16/01/2025 11:0216/01/2025 11:03A8
16/01/2025 11:0616/01/2025 11:07B8

 

Session Table

StartTimeEndTimeSessionId
15/01/2025 15:0215/01/2025 15:571
09/01/2025 15:0109/01/2025 15:372
03/01/2025 11:1003/01/2025 15:313
27/01/2025 09:1427/01/2025 09:174
23/01/2025 10:1523/01/2025 10:165
15/01/2025 10:1315/01/2025 10:146
13/01/2025 11:0713/01/2025 13:227
16/01/2025 10:3216/01/2025 12:278



Any help is greatly appreciated.

1 ACCEPTED SOLUTION
DataNinja777
Super User
Super User

Hi @Zosy ,

 

You can calculate the average time a customer spends on an activity by computing the duration for each activity, filtering for the current year, and then taking the average. The following DAX measure achieves this:

Avg Activity Duration (Seconds) =
VAR CurrentYear = YEAR(TODAY())
VAR FilteredActivities =
    FILTER('Activity Table', YEAR('Activity Table'[StartTime]) = 2025)
VAR ActivityDurations =
    ADDCOLUMNS(
        FilteredActivities,
        "Duration", DATEDIFF([StartTime], [EndTime], SECOND)
    )
RETURN
    AVERAGEX(ActivityDurations, [Duration])

To determine what proportion of a session was spent on an activity, you need to calculate the total session duration and compare it to the total time spent on the selected activity within that session. The following measure accomplishes this:

Activity Proportion in Session (%) =
VAR SessionDuration =
    CALCULATE(
        DATEDIFF(
            MIN('Session Table'[StartTime]),
            MAX('Session Table'[EndTime]),
            SECOND
        ),
        'Session Table'[SessionId] = SELECTEDVALUE('Activity Table'[SessionId])
    )
VAR ActivityDuration =
    SUMX(
        FILTER(
            'Activity Table',
            'Activity Table'[SessionId] = SELECTEDVALUE('Session Table'[SessionId])
        ),
        DATEDIFF([StartTime], [EndTime], SECOND)
    )
RETURN
    DIVIDE(ActivityDuration, SessionDuration, 0) * 100

Using these formulas, you can determine that for activity A, the average time spent this year is approximately 49 seconds, while activity B in session 8 accounts for about 0.81% of the total session duration. These measures ensure accurate calculations without the need for a Date Table, as all calculations rely directly on the provided timestamps.

 

Best regards,

View solution in original post

2 REPLIES 2
DataNinja777
Super User
Super User

Hi @Zosy ,

 

You can calculate the average time a customer spends on an activity by computing the duration for each activity, filtering for the current year, and then taking the average. The following DAX measure achieves this:

Avg Activity Duration (Seconds) =
VAR CurrentYear = YEAR(TODAY())
VAR FilteredActivities =
    FILTER('Activity Table', YEAR('Activity Table'[StartTime]) = 2025)
VAR ActivityDurations =
    ADDCOLUMNS(
        FilteredActivities,
        "Duration", DATEDIFF([StartTime], [EndTime], SECOND)
    )
RETURN
    AVERAGEX(ActivityDurations, [Duration])

To determine what proportion of a session was spent on an activity, you need to calculate the total session duration and compare it to the total time spent on the selected activity within that session. The following measure accomplishes this:

Activity Proportion in Session (%) =
VAR SessionDuration =
    CALCULATE(
        DATEDIFF(
            MIN('Session Table'[StartTime]),
            MAX('Session Table'[EndTime]),
            SECOND
        ),
        'Session Table'[SessionId] = SELECTEDVALUE('Activity Table'[SessionId])
    )
VAR ActivityDuration =
    SUMX(
        FILTER(
            'Activity Table',
            'Activity Table'[SessionId] = SELECTEDVALUE('Session Table'[SessionId])
        ),
        DATEDIFF([StartTime], [EndTime], SECOND)
    )
RETURN
    DIVIDE(ActivityDuration, SessionDuration, 0) * 100

Using these formulas, you can determine that for activity A, the average time spent this year is approximately 49 seconds, while activity B in session 8 accounts for about 0.81% of the total session duration. These measures ensure accurate calculations without the need for a Date Table, as all calculations rely directly on the provided timestamps.

 

Best regards,

Thank you for the clarifications! It worked 🙂

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.