Hi @hafizsultan,
Good catch on the condition
Love it when I see people dive into the M code, figuring out what is going on…
I’ve amended the function fxSplitLeaveHours further and added a new column to the output table. The condition in now > -8 so we can also catch partial leave days taken.
( myStartDate as date, myEndDate as date, myLeave as number, myHolidayList as list ) as table =>
let
// load the Holidays into memory
HolidayList = List.Buffer(myHolidayList),
DateRange =
List.Buffer(
List.Select(
List.Difference(
List.Dates(myStartDate, Number.From(myEndDate - myStartDate) +1, Duration.From(1)),
List.Transform(HolidayList, Date.From )),
each Date.DayOfWeek(_, 1) < 5 )
),
GenerateList = List.Generate(
// Create an initial Record
() => [Date = DateRange{0}, hLeave = myLeave-8, i = 0, h = if myLeave >8 then 8 else myLeave],
// Define the Condition (Do-While-Loop)
each [hLeave] >-8 and List.Count(DateRange)-1 >= [i],
// Function that defines a new value for each Record element in that iteration.
each [Date = DateRange{i}, i = [i] +1, hLeave = [hLeave]-8, h = if hLeave <0 then hLeave+8 else 8]
),
CreateTable = Table.FromRecords(GenerateList),
AddMonthColumn = Table.AddColumn(CreateTable, "Month", each Text.Proper( Date.MonthName([Date])) & ", " & Text.From(Date.Year([Date])), type text),
GroupRows = Table.Group(AddMonthColumn, {"Month"}, {{"Leave Hours p/m", each List.Sum([h]), type number}}),
AddCheckHours = Table.AddColumn(GroupRows, "Check Hours", each List.Sum(GroupRows[#"Leave Hours p/m"]), Int64.Type)
in
try AddCheckHours otherwise #table( {}, {} )
.
Added a FileLocation parameter to your file just select yours from the list and the queries will restore. Here’s the updated file. eDNA - Leave Hours.pbix (84.7 KB)
I hope this is helpful.