Where <... underlying dimension ...> is the dimension field (or fields) by which the measure should be calculated
But both calculated dimensions and nested ifs can be performance killers, so if your data set is large, you may want to implement this logic in the load script and create a new field with these values.
I used duals in the expression above so that the values will correctly sort in your chart/table.
Logic will get you from a to b. Imagination will take you everywhere. - A Einstein