Backport of MONDRIAN-2229  (3.8)

Hi, @lucboudreau! Could you review please.
The main issue is here:
https://github.com/pentaho/mondrian/pull/397