Forum Discussion

Fistachpl's avatar
Fistachpl
Icon for Helper III rankHelper III
1 year ago
Solved

Time spent in programm - measure

Hello,       I have a table TProgram with 3 columns:       User, dateandtime, type       User is the name of the user       In this table column dateandtime is the dateand hour operat...
  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi Fistachpl 

    First, create a calculated column for session end time:

    EndTime = 
    VAR CurrentUser = TProgram[User]
    VAR CurrentDateTime = TProgram[dateandtime]
    VAR CurrentDate = DATE(YEAR(CurrentDateTime), MONTH(CurrentDateTime), DAY(CurrentDateTime))
    VAR NextLogout = 
        CALCULATE(
            MIN(TProgram[dateandtime]),
            FILTER(
                ALL(TProgram),
                TProgram[User] = CurrentUser &&
                TProgram[type] = "Logout" &&
                TProgram[dateandtime] > CurrentDateTime &&
                DATE(YEAR(TProgram[dateandtime]), MONTH(TProgram[dateandtime]), DAY(TProgram[dateandtime])) = CurrentDate
            )
        )
    VAR NextLogin = 
        CALCULATE(
            MIN(TProgram[dateandtime]),
            FILTER(
                ALL(TProgram),
                TProgram[User] = CurrentUser &&
                TProgram[type] = "Login" &&
                TProgram[dateandtime] > CurrentDateTime &&
                DATE(YEAR(TProgram[dateandtime]), MONTH(TProgram[dateandtime]), DAY(TProgram[dateandtime])) = CurrentDate
            )
        )
    VAR LastEventOfDay = 
        CALCULATE(
            MAX(TProgram[dateandtime]),
            FILTER(
                ALL(TProgram),
                TProgram[User] = CurrentUser &&
                DATE(YEAR(TProgram[dateandtime]), MONTH(TProgram[dateandtime]), DAY(TProgram[dateandtime])) = CurrentDate
            )
        )
    VAR MinNextEvent = 
        SWITCH(
            TRUE(),
            NOT ISBLANK(NextLogout) && NOT ISBLANK(NextLogin), MIN(NextLogout, NextLogin),
            NOT ISBLANK(NextLogout), NextLogout,
            NOT ISBLANK(NextLogin), NextLogin,
            BLANK()
        )
    RETURN
    IF(
        TProgram[type] = "Login",
        IF(
            NOT ISBLANK(MinNextEvent),
            MinNextEvent,
            LastEventOfDay
        ),
        BLANK()
    )

     

     

    Then create a calculated column for session duration:

    Duration (Hours) = 
    IF(
        TProgram[type] = "Login",
        DATEDIFF(TProgram[dateandtime], TProgram[EndTime], SECOND) / 3600,
        BLANK()
    )

     

    Then create a calculated column to sum the duration:

     

    Total Time Spent (Hours) = 
    SUMX(
        FILTER(TProgram, TProgram[type] = "Login"),
        TProgram[Duration (Hours)]
    )

     

    Result:

     

     

     

     

    Best Regards,

    Jayleny

     

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