Question: I would like to use the old value of a cell to calculate its new value (hence, the equivalent of the '+=' operator in programming languages) while using excel solver.

How can I achieve that ? What do I have to write in the cell ?

Thanks.

EDIT : as it seems the question is not that clear, I will expand it. So here is a picture summarizing what I want to accomplish :

[![enter image description here](https://i.stack.imgur.com/PrzDE.png)](https://i.stack.imgur.com/PrzDE.png) 

Now, imagine the objective of the excel solver is defined on the sum cell and the variable of the solver is x. The goal could be something like: find the maximum value of x so that the sum is inferior to an upper boundary. 

X is incremented steadily and each time a new x is used, a new value f(x) has to be calculated. 

The **sum** cell is the sum of the past value + the newly calculated f(x) i.e.

* first iteration: sum = f(0)
* second iteration: sum = f(0) + f(1),
* third iteration: sum = f(0) + f(1) + f(2) ; and so on.

I would like to know how to write the formula in the sum cell. I would preferably use only one cell for that purpose if possible. Hope this clarifies everything.

Answer: 
You just need to group by `agentId` and month\_year:

```
SELECT 
  AgentID,
  FORMAT(CompletedDate,'MMMM yyyy') as MonthYear,
  COUNT(CASE WHEN TypeID=50 then 1 ELSE NULL END) as "Type1",
  COUNT(CASE WHEN  TypeID=51 then 1 ELSE NULL END) as "Type2",
  COUNT(*) AS Total
FROM RAW_DATA
group by  AgentID, FORMAT(CompletedDate,'MMMM yyyy');

```

[SQL Fiddle](http://sqlfiddle.com/#!6/ab34b/8)
Just add Count(\*) as "monthelyAgentcount" in your query ie

```
 SELECT AgentID,FORMAT(CompletedDate,'MMMM yyyy') as Date,datepart(year,CompletedDate) YearNr,datepart(month,CompletedDate) MonthNr,
     COUNT(CASE WHEN TypeID=50 then 1 ELSE NULL END) as "Type1",
     COUNT(CASE WHEN  TypeID=51 then 1 ELSE NULL END) as "Type2" ,
     Count(*) as "monthelyAgentcount"
     FROM RAW_DATA
     group by  AgentID,datepart(year,CompletedDate),datepart(month,CompletedDate),  FORMAT(CompletedDate,'MMMM yyyy')

```