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
nleuck_101
Responsive Resident
Responsive Resident

Find if time is between 2 different times

Hello All,

 

I have a Time table that I calculated with the following DAX:

nleuck_101_0-1693331111207.png

I would like to add a Shift column the states if the time is between these two times it's 1st shift. Or if the time is between these other two times it's 2nd shift. If it's not either of those then there is no shift. Below is the code I'm using:

shift power bi.png

It correctly gets the first shift but does not get second shift at all.

 

Any help would be greatly appreciated.

 

Thanks!

1 ACCEPTED SOLUTION
parry2k
Super User
Super User

@nleuck_101 this:

 

Shift = 
SWITCH ( TRUE (),
  'Time'[TimeandDay] >= TIME ( 8,0,0) &&   'Time'[TimeandDay] < TIME(16,30,0), "Shift 1",
  ( 'Time'[TimeandDay] >= TIME ( 18,30,0) ) || ( 'Time'[TimeAndDay] >= TIME(0,0,0) && 'Time'[TimeAndDay] < TIME (3, 0,0)), "Shift 2",
  "Shift 0"
)


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

14 REPLIES 14
parry2k
Super User
Super User

@nleuck_101 this:

 

Shift = 
SWITCH ( TRUE (),
  'Time'[TimeandDay] >= TIME ( 8,0,0) &&   'Time'[TimeandDay] < TIME(16,30,0), "Shift 1",
  ( 'Time'[TimeandDay] >= TIME ( 18,30,0) ) || ( 'Time'[TimeAndDay] >= TIME(0,0,0) && 'Time'[TimeAndDay] < TIME (3, 0,0)), "Shift 2",
  "Shift 0"
)


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

@parry2k 

Thank you that worked!

parry2k
Super User
Super User

@nleuck_101 I have no idea what you are talking about, give the range in plain simple English (NO DAX)



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

@parry2k 

 

1st shift = 8:00am to 4:30pm
2nd shift = 6:30pm to 3:00am

parry2k
Super User
Super User

@nleuck_101 See attached, in my opinion, it is working

 

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

@parry2k 
But 2nd shift cross over into midnight when the time changes. If I use just use >= TIME(18, 30, 0) at midnight it flips back to 0. 2nd shift needs to go through to 3:00am.

parry2k
Super User
Super User

@nleuck_101 I think you need this:

 

SWITCH ( TRUE (),
  'Time'[TimeandDay] >= TIME ( 3,0,0) &&   'Time'[TimeandDay] < TIME(8,0,0), "Shift 0",
  'Time'[TimeandDay] >= TIME ( 8,0,0) &&   'Time'[TimeandDay] < TIME(16,30,0), "Shift 1",
  'Time'[TimeandDay] >= TIME ( 18,30,0), "Shift 2",
  "Shift 0"
)


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

@parry2k 

Still doesn't work. I only get 1st shift. Everything else is 0.

parry2k
Super User
Super User

@nleuck_101 can you give me the range of your shifts:



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

SWITCH(
    TRUE(),
    'Time'[TimeandDay] >= TIME(8, 0, 0) && 'Time'[TimeandDay] <= TIME(16, 30, 0), 1,
    'Time'[TimeandDay] >= TIME(18, 30, 0) && 'Time'[TimeandDay] <= TIME(3, 0, 0), 2,
    0
)
parry2k
Super User
Super User

@nleuck_101 try this:

 

SWITCH ( TRUE ()
Table[TimeAndDay] >= TIME ( 8,0,0) && Table[TimeAnDay] < TIME(16,0,0), "Shift 1",
Table[TimeAndDay] >= TIME ( 16,0,0) && Table[TimeAnDay] <= TIME(23,59,0), "Shift 2",
"Shift 3"
)


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

@parry2k 
I've tried changing the times around for 2nd shift and it doesn't work.

nleuck_101
Responsive Resident
Responsive Resident

@parry2k 

It's not working. I'm still on getting 1st Shift and 3rd Shift. It's not picking up the 2nd Shift.

parry2k
Super User
Super User

@nleuck_101 you should compare it against timeandday column which is actually time rather than a text column:

 

SWITCH ( TRUE ()
Table[TimeAndDay] >= TIME ( 8,0,0) && Table[TimeAnDay] < TIME(16,0,0), "Shift 1",
Table[TimeAndDay] >= TIME ( 16,0,0) && Table[TimeAnDay] < TIME(0,0,0), "Shift 2",
"Shift 3"
)


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

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.