Thank you Brandon!
Yes, this is something I already was aware. This works well for dimension set with main account only. Thank you for this nice piece of code.
But the problem is more general, because same issues we have for more complex dimension sets: 2 minutes in Ax, 1 second in SQL. Any clue how to fight with that?