# Calculation contexts issue with Sum, Where and If

Make a report based on eFashion universe with [Year], [City], and [Sales revenue]. Now we will try to calculate total Sales revenue for Austin in 2004 but in two steps.

## Where and If

Define variables as:

[Sales Austin] =[Sales revenue] Where([City]=”Austin”)

[Sales Austin 2004] =Sum(If [Year]=”2004″ Then [Sales Austin])

The expected value for [Sales Austin 2004] is 561123.

But

1) The value calculated in the Sum line for Sales Austin for some reason is 2805617 (which is 5 x Sales Austin 2004 = 5 x 561123).

2) The value calculated in the table on the right is 13498366 (Which is the total of all values in the column Sales Austin i.e. 5 x (561123 + 1003071 + 1135479) = 13498366). This was a result for BO3.1 SP7. In BO4.1 SP7, the result is slightly different, we will get #MULTIVALUE in the right table.

We can explicitly add [Year] to calculation context.

[Sales Austin 2004] =Sum((If [Year]=”2004″ Then [Sales Austin])ForEach([Year]))

This will fix the value in the right table, but not in the Sum line.

If we explicitly add [Year] and remove [City], then the result will become correct.

[Sales Austin 2004] =Sum((If [Year]=”2004″ Then [Sales Austin])ForEach([Year])ForAll([City]))

or

[Sales Austin 2004] =Sum((If [Year]=”2004″ Then [Sales Austin])In([Year]))

So the calculation context in the variable [Sales Austin 2004] is derived incorrectly. Since we have [City] in the expression, it should have been included by BO. The variable [Sales Austin] has Where([City]=”Austin”), which removes [City] from context, so [City] should have been excluded. But for some reason, we have to specify this manually.

## If

[Sales Austin] =If [City]=”Austin” Then [Sales revenue]

[Sales Austin 2004] =Sum(If [Year]=”2004″ Then [Sales Austin])

This works fine unless we add sum to [Sales Austin]:

[Sales Austin] =Sum(If [City]=”Austin” Then [Sales revenue])

[Sales Austin 2004] =Sum(If [Year]=”2004″ Then [Sales Austin])

In this case, the right table will show wrong value (13498366 in BO3 and #MULTIVALUE in BO4). We need to add [Year] to calculation context in [Sales Austin 2004].

## Where

[Sales Austin] =[Sales revenue] Where ([City]=”Austin”)

[Sales Austin 2004] =[Sales Austin] Where ([Year]=”2004″)

The result will be correct if we use variable value [Sales Austin 2004] in Sum line. If we add Sum, the result in Sum line will become wrong 2805617.

You might also find it interesting that the following two formulas return different result

=Sum([Sales revenue] Where ([Year]=”2004″ And [City]=”Austin”))

=Sum([Sales revenue] Where ([Year]=”2004″)Where([City]=”Austin”))

The first is correct but the second will return 1683370. (I am not sure what this number is.)