How to create a Headcount by Hour for employees of multiple positions using Power Query to Automatically refresh data.

mtfedder

New Member
Joined
Aug 30, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I have data for a couple hundred employees each day mainly position, date, shift start, shift end. I need to create a bar chart showing the total count by position per hour. The part I am having trouble with is getting excel to recognize shift start and shift end as a time range and to count once in each hour. I am also trying to do this through power query to auto-update the data each day we update shift information. Can anyone help? I have come across "countIF" statements for existing tables, but couldn't translate it to my dataset in power query. Any suggestions? (Asking as a bit of a novice so explain each step if you could)
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
if data is like this (pivot is the result of the PQ)
Book1
ABCDEFGHIJKLMNOP
1
2
3namepositiondateshift startshift endCount of Unique Hrs WorkedColumn Labels
4aPx2/09/20212:42:037:04:04Row Labels1/09/20212/09/20213/09/20214/09/20215/09/20216/09/2021
5aPx3/09/20212:02:268:02:5601111
6aPx1/09/20217:32:4211:29:2411111
7aPx5/09/20215:03:0011:38:472111111
8bPx5/09/20211:42:368:53:173111111
9cPx5/09/202115:56:2522:45:08411111
10bPx3/09/20213:31:188:46:46511111
11cPx1/09/202119:43:433:23:29611111
12aPx4/09/20213:23:438:17:21711111
13bPx2/09/202113:08:0918:48:05811111
14cPx5/09/202121:41:493:53:3191111
15cPx4/09/20217:38:4212:09:01101111
16cPx3/09/202115:26:2923:01:38111111
17dPx5/09/20215:32:2211:05:441211
18ePx5/09/20214:12:098:21:11131
19ePx3/09/202123:48:415:13:13141
20bPx1/09/202117:39:4823:05:5915111
21dPx4/09/202119:02:131:22:0616111
22cPx2/09/20215:05:5312:41:05171111
23dPx1/09/20212:05:066:57:24181111
24191111
25201111
26211111
27221111
28231111
29Grand Total17191618214
30
31
Sheet1


then this query might work
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"name", type text}, {"position", type text}, {"date", type datetime}, {"shift start", type time}, {"shift end", type time}}),
    OverNight = Table.AddColumn(#"Changed Type", "OverNightShift", each [shift end]<[shift start]),
    GetHrs = Table.AddColumn(OverNight, "Hours Worked", each let
 Hrs1 = if [OverNightShift] = false then {{Time.Hour([shift start]) .. Time.Hour([shift end])}, {}} else
 { {Time.Hour([shift start]) .. 23}
 , {0 .. Time.Hour([shift end]) } }
in
Hrs1),
    HrsOnDate = Table.AddColumn(GetHrs, "HrsOnDate", each [Hours Worked]{0}),
    HrsOnNextDate = Table.AddColumn(HrsOnDate, "HrsOnNextDate", each [Hours Worked]{1}),
    AddNextDate = Table.AddColumn(HrsOnNextDate, "NextDate", each if [OverNightShift] = true then Date.AddDays([date],1) else null, Date.Type),
    ExtractHrs1 = Table.TransformColumns(AddNextDate, {"HrsOnNextDate", each Text.Combine(List.Transform(_, Text.From), ";"), type text}),
    ExtractHrs2 = Table.TransformColumns(ExtractHrs1, {"HrsOnDate", each Text.Combine(List.Transform(_, Text.From), ";"), type text}),
    Remove1 = Table.RemoveColumns(ExtractHrs2,{"Hours Worked", "HrsOnDate", "date"}),
    Day2Table = Table.Buffer(Table.RenameColumns(Remove1,{{"NextDate", "date"}, {"HrsOnNextDate", "HrsOnDate"}})),
    Day1Table = Table.Buffer(Table.RemoveColumns(ExtractHrs2,{"Hours Worked", "HrsOnNextDate", "NextDate"})),
    TableD1D2 = Table.Combine({Day1Table,Day2Table}),
    #"Filtered Rows" = Table.SelectRows(TableD1D2, each ([date] <> null)),
    #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Filtered Rows", {{"HrsOnDate", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "HrsOnDate"),
    #"Grouped Rows" = Table.Group(#"Split Column by Delimiter", {"date"}, {{"AllDay", each _, type table [name=nullable text, position=nullable text, date=datetime, shift start=nullable time, shift end=nullable time, OverNightShift=logical, HrsOnDate=nullable text]}}),
    GetUniqueHrsPerDay = Table.AddColumn(#"Grouped Rows", "Unique Hrs Worked", each List.Sort(List.Distinct([AllDay][HrsOnDate]))),
    RemoveGroupBy = Table.RemoveColumns(GetUniqueHrsPerDay,{"AllDay"}),
    #"Expanded Unique Hrs Worked" = Table.ExpandListColumn(RemoveGroupBy, "Unique Hrs Worked"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Unique Hrs Worked",{{"Unique Hrs Worked", Int64.Type}, {"date", type date}})
in
    #"Changed Type1"
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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