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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
wiselyman3
Helper II
Helper II

How to create aMeasure/Column to show the duration of time since last entry, with multiple condition

Hi All, this may be a confusing one so bear with me. I have a table that logs changes that employees have made to the workgroups they are in, and the date/time that these changes were made. I am in need of adding a column that calculates the total amount of time that they were out of each workgroup when they activate themselves back in it. Please see below for samples data and expected result: 

Sample: 

UserChangeWorkgroupDate/Time
SamOutRed1/1/25 1:00 PM
AlexOutRed1/1/25 1:30 PM
SamOutGreen1/1/25 1:45 PM
TinaOutRed1/1/25 2:00 PM
TinaOutGreen1/1/25 2:30 PM
AlexInRed1/1/25 3:00 PM
AlexOutGreen1/1/25 3:15 PM
TinaInRed1/1/25 3:30 PM
SamInRed1/1/25 8:00 PM
Sam InGreen1/1/25 8:30 PM
AlexInGreen1/1/25 9:00 PM
TinaInGreen

1/2/25 8:00 AM

In this sample, "Out" means that they have taken themselves out of that workgroup. "In" means they have placed themselves back in to that workgroup. I am looking for a column that shows the total duration they were "Out" of that workgroup, and the duration of that will be displayed on the row that they went back "In" to it. Expected result for this sample data is below: 

UserChange WorkgroupDate/TimeTime Out of Workgroup
SamOutRed1/1/25 1:00 PMN/A
AlexOutRed1/1/25 1:30 PMN/A
SamOutGreen1/1/25 1:45 PMN/A
TinaOutRed1/1/25 2:00 PMN/A
TinaOutGreen1/1/25 2:30 PMN/A
AlexInRed1/1/25 3:00 PM2 hrs
AlexOutGreen1/1/25 3:15 PMN/A
TinaInRed1/1/25 3:30 PM1 hr 30 mins
SamInRed1/1/25 8:00 PM7 hrs
Sam InGreen1/1/25 8:30 PM6 hrs 45 mins
AlexInGreen1/1/25 9:00 PM5 hrs 45 mins
TinaInGreen1/2/25 8:00 AM

17 hrs 30 mins

As you can see, I need it to look back to the last "Out" entry for that specific User AND workgroup. The logic is that for example, "Sam" goes Out of the Red workgroup at 1/1/25 1:00 pm. He then goes back In to the Red workgroup at 1/1/25 8:00 pm. This gives us a time of 7 hrs of Sam being Out of the Red workgroup, shown in the expected result column. Hopefully this is clear and I appreciate the help! 

1 ACCEPTED SOLUTION
Cookistador
Solution Sage
Solution Sage

Hi @wiselyman3 

 

After some tests, this is what I achieve

Cookistador_0-1749669425875.png

 

 

This is the dax code I used

 

Time Out of Workgroup =
VAR CurrentUser = [User]
VAR CurrentWorkgroup = [Workgroup]
VAR CurrentDateTime = [Date/Time]
VAR CurrentChange = [Change]

RETURN
    IF(
        CurrentChange = "In",
        VAR LastOutDateTime =
            CALCULATE(
                MAXX(
                    FILTER(
                        ALL('Table'),
                        'Table'[User] = CurrentUser &&
                        'Table'[Workgroup] = CurrentWorkgroup &&
                        'Table'[Change] = "Out" &&
                        'Table'[Date/Time] < CurrentDateTime
                    ),
                    'Table'[Date/Time]
                ),
                ALLEXCEPT('Table', 'Table'[User], 'Table'[Workgroup])
            )
        RETURN
            IF(
                NOT ISBLANK(LastOutDateTime),
                VAR DurationInSeconds = DATEDIFF(LastOutDateTime, CurrentDateTime, SECOND)
                VAR Hours = INT(DurationInSeconds / 3600)
                VAR Minutes = INT(MOD(DurationInSeconds, 3600) / 60)
                RETURN
                    IF(
                        Hours = 0 && Minutes = 0,
                        "N/A",
                        FORMAT(Hours, "0") & " hrs " & FORMAT(Minutes, "0") & " mins"
                    )
            )
    )
 
 
Do no hesistate to ask if something is not working

View solution in original post

4 REPLIES 4
Cookistador
Solution Sage
Solution Sage

Hi @wiselyman3 

 

After some tests, this is what I achieve

Cookistador_0-1749669425875.png

 

 

This is the dax code I used

 

Time Out of Workgroup =
VAR CurrentUser = [User]
VAR CurrentWorkgroup = [Workgroup]
VAR CurrentDateTime = [Date/Time]
VAR CurrentChange = [Change]

RETURN
    IF(
        CurrentChange = "In",
        VAR LastOutDateTime =
            CALCULATE(
                MAXX(
                    FILTER(
                        ALL('Table'),
                        'Table'[User] = CurrentUser &&
                        'Table'[Workgroup] = CurrentWorkgroup &&
                        'Table'[Change] = "Out" &&
                        'Table'[Date/Time] < CurrentDateTime
                    ),
                    'Table'[Date/Time]
                ),
                ALLEXCEPT('Table', 'Table'[User], 'Table'[Workgroup])
            )
        RETURN
            IF(
                NOT ISBLANK(LastOutDateTime),
                VAR DurationInSeconds = DATEDIFF(LastOutDateTime, CurrentDateTime, SECOND)
                VAR Hours = INT(DurationInSeconds / 3600)
                VAR Minutes = INT(MOD(DurationInSeconds, 3600) / 60)
                RETURN
                    IF(
                        Hours = 0 && Minutes = 0,
                        "N/A",
                        FORMAT(Hours, "0") & " hrs " & FORMAT(Minutes, "0") & " mins"
                    )
            )
    )
 
 
Do no hesistate to ask if something is not working

Hi, 

 

When I try this, I get this error: 

"The value for 'User' cannot be determined. Either the column doesn't exist, or there is no current row for this column.

 

I have made sure Table and all of the column names match the dax, but I am still getting this error

Hi 

Sorry I didn't mentioned it but it is a calculated column not a measure 

If it's not working with a calculated column, can you try to add your table name before the field 

Got it, this works perfectly. Thank you!!

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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