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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
Peter_2020
Helper III
Helper III

DATEDIFF with specific conditions

Hi all, I would like to ask you for help with following situation:

 

I have the table with the list of the product numbers and for each product I have START and END date/time. And I want to calculate duration between Start and End date/time but without weekends and I am working only on 2 shifts - it means from 6:00 - 14:00 first shift and 14:00 - 22:00 second shift. So time period 22:00 - 6:00 must be out of the calculation.

 

Here is example:

Peter_2020_0-1700201657481.png

62 hours is calculated using formula with DATEDIFF, but it calculate time including weekend and 24 hours per day. By using proper formula, expected result is - 6 hours.

 

10.11.2023 - 18:00 - 22:00 - 4hours

11.11.2023 - weekend - 0 hours

12.11.2023 - weekend - 0 hours

13.11.2023 - 6:00 - 8:00 - 2 hours

 

Any idea how to do this?

Thank you in advance!

P.

1 ACCEPTED SOLUTION

@Peter_2020 

I just got some time so I re wrote the formula with break time and based on minute calcualtion. please check the attached file:

 

Production Time = 
VAR __SHIFTSTART = TIME( 6,0,0 )      -- Provide shift start hour 
VAR __SHIFTEND = TIME( 22,0,0 )       -- Provide shift end hour
VAR __START = Production[Start]
VAR __END =  Production[End]
VAR __PERIOD =  
	FILTER( 
		GENERATESERIES( __START, __END,  1/24/60 ) , 
		VAR __TimeVal = MOD( [Value] , 1 ) RETURN
		NOT WEEKDAY( [Value] , 1) IN {1,7} 		
		&&   __TimeVal >=  __SHIFTSTART  && __TimeVal <=  __SHIFTEND
		&&
           ISEMPTY( 
                FILTER(
                    Breaks,
                    AND( 
                        __TimeVal >= Breaks[Break Start] , __TimeVal <= Breaks[Break End]
                    )
                )
            )		
	) 
VAR __TOTALMIN = COUNTROWS( __PERIOD )
VAR __RESULT = 
    FORMAT( INT( DIVIDE(__TOTALMIN , 60) ) , "00:" ) & FORMAT(  MOD(__TOTALMIN , 60 ) , "00" )
RETURN 
    __RESULT    

 

 

 



Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

5 REPLIES 5
Fowmy
Super User
Super User

@Peter_2020 

Add a calculated column using the DAX code below:

Produnction Time = 
VAR __SHIFTSTART = 6      -- Provide shift start hour 
VAR __SHIFTEND = 22       -- Provide shift end hour
VAR __START = Table1[Start]
VAR __END =  Table1[End] -  TIME(1,0,0)
VAR __SHIFTPERIOD = GENERATESERIES( __SHIFTSTART , __SHIFTEND - 1  , 1 )
VAR __PERIOD =  FILTER( GENERATESERIES( __START, __END,  TIME(1,0,0) ) , NOT WEEKDAY( [Value] , 1 ) IN {1,7} )

RETURN
    COUNTROWS(
        FILTER(
            __PERIOD,
            HOUR( [Value] ) IN  __SHIFTPERIOD
        )
    )

Fowmy_0-1700249151530.png

 



Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

@Fowmy Thank you so much! This is exactly what I needed.

 

But I have one more challenge - is it possible to define the breaks during the shift? Example: BREAK1 - 9:00 - 9:30, BREAK2 - 16:00 - 16:10 and that time will be of course out of the calculated production time...

I tried to add this into the formula but I don´t know how to define minutes and how will looks second part of the formula:

 

PRODUCTION TIME2 =
VAR __SHIFTSTART = 6
VAR __SHIFTEND = 22  
VAR __SHIFTBREAKSTART1 = 9
VAR __SHIFTBREAKEND1 = 10  
VAR __SHIFTBREAKSTART2 = 16
VAR __SHIFTBREAKEND2 = 17  
VAR __START = Table[Start date/time]
VAR __END =  Table[End date/time] -  TIME(1,0,0)
VAR __SHIFTPERIOD = GENERATESERIES( __SHIFTSTART , __SHIFTEND - 1  , 1 )
VAR __SHIFTBREAKPERIOD1 = GENERATESERIES( __SHIFTBREAKSTART1 , __SHIFTBREAKEND1 - 1  , 1 )
VAR __SHIFTBREAKPERIOD2 = GENERATESERIES( __SHIFTBREAKSTART2 , __SHIFTBREAKEND2 - 1  , 1 )
VAR __PERIOD =  FILTER( GENERATESERIES( __START, __END,  TIME(1,0,0) ) , NOT WEEKDAY( [Value] , 1 ) IN {1,7} )

RETURN
    COUNTROWS(
        FILTER(
            __PERIOD,
            HOUR( [Value] ) IN  {__SHIFTPERIOD,__SHIFTBREAKPERIOD1,__SHIFTBREAKPERIOD2}
        )
    )
 
Thank you for your help!
P.

@Peter_2020 

I just got some time so I re wrote the formula with break time and based on minute calcualtion. please check the attached file:

 

Production Time = 
VAR __SHIFTSTART = TIME( 6,0,0 )      -- Provide shift start hour 
VAR __SHIFTEND = TIME( 22,0,0 )       -- Provide shift end hour
VAR __START = Production[Start]
VAR __END =  Production[End]
VAR __PERIOD =  
	FILTER( 
		GENERATESERIES( __START, __END,  1/24/60 ) , 
		VAR __TimeVal = MOD( [Value] , 1 ) RETURN
		NOT WEEKDAY( [Value] , 1) IN {1,7} 		
		&&   __TimeVal >=  __SHIFTSTART  && __TimeVal <=  __SHIFTEND
		&&
           ISEMPTY( 
                FILTER(
                    Breaks,
                    AND( 
                        __TimeVal >= Breaks[Break Start] , __TimeVal <= Breaks[Break End]
                    )
                )
            )		
	) 
VAR __TOTALMIN = COUNTROWS( __PERIOD )
VAR __RESULT = 
    FORMAT( INT( DIVIDE(__TOTALMIN , 60) ) , "00:" ) & FORMAT(  MOD(__TOTALMIN , 60 ) , "00" )
RETURN 
    __RESULT    

 

 

 



Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

 @Fowmy it works perfectly. Thank you so much for your help!

Ahmedx
Super User
Super User

share an example with several products

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors