let
// list of slots
tblSlotting = Excel.CurrentWorkbook(){[Name="tblSlotting"]}[Content],
s = Table.TransformColumns(
Table.SelectRows(tblSlotting, (x) => x[Slots] > 0),
{"Slots", (x) => List.Repeat({null}, x)}
),
slots = List.Buffer(Table.ExpandListColumn(s, "Slots")[WeekMonday]),
// products
Table1 = Table.Sort(Excel.CurrentWorkbook(){[Name="Table1"]}[Content], "ReadyToStart"),
prod = List.Buffer(Table1[WeekMonday]),
// use slots
find_slot = (k, skp) =>
[
i = k,
pos = List.PositionOf(List.Skip(slots, skp), prod{i}, Occurrence.First, (c, v) => c >= v),
slot_position = pos + skp,
skip = slot_position + 1
],
gen = List.Generate(
() => find_slot(0, 0),
(x) => x[i] < List.Count(prod) and x[pos] <> -1,
(x) => find_slot(x[i] + 1, x[skip]),
(x) => slots{x[slot_position]}
),
result = Table.FromList(
List.Zip(Table.ToColumns(Table1) & {gen}),
(x) => {x{0}, if x{3} is null then "no slot found" else if x{2} = x{3} then x{1} else x{3}},
{"Item", "StartDate"}
)
in
result