Backport of MONDRIAN-2229  (3.8)

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