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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Tarun-Bhatia
Regular Visitor

Creating a Shift Column from Date-Time

Hi There

 

Need help in creating Shift column in PowerBi Desktop using Date-Time(READY_DATETIME) column.

Shift 1: 6am - 2pm

Shift 2: 2pm - 10pm

Shift 3: 10pm - 6am

 

 

Tarun-Bhatia_0-1602553084354.png

 

Any help would be greatly appreciated.

 

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Tarun-Bhatia ,

Try a new column as

New column ?
var _hr á hour([readydatetime])
Return
switch(True(),
_hr<6 && _hr >22 , "Shift 3",
_hr < 14 , "Shift 2",
"Shift 1"
)

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

3 REPLIES 3
edhans
Super User
Super User

Hi @Tarun-Bhatia - can you push this back into Power Query? Custom columns there will generally perform better than calculated columns in DAX. See links below for explanation.

 

If you can, use this formula in a new Custom Column in Power Query:

let
   varTime = DateTime.Time([Date])
in
if varTime >= #time(6,0,0) and varTime < #time(14,0,0) then "Shift 1"
else if varTime >= #time(14,0,0) and varTime < #time(22,0,0) then "Shift 2"
else "Shift 3"

It returns this:

edhans_0-1602600258880.png

You'd then just need to change the data type of the [Shift] column to Text before closing and loading to the Data Model. The varTime variable I created just simplifies data entry. You could also just type this:

if DateTime.Time([Date]) >= #time(6,0,0) and DateTime.Time([Date]) < #time(14,0,0) then "Shift 1"
else if DateTime.Time([Date]) >= #time(14,0,0) and DateTime.Time([Date]) < #time(22,0,0) then "Shift 2"
else "Shift 3"

Same result, just slightly harder to read and edit imho.

 

In general, try to avoid calculated columns. There are times to use them, but it is rare. Getting data out of the source system, creating columns in Power Query, or DAX Measures are usually preferred to calculated columns. See these references:
Calculated Columns vs Measures in DAX
Calculated Columns and Measures in DAX
Storage differences between calculated columns and calculated tables
SQLBI Video on Measures vs Calculated Columns

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
amitchandak
Super User
Super User

@Tarun-Bhatia ,

Try a new column as

New column ?
var _hr á hour([readydatetime])
Return
switch(True(),
_hr<6 && _hr >22 , "Shift 3",
_hr < 14 , "Shift 2",
"Shift 1"
)

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Thanks @amitchandak 

 

I guess small issue here, Its not showing correct Shifts based on hour (Shift 1 should be between 6am - 2pm, Shift 2 between 2pm - 10pm).

Wondering If thats because ready_datetime column shows AM/PM Instead of 24 hour?

 

Tarun-Bhatia_0-1602558681865.png

Thanks

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

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

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.