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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
gavinf4444
Frequent Visitor

Help finding earliest and lates times in different columns with criteria

Hi, I've seen some solutions where users have looked for the earliest dates/times and have used the Group By / Min function to achieve this however I have a slightly more complicated situation which I cannot get my head around.

 

I have 2 situations where I want to calculate the shift time of a machine with example data below.

In the first instance, I want to calculate the duration of the day shift (day shift = 06:00-17:59) so for Machine A this would be between 06:00 and 17:30 (690 minutes)

For night shifts (night shift = 18:00-05:59 following day) I think that it gets more complicated because the End Time could be the following day to the Start Date.  For Machine B I want to show 18:30-00:10 (280 minutes) and Machine C should be 22:00-23:45 (105 minutes).  Machine would be 30 minutes however I'd want it to appear with a shift date of 07/07/23.

 

Desired Output -

 

MachineShift Start DateShiftDuration
A07/07/23Day690
B07/07/23Night280
C07/07/23Night105
D07/07/23Night30

 

Sample Data - 

 

MachineStart DateStart TimeEnd DateEnd Time
A07/07/2306:0007/07/2312:00
A07/07/2313:0007/07/2315:00
A07/07/2316:0007/07/2317:30
B07/07/2318:3007/07/2319:00
B07/07/2323:0008/07/23 (UK format)00:10
C07/07/2322:0007/07/2322:30
C07/07/2323:3007/07/2323:45
D08/07/23 (UK format)00:0008/07/2300:30
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @gavinf4444 

You can refer to the following solutions

1.Create four calculated columns in table

 

StartTimeDate = CONVERT([Start Date]&" "&[Start Time],DATETIME)
EndTimeDate = CONVERT([End Date]&" "&[End Time],DATETIME)
ShitDate = IF([Start Time]=TIMEVALUE("00:00"),[Start Date]-1,[Start Date]) 
Type =
VAR a =
    CONVERT ( 'Table'[ShitDate] & " " & TIMEVALUE ( " 06:00" ), DATETIME )
VAR b =
    CONVERT ( 'Table'[ShitDate] & " " & TIMEVALUE ( "17:30" ), DATETIME )
VAR c =
    CONVERT ( 'Table'[ShitDate] & " " & TIMEVALUE ( "18:00" ), DATETIME )
VAR d =
    CONVERT ( 'Table'[ShitDate] + 1 & " " & TIMEVALUE ( "05:59" ), DATETIME )
RETURN
    SWITCH (
        TRUE (),
        [StartTimeDate] >= a
            && [EndTimeDate] <= b, "Day",
        [StartTimeDate] >= c
            && [EndTimeDate] <= d, "Night"
    )

 

2.Then create a measure

 

Duration =
VAR a =
    MINX (
        FILTER (
            ALLSELECTED ( 'Table' ),
            [Machine]
                IN VALUES ( 'Table'[Machine] )
                    && [Type] IN VALUES ( 'Table'[Type] )
        ),
        [StartTimeDate]
    )
VAR b =
    MAXX (
        FILTER (
            ALLSELECTED ( 'Table' ),
            [Machine]
                IN VALUES ( 'Table'[Machine] )
                    && [Type] IN VALUES ( 'Table'[Type] )
        ),
        [EndTimeDate]
    )
RETURN
    DATEDIFF ( a, b, MINUTE )

 

Output

vxinruzhumsft_1-1695178572701.png

 

Best Regards!

Yolo Zhu

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

 

View solution in original post

1 REPLY 1
Anonymous
Not applicable

Hi @gavinf4444 

You can refer to the following solutions

1.Create four calculated columns in table

 

StartTimeDate = CONVERT([Start Date]&" "&[Start Time],DATETIME)
EndTimeDate = CONVERT([End Date]&" "&[End Time],DATETIME)
ShitDate = IF([Start Time]=TIMEVALUE("00:00"),[Start Date]-1,[Start Date]) 
Type =
VAR a =
    CONVERT ( 'Table'[ShitDate] & " " & TIMEVALUE ( " 06:00" ), DATETIME )
VAR b =
    CONVERT ( 'Table'[ShitDate] & " " & TIMEVALUE ( "17:30" ), DATETIME )
VAR c =
    CONVERT ( 'Table'[ShitDate] & " " & TIMEVALUE ( "18:00" ), DATETIME )
VAR d =
    CONVERT ( 'Table'[ShitDate] + 1 & " " & TIMEVALUE ( "05:59" ), DATETIME )
RETURN
    SWITCH (
        TRUE (),
        [StartTimeDate] >= a
            && [EndTimeDate] <= b, "Day",
        [StartTimeDate] >= c
            && [EndTimeDate] <= d, "Night"
    )

 

2.Then create a measure

 

Duration =
VAR a =
    MINX (
        FILTER (
            ALLSELECTED ( 'Table' ),
            [Machine]
                IN VALUES ( 'Table'[Machine] )
                    && [Type] IN VALUES ( 'Table'[Type] )
        ),
        [StartTimeDate]
    )
VAR b =
    MAXX (
        FILTER (
            ALLSELECTED ( 'Table' ),
            [Machine]
                IN VALUES ( 'Table'[Machine] )
                    && [Type] IN VALUES ( 'Table'[Type] )
        ),
        [EndTimeDate]
    )
RETURN
    DATEDIFF ( a, b, MINUTE )

 

Output

vxinruzhumsft_1-1695178572701.png

 

Best Regards!

Yolo Zhu

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

 

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.