Forum Discussion
Create time groups/shifts
- 1 year ago
You want to have at least two separate dimension tables, one for dates and another for time. See the following guidance if you are unfamiliar with dimension and fact tables: https://learn.microsoft.com/en-us/power-bi/guidance/star-schema
Once you have your separate time dimension set up, you can add a Shift column (either in Power Query with M or in modeling with DAX) that will assign the shift according to your logic.
Once relationships are set up, you can then group/filter your fact/transactional data by shifts from your time dimesnsion. You would be able to do something like add Shifts to one axis of a visual and add SUM or some other aggregation of your fact to the other axis.
If you want more specific help on certain transformations, etc., please provide a clear description of your desired output and some dummy data pasted into a table that we can easily copy and paste into PBI through Enter Data.
- 1 year ago
Good day Coops_15,
One way of doing this is to calculate the hour of day of each "TIME OF Event" and use a look up list to find the shift for that hour.
For example, create the lookup list by adding a step called "Shifts" - each entry corresponds to an hour of the day.Shifts = List.Repeat({"Night"},5) & List.Repeat({"Morning"},8) & List.Repeat({"Afternoon"},8) & List.Repeat({"Night"},3)then add a step to add a column (replace "Previous Step" with the name of step which got you to the table you illustrated). In this step the hour of the timestamp acts as an index into the list. It picks out the shift name at that index.
AddShifts = Table.AddColumn(#"Previous Step", "Shift", each Shifts{Time.Hour([TIME OF Event])}, type text)Here is some sample data and the complete code to allocated each time to a shift - you can create a blank query and paste this code in. The sample data looks like this,
The result looks like this
...and here is the code
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjK0MjWwMjBQitWJVjIwsjKCcwwtrYyROYamcGUWQBZcxgimLBYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"TIME OF Event" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"TIME OF Event", type time}}),
Shifts = List.Repeat({"Night"},5) & List.Repeat({"Morning"},8) & List.Repeat({"Afternoon"},8) & List.Repeat({"Night"},3),
AddShifts = Table.AddColumn(#"Changed Type", "Shift", each Shifts{Time.Hour([TIME OF Event])}, type text)
in
AddShiftsWith this done you can load the data and count the rows, stratifying by the name of the shift.
Hope this helps
Good day Coops_15,
One way of doing this is to calculate the hour of day of each "TIME OF Event" and use a look up list to find the shift for that hour.
For example, create the lookup list by adding a step called "Shifts" - each entry corresponds to an hour of the day.
Shifts = List.Repeat({"Night"},5) & List.Repeat({"Morning"},8) & List.Repeat({"Afternoon"},8) & List.Repeat({"Night"},3)
then add a step to add a column (replace "Previous Step" with the name of step which got you to the table you illustrated). In this step the hour of the timestamp acts as an index into the list. It picks out the shift name at that index.
AddShifts = Table.AddColumn(#"Previous Step", "Shift", each Shifts{Time.Hour([TIME OF Event])}, type text)
Here is some sample data and the complete code to allocated each time to a shift - you can create a blank query and paste this code in. The sample data looks like this,
The result looks like this
...and here is the code
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjK0MjWwMjBQitWJVjIwsjKCcwwtrYyROYamcGUWQBZcxgimLBYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"TIME OF Event" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"TIME OF Event", type time}}),
Shifts = List.Repeat({"Night"},5) & List.Repeat({"Morning"},8) & List.Repeat({"Afternoon"},8) & List.Repeat({"Night"},3),
AddShifts = Table.AddColumn(#"Changed Type", "Shift", each Shifts{Time.Hour([TIME OF Event])}, type text)
in
AddShifts
With this done you can load the data and count the rows, stratifying by the name of the shift.
Hope this helps