Power Query - Creating a Custom Column to show Text based on Times

HCW1966

New Member
Joined
May 6, 2016
Messages
20
Hi There,

Please i want to create a custom column based on the "Stop Time" column below. This column has approx 1,000 rows of information.

What i want to show on the Custom Column is

"Early Shift" if the stop time is between 07:00:00 and 14:59:59
"Back Shift" if the stop time is between 15:00:00 and 22:29:59
"Night Shift" if the stop time is between 22:30:00 and 06:59:59

The above is my primary question, however i have a secondary question, is it possible to create another column showing the Day of the Week (Sun, Mon, Tue..........) that corresponds to the StopTime column date.

Kind Regards Harry

1602675713717.png
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
you cannot use xl2bb in Power Query editor
post link to the shared excel file with your example
or
load example to the sheet, select whole range then use xl2bb and post here
 
Upvote 0
you cannot use xl2bb in Power Query editor
post link to the shared excel file with your example
or
load example to the sheet, select whole range then use xl2bb and post here
PQ Report - WK42.xlsx
B
2StartTime
Sheet1



I tried it again, basically its the same data as my original post, only its in a table? I cannot link any file, as my employer doesnt allow
 
Upvote 0
you need to select range of your data first then use xl2bb ;)
Fingers Crossed

PQ Report - WK42.xlsx
BC
2StartTimeStopTime
311/10/2020 06:0111/10/2020 07:31
411/10/2020 06:0211/10/2020 09:09
511/10/2020 06:0311/10/2020 08:01
611/10/2020 06:0611/10/2020 08:04
711/10/2020 06:0611/10/2020 07:26
811/10/2020 07:2611/10/2020 09:29
911/10/2020 07:3111/10/2020 08:56
1011/10/2020 08:0211/10/2020 09:39
1111/10/2020 08:0711/10/2020 08:38
1211/10/2020 08:3811/10/2020 11:01
1311/10/2020 08:5611/10/2020 10:32
1411/10/2020 09:1011/10/2020 12:41
1511/10/2020 09:3011/10/2020 11:37
1611/10/2020 09:3911/10/2020 10:56
1711/10/2020 10:4411/10/2020 12:18
1811/10/2020 10:5611/10/2020 12:54
1911/10/2020 11:3711/10/2020 12:55
2011/10/2020 12:1911/10/2020 13:13
2111/10/2020 12:4111/10/2020 12:57
2211/10/2020 21:3112/10/2020 00:05
2311/10/2020 21:3111/10/2020 23:06
2411/10/2020 21:3112/10/2020 01:09
2511/10/2020 21:3211/10/2020 23:09
2611/10/2020 21:3312/10/2020 01:48
2711/10/2020 21:3411/10/2020 22:44
2811/10/2020 21:4111/10/2020 22:52
2911/10/2020 21:4112/10/2020 01:30
3011/10/2020 21:4811/10/2020 23:10
3111/10/2020 22:4412/10/2020 03:12
3211/10/2020 22:5212/10/2020 00:36
3311/10/2020 23:0611/10/2020 23:50
3411/10/2020 23:1012/10/2020 01:48
3511/10/2020 23:1112/10/2020 00:59
3611/10/2020 23:5012/10/2020 01:19
3712/10/2020 00:0512/10/2020 00:30
3812/10/2020 00:3112/10/2020 02:30
3912/10/2020 00:4112/10/2020 01:20
4012/10/2020 00:5912/10/2020 06:49
4112/10/2020 01:1312/10/2020 01:13
4212/10/2020 01:1312/10/2020 02:23
4312/10/2020 01:2112/10/2020 01:42
4412/10/2020 01:2912/10/2020 01:58
4512/10/2020 01:4312/10/2020 02:46
4612/10/2020 01:4812/10/2020 03:04
4712/10/2020 01:5112/10/2020 03:24
4812/10/2020 01:5912/10/2020 06:51
4912/10/2020 03:0212/10/2020 03:46
5012/10/2020 03:0212/10/2020 03:50
Sheet1
 
Upvote 0
try
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    DT = Table.TransformColumnTypes(Source,{{"StartTime", type datetime}, {"StopTime", type datetime}}),
    DayName = Table.AddColumn(DT, "Day Name", each Date.DayOfWeekName([StopTime]), type text),
    Time = Table.TransformColumnTypes(DayName,{{"StopTime", type time}}),
    Conditions = Table.AddColumn(Time, "Shift", each if [StopTime] >= #time(22, 30, 0) or [StopTime] <= #time(6, 59, 59) then "Night Shift"  else if [StopTime] >= #time(15, 0, 0) or [StopTime] <= #time(22, 29, 59) then "Back Shift" else if [StopTime] >= #time(7, 0, 0) or [StopTime] <= #time(14, 59, 59) then "Early Shift"  else null)
in
    Conditions
 
Upvote 0
or this
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    DT = Table.TransformColumnTypes(Source,{{"StartTime", type datetime}, {"StopTime", type datetime}}),
    DayName = Table.AddColumn(DT, "Day Name", each Date.DayOfWeekName([StopTime]), type text),
    Time = Table.TransformColumnTypes(DayName,{{"StopTime", type time}}),
    Conditions = Table.AddColumn(Time, "Shift", each if [StopTime] >= #time(22, 30, 0) or [StopTime] <= #time(6, 59, 59) then "Night Shift"  else if [StopTime] >= #time(7, 0, 0) or [StopTime] <= #time(14, 59, 59) then "Early Shift"  else if [StopTime] >= #time(15, 0, 0) or [StopTime] <= #time(22, 29, 59) then "Back Shift" else null)
in
    Conditions
if something goes wrong change ifs order in Conditions step
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top