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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

M code problem, create column for shift pattern based on parameterised times.

I have this m code for a column:
if Time.From([#"Date/Time"]) >= Time.From(#"Shift Pattern 1 Start") and Time.From([#"Date/Time"]) <= Time.From(#"Shift Pattern 1 End") then "Shift Pattern 1"
else if Time.From([#"Date/Time"]) >= Time.From(#"Shift Pattern 2 Start") and Time.From([#"Date/Time"]) <= Time.From(#"Shift Pattern 2 End") then "Shift Pattern 2"
else if Time.From([#"Date/Time"]) >= Time.From(#"Shift Pattern 3 Start") and Time.From([#"Date/Time"]) <= Time.From(#"Shift Pattern 3 End") then "Shift Pattern 3"
else null

 

 

The times rows that are within shift pattern 1 correctly display "shift pattern 1" in the column, but any other condition returns null. What is the problem with this code?

Any additional info need please comment and i'll get back to you straight away. Thanks!

2 REPLIES 2
j_ocean
Helper V
Helper V

If you're using Time.From I take it things aren't being stored in time or datetime formats, correct? Are you sure shift patterns 2+ are parsing as times correctly?

 

I'd suggest converting the columns to a consistent format in an earlier step and visually confirming how it works. Side benefit: this step will have simpler code.

mlsx4
Memorable Member
Memorable Member

Hi @Anonymous 

 

Have you checked the conditions one by one? I mean just leave this part and check:

 

if Time.From([#"Date/Time"]) >= Time.From(#"Shift Pattern 2 Start") and Time.From([#"Date/Time"]) <= Time.From(#"Shift Pattern 2 End") then "Shift Pattern 2" else null

 

 

It would also be very useful if you attached some example data of your table (with dummy data)

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.