Skip to content

Instantly share code, notes, and snippets.

@bjulius
Created September 2, 2023 07:16
Show Gist options
  • Save bjulius/a1d06b5b5d1e0cfc25e4b7fd4584e1a4 to your computer and use it in GitHub Desktop.
Save bjulius/a1d06b5b5d1e0cfc25e4b7fd4584e1a4 to your computer and use it in GitHub Desktop.
Excel BI Power Query Challenge 108
let
Source = Table.TransformColumnTypes( Excel.CurrentWorkbook(){[Name="Table1"]}[Content], {"Date Time", type datetime}),
Sort = Table.Sort(Source,{{"Emp ID", Order.Ascending}, {"Date Time", Order.Ascending}}),
Group = Table.Group(Sort, {"Emp ID"}, {{"Count", each Table.RowCount(_), Int64.Type}, {"All", each _, type table [Emp ID=nullable number, Date Time=nullable datetime, Category=nullable text]}, {"MaxDateTime", each List.Max([Date Time]), type nullable datetime}}),
AddIndex = Table.RemoveColumns( Table.AddColumn(Group, "Index", each Table.AddIndexColumn([All], "Idx", 0, 1)), "All"),
AddModulo = Table.RemoveColumns( Table.AddColumn(AddIndex, "Modulo", each Table.AddColumn( [Index], "Mod2", each Number.Mod([Idx], 2))), "Index"),
Expand = Table.ExpandTableColumn(AddModulo, "Modulo", {"Emp ID", "Date Time", "Category", "Idx", "Mod2"}, {"Emp ID.1", "Date Time", "Category", "Idx", "Mod2"}),
AddProblems = Table.AddColumn(Expand, "Problems", each
[
a = if Number.IsOdd( [ Count ] ) then 1 else 0,
b = if DateTime.From( [Date Time] ) = [MaxDateTime] then 1 else 0,
c = a + b,
d = if ( [Category] = "Entry" ) and ( [Mod2] <> 0 ) then 1 else
if ( [Category] = "Exit" ) and ( [Mod2] <> 1 ) then 1 else 0,
e = if c = 2 or d = 1 then 1 else 0
][e]),
Filter = Table.SelectColumns( Table.SelectRows(AddProblems, each ([Problems] = 1)), {"Emp ID", "Date Time", "Category"})
in
Filter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment