It seems like you are working with parent child hierarchy relationship requirement.
If you want to summary records according to this type of relationships, you can take a look at the following link to get the relationship path. Then you can write formula to extract the result path string to get correspond id list and use them as conditions to filter and aggregate records.
Parent-child hierarchies – DAX Patterns
Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.