Either helper columns or VBA is usually the answer in these cases, but I don't have enough info to be more explicit.