Hi @Bahalzamon :
I used ForALL's multiple nesting, please check whether the scheme meets your needs.
I've made a test for your reference:
1\My data source:(Table)
ClearCollect(Table,{ID:1,Tier:1,Flag:1,ParentID:0},{ID:2,ParentID:0},{ID:3,ParentID:1},{ID:4,ParentID:2},{ID:5,ParentID:3},{ID:6,ParentID:4})
2\Add a button and set it's OnSelect proeprty to:
ClearCollect(
TheTable1, /*TheTable1 is my custom collecion,Its role is to sort the data sources initially*/
SortByColumns(
Table,
"ParentID",
Ascending,
"ID",
Ascending
)
);
ForAll(
[1,2,3,4,5,6,7,8,9], /*Search records with ParentID=0, loop 9 times (if the number of eligible records may be greater than 9, please increase the number of loops)*/
If(
CountRows(
Filter(
TheTable1,
ParentID = 0
)
) >= Value,
Collect(
Table2,
{
ID: Last(
FirstN(
Filter(
TheTable1,
ParentID = 0
),
Value
)
).ID,
ParentID: Last(
FirstN(
Filter(
TheTable1,
ParentID = 0
),
Value
)
).ParentID,
Tier: Last(
FirstN(
Filter(
TheTable1,
ParentID = 0
),
Value
)
).Tier,
Flag: Last(
FirstN(
Filter(
TheTable1,
ParentID = 0
),
Value
)
).Flag
}
);
ForAll(
[0,1,2,3,4,5,6,7,8,9],/*Search child records, loop 9 times (the number of child records of a record with parentID=0 may be greater than 9, please increase the number of cycles)*/
ForAll(
TheTable1,
If(
ParentID = Last(Table2).ID,
Collect(
Table2,
{
ID: ID,
ParentID: ParentID,
Tier: Tier,
Flag: Flag
}
)
)
)
)
)
)/*Finally, the sorted records are stored in Table2*/

Best Regards,
Bof