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
brokencornets
Helper III
Helper III

Comparing dates in adjacent rows

Hi all

 

I'm new to Power BI and I'm trying to convert an Excel report. One thing I'm finding extremely difficult is comparing different rows. To give some context, the structure of my data is as follows:

 

Job RefStatusTimestampActivityCrew
1111111Travel12/02/2018 09:3322John Doe
1111111Working12/02/2018 09:3422John Doe
1111111Suspended12/02/2018 15:5822John Doe
2222222Travel13/02/2018 07:553John Doe
2222222Working13/02/2018 07:593John Doe
2222222Suspended13/02/2018 15:463John Doe
1111111Travel14/02/2018 08:2022John Doe
1111111Working14/02/2018 08:2022John Doe
1111111Complete14/02/2018 11:4422John Doe

 

So the issue with my data is that there is no 'end' timestamp - so in the above, the only way of knowing how long John Doe was in a 'Working' status is by getting the 'Suspended' timestamp. I'm currently using VBA to achieve this in Excel, but I'm struggling to get my head around DAX to be honest.

 

Looking at other solutions, I'm assuming I'm going to need EARLIER but nothing I'm trying is working so I hoped someone using my data would be able to offer a more bespoke solution? What I'm basically aiming to achieve is an extra column which will show the duration for every Travel and Working row.

 

Thanks for any help you can offer!

1 ACCEPTED SOLUTION

@brokencornets

 

See the File attached

 

comparingdates.png


Regards
Zubair

Please try my custom visuals

View solution in original post

5 REPLIES 5
vmakhija
Post Prodigy
Post Prodigy

@brokencornets

Using your dataset, I created 3 measures as below -

WorkingTime =
CALCULATE ( MINX( FILTER(ShiftData,ShiftData[Status]="Working"),ShiftData[Timestamp]) , ALLEXCEPT ( ShiftData, ShiftData[Job Ref] ) )

 

SuspendedTime =
CALCULATE ( MAXX( FILTER(ShiftData,ShiftData[Status]="Suspended"),ShiftData[Timestamp]) , ALLEXCEPT ( ShiftData, ShiftData[Job Ref] ) )

 

TotalTimeSpent = [SuspendedTime] - [WorkingTime]

 

Here is how the data looks -

ShiftData.JPG

 

 

 

 

 

 

 

 

 

 

 

 

Check and let me know if it helps you.

 

Regards

Thanks for getting back to me vmakhija

 

Unfortunately it's not quite bring back the results I need, as it only brings back the working time and not the travelling time, and also doesn't take into account when jobs are completed, only suspended. It also only seems to look at one days worth of work, although that could be because it's only looking for suspended and not complete?

 

The ideal reasult I'm looking for is this:

 

StatusTimestampActivityCrewEnd Timestamp
Travel12/02/2018 09:3322John Doe12/02/2018 09:34
Working12/02/2018 09:3422John Doe12/02/2018 15:58
Suspended12/02/2018 15:5822John Doenull
Travel13/02/2018 07:553John Doe13/02/2018 07:59
Working13/02/2018 07:593John Doe13/02/2018 15:46
Suspended13/02/2018 15:463John Doenull
Travel14/02/2018 08:2022John Doe14/02/2018 08:20
Working14/02/2018 08:2022John Doe14/02/2018 11:44
Complete14/02/2018 11:4422John Doenull

Hi @brokencornets

 

Try this. First Add an Index Column in your Table using Query Editor

 

Then use this calculated column

 

Column =
VAR NextTimeSTamp =
    CALCULATE (
        MIN ( Table1[Timestamp] ),
        FILTER (
            ALLEXCEPT ( Table1, Table1[Crew] ),
            Table1[Index] > EARLIER ( Table1[Index] )
        )
    )
VAR NextJobRef =
    CALCULATE (
        MIN ( Table1[Job Ref] ),
        FILTER ( ALLEXCEPT ( Table1, Table1[Crew] ), Table1[Timestamp] = NextTimeSTamp )
    )
RETURN
    IF ( Table1[Job Ref] = NextJobRef, NextTimeSTamp )

Regards
Zubair

Please try my custom visuals

@brokencornets

 

See the File attached

 

comparingdates.png


Regards
Zubair

Please try my custom visuals

This looks great @Zubair_Muhammad, thanks!

 

Unfortunately when I try it in my file I get an error stating I don't have enough memory, so I'm probably going to need to strip back the amount of data I'm pulling into it, but if it works like it looks like it does it'll be awesome.

 

Thanks again!

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.