Sum Distinct Aggreration in a multi-dimensional model

I am working with a multi-dimensional star-schema



and I need a special aggregation function to sum without duplicates



I can see how this is done with SQL but not sure what is the best approch to implement it with Mondrian.


UPDATE: I was able to create the measure with relativly simle MDX. Code is attached.