Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Want to see what's currently in public preview? We've pulled it all together in one place. Check it out here
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

IF statement in an expression

I am trying to wirte an IF statement in an expression to either Add or subtract sales and costs depeneding on whether the sales figure is a '+' or a '-'. my code so far is

Sum(Aggr(If([Sales]='+',  Sum([Sales Amount (Actual)])-([InvoiceLine_UnitCost]*[Invoiced Quantity]) ,

         If(
[Sales]='-', Sum([Sales Amount (Actual)])+([InvoiceLine_UnitCost]*[Invoiced Quantity])))))

The [Sales] tag is an existing column in the chart.



Labels (1)
12 Replies
bbi_mba_76
Partner - Specialist
Partner - Specialist

Hi,

maybe something like:

Sum(Aggr(Sum([Sales Amount (Actual)]+

          ( If([Sales]='+',-1,1) * [InvoiceLine_UnitCost]*[Invoiced Quantity]))))

Not applicable
Author

Hi,

This doesn't seem to return a number in my table. I get a - instead.?

danieloberbilli
Specialist II
Specialist II

or is just the dimension missing in the aggr() function?

Here I inserted something what I called SalesID at the end:

sum(aggr(if(Sales='+', Sum([Sales Amount (Actual)])-([InvoiceLine_UnitCost]*[Invoiced Quantity]),

if(Sales='-', Sum([Sales Amount (Actual)])+([InvoiceLine_UnitCost]*[Invoiced Quantity]))),SalesID))

Not applicable
Author

Can you please post the sample application

Not applicable
Author

Hi,

That returns a value of £0.0 now, so getting closer. In my table i have a sale of -£25.92 and cost of +£16.80. As the sale is a credit and negative in value, but the cost is a positive, i should return a value of -£9.12.

danieloberbilli
Specialist II
Specialist II

what about inserting a *(-1) for the part that should become negative (as these are costs):

sum(aggr(if(Sales='+', Sum([Sales Amount (Actual)])-([InvoiceLine_UnitCost]*[Invoiced Quantity]),

if(Sales='-', Sum([Sales Amount (Actual)])*(-1)+([InvoiceLine_UnitCost]*[Invoiced Quantity]))),SalesID))

preminqlik
Specialist II
Specialist II

hi try this

sum(If([Sales]='+', [Sales Amount (Actual)]-([InvoiceLine_UnitCost]*[Invoiced Quantity]),


         If(
[Sales]='-', [Sales Amount (Actual)]+([InvoiceLine_UnitCost]*[Invoiced Quantity]))


--->or

sum(aggr(sum(If([Sales]='+', [Sales Amount (Actual)]-([InvoiceLine_UnitCost]*[Invoiced Quantity]),


         If(
[Sales]='-', [Sales Amount (Actual)]+([InvoiceLine_UnitCost]*[Invoiced Quantity])),SalesID))


Not applicable
Author

Hi All,

Answers all seem to return a value of zero instead of correct number.

rajeshvaswani77
Specialist III
Specialist III

Hi Neil,

You will need to break it up in to parts and check teh values, or use a one or many list boxes with expressions.

thanks,

Rajesh Vaswani