Nolock: I see a comment regarding cyclic relations is not there anymore, so here it is again.
To support cyclic relations, I adjusted the code of the 'fnTransitiveRelationList' part as follows:
// get recursively list of all descendants
//fnTransitiveRelationList = (toBeDoneList as list, directDescendantsRecord as record) as list =>
fnTransitiveRelationList = (toBeDoneList as list, directDescendantsRecord as record, alreadyDoneList as list) as list =>
let
result =
if List.IsEmpty(toBeDoneList) then
{}
else
let
// added by JVos:
alreadyDoneList = List.Combine({alreadyDoneList, toBeDoneList}),
newToBeDoneList = List.RemoveItems(
// combine all lists together
List.Combine(
// get a list of direct descendants for every value
List.Transform(
toBeDoneList,
each Record.FieldOrDefault(directDescendantsRecord, _, {})
)
),
toBeDoneList
//)
),
// added by JVos:
newToBeDoneList1 = List.Difference(newToBeDoneList, alreadyDoneList)
in
//List.Union({toBeDoneList, newToBeDoneList, @fnTransitiveRelationList(newToBeDoneList, directDescendantsRecord)})
List.Union({toBeDoneList, newToBeDoneList1, @fnTransitiveRelationList(newToBeDoneList1, directDescendantsRecord, alreadyDoneList)})
in
result,And in the 'TransitiveClosure' part:
//allRelations = fnTransitiveRelationList({[ROUTING_ID_From]}, DirectDescendantsRecord),
allRelations = fnTransitiveRelationList({[ROUTING_ID_From]}, DirectDescendantsRecord,{}),