Sometimes it is necessary to implement divisions using MDX. And what happens when the divisor is a zero or a null? What to do? Well many bloggers and Mosha have already handled that. Although in special cases you are not able to use this straightaway. An example:
You want to calculate some values, using MDX that is generated automatically, depending on other values. Furthermore you have formulas that use division, and those are created dynamically. Something like ((A+B)*0.5/(C+D)*0.5)*0.5 whereas {A,B,C,D} are values in cube.
So what to do? At first it’s necessary to know what the result of a division by zero or null is. Well this is easy to test. Listing 1 shows the results from a mdx query that use the calculation, whereas x > 0.
| Calculation | Results | How to handle it |
| 0 / NULL | 1.#INF | [Measures].[Formula] > 1 |
| NULL / NULL | NULL | [Measures].[Formula] = 0 |
| 0 / 0 | -1.#IND | [Measures].[Formula] = 0 |
| NULL / 0 | NULL | [Measures].[Formula] = 0 |
| X / NULL | 1.#INF | [Measures].[Formula] > 1 |
| NULL / X | NULL | [Measures].[Formula] = 0 |
| X / 0 | 1.#INF | [Measures].[Formula] > 1 |
| 0 / X | 0 | [Measures].[Formula] = 0 |
| ? | -1.#INF | [Measures].[Formula] < -1 |
You see that sometimes the results are like ±1.#INF (infinity) or ±1.#IND (not defined). Once you are able to handle the formula as a member you may check if this is in the range -1 <= 0 <= 1. An MDX Statement could be something like:
iif([Measures].[Formula] < -1 or [Measures].[Formula] > 1 or [Measures].[Formula] = 0, null ,[Measures].[Formula]))
You may want to test it with:
WITH
MEMBER [Measures].[Formula] as 1/0
MEMBER [MEMBER_NAME] AS
(
iif([Measures].[Formula] < 1 or [Measures].[Formula] > 1 or
[Measures].[Formula] = 0, null ,[Measures].[Formula])
)
select {[MEMBER_NAME],[Measures].[Formula]} on 0 from [CUBE_NAME]
