Himanshu Goel
Himanshu Goel

Reputation: 1

Add and subtract simultaneously in one formula

I have 3 columns in my details section:

  1. s.no
  2. balance
  3. type

I want my formula field total to subtract the value if the type is debit and add the value when it is credit. The logic would look like this in C++. How can I rewrite this in Crystal syntax?

if(type=="credit")    
    total = total+balance    
else if((type=="credit")   
    total = total-balance;   

Upvotes: 0

Views: 126

Answers (2)

4444
4444

Reputation: 3680

Try creating a formula field called UpdatedTotal and input the following code:

If {type} = "debit" Then
    {total} - {balance}
Else If {type} = "credit" Then
    {total} + {balance}

Then just drop the formula field into the report wherever you want it to display.

In Crystal it's not necessary to assign the updated total value: This new field automatically displays the updated total and can be used in other calculations by referencing {@UpdatedTotal} by name.

Upvotes: 0

Paarth
Paarth

Reputation: 590

You can try this for crystal report formula

If {type} = "credit" Then
{total} := {total} + {balance}
Else If {type} = "debit" Then
{total} := {total} - {balance}

Alternately you can write case statement in sql query to get updated_total

SELECT type, total,balance.....,  
   CASE   
      WHEN type ="credit" THEN (total + balance)   
      WHEN type ="debit"  THEN  (total - balance)  
   END  as updated_total 
FROM Datatable ; 

Upvotes: 1

Related Questions