Forum Discussion

renny13's avatar
renny13
New Member
3 years ago
Solved

Weekly Duty Schedule in PowerBI

Hi, how do I create a random weekly duty schdule assignment in PowerBI? Or doI have to use Excel? I have 8 groups of people and need to asign 1 person of each group to a week of the year for duty. R...
  • jbwtp's avatar
    jbwtp
    3 years ago

    This is the answer in a simplistic scenario. Groups are autoallocated based on the list of employees (in Source) and a number of groups (in NoOfGroups). Holidays are passed in Holidays.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclSK1YlWcgKTzmDSBUy6gkk3MOkOJj2UYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}}),
        Holidays = let
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyVorViVYy0jeCMY31jcHMWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
            #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}})
        in
            #"Changed Type",
        NoOfGroups = 3,
        SplitToGroups = List.Skip(List.Accumulate(Table.ToRecords(#"Changed Type"), {[GroupID = 0]}, (a, n)=> a & {Record.AddField(n, "GroupID", if List.Last(a)[GroupID] = NoOfGroups then 1 else List.Last(a)[GroupID]+1 )} )),
        Main = Table.FromRecords(SplitToGroups),
        #"Grouped Rows" = Table.Group(Main, {"GroupID"}, {{"Assign", each Table.ExpandTableColumn(Table.NestedJoin(Table.AddIndexColumn(_, "Index", 0, 1, Int64.Type), {"Index"}, Table.AddIndexColumn(Holidays, "Index", 0, 1, Int64.Type), {"Index"}, "Added Index", JoinKind.LeftOuter), "Added Index", {"Date"}, {"Date"})}}),
        #"Expanded Assign" = Table.ExpandTableColumn(#"Grouped Rows", "Assign", {"ID", "Date"}, {"ID", "Date"}),
        #"Removed Columns" = Table.RemoveColumns(#"Expanded Assign",{"GroupID"}),
        Output = Table.TransformColumnTypes(#"Removed Columns",{{"ID", type text}, {"Date", type date}})
    in
        Output

     

    Cheers,

    John