Forum Discussion
sunny18pc
2 years agoRegular Visitor
Power Bi Attendance Calculation (mom wise)
Hi All, Please help me calculate the attendance in power bi, Joing Date= when a employee joins the company LWD=when a employoess leaves Active Stage=Active/Inactive, Active=employee is s...
dufoq3
2 years agoCommunity Champion
Hi sunny18pc, it is also possible in Power Query, but of course - DAX has better performance:
Result
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjLVNTDXNTIwMlbSUTIy1zWwhHBiddDlzHQNzEAcE6XYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Joining = _t, LWD = _t]),
ChangedType = Table.TransformColumnTypes(Source,{{"Joining", type date}, {"LWD", type date}}),
Helper = [ minDate = List.Min(ChangedType[Joining] & ChangedType[LWD]),
maxDate = List.Max(ChangedType[Joining] & ChangedType[LWD]),
dates = List.Dates(minDate, Duration.TotalDays(maxDate-minDate)+1, #duration(1,0,0,0)),
months = List.Buffer(List.Distinct(List.Transform(dates, (x)=> Date.Year(x)*100 + Date.Month(x)))),
monthsFormatted = List.Buffer(List.Transform(months, (y)=> Date.ToText(Date.From(Text.From(y) & "01"), [Format="MMM yy", Culture="en-US"]))) ],
StepBack = ChangedType,
Ad_MonthsList = Table.AddColumn(StepBack, "Months", each List.Distinct(List.Transform(List.Dates([Joining], Duration.TotalDays([LWD]-[Joining])+1, #duration(1,0,0,0)), (x)=> Date.Year(x)*100 + Date.Month(x))), type list),
Ad_Months = List.Accumulate(
List.Zip({Helper[months], Helper[monthsFormatted]}),
Ad_MonthsList,
(s,c)=> Table.AddColumn(s, c{1}, (x)=> if List.Contains(x[Months], c{0}) then 1 else 0, Int64.Type)
),
RemovedColumns = Table.RemoveColumns(Ad_Months,{"Months"})
in
RemovedColumns