Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Selling price index and aggr()

Hello,

I have a table which looks like the following

 

SalesOfficeProductDateYearQuantityValue
1000ABC12/10/201520155100
1000ABC12/09/201420141075
1200DEF12/08/2015201510200
1000DEF12/08/2015201520150
1000GHI12/08/20142014790
1200DEF12/07/20142014545

I was asked to calculate a selling price index, which can be summed up like this:

([2015 sales])/([2015 quantities]*(If ([2014 average price] = 0, [2015 average price],[2014 average price]))*100;

I use this function, which works fine per product:

=Sum(Aggr(Sum({$<Year={$(=max(Year))}>}[Quantity])*Sum({$<Year={$(=max(Year))}>}[Value])/Sum({$<Year={$(=max(Year))}>[Quantity]),Product))

/

Sum(Aggr(Sum({$<Year={$(=max(Year))}>}[Quantity])*

(If(IsNull(Sum({$<Year={$(=max(Year)-1)}>}[Value])/Sum({$<Year={$(=max(Year)-1)}>}[Quantity]))

  or Sum({$<Year={$(=max(Year)-1)}>}[Value])/Sum({$<Year={$(=max(Year)-1)}>}[Quantity]) = 0,

  Sum ({$<Year={$(=max(Year))}>}[Value])/Sum({$<Year={$(=max(Year))}>}[Quantity]),

  Sum ({$<Year={$(=max(Year)-1)}[Value])/Sum({$<Year={$(=max(Year)-1)}>}[Quantity]))),Product))

*100

But in some cases, on sales office level, I have sales for 2015 and 2014, but this function returns a null. However, as soon as I select only one sales office, then the result is correct.

I suppose I don't use the aggr() function correctly, so I'd like some advice, of course.

4 Replies
jonathandienst
Partner - Champion III
Partner - Champion III

The dimensions in the Aggr() must include all the dimensions in the table -- in this case it looks like SalesOffice, Product, Date, Year. So the Aggr would look like:

     Sum(Aggr(Sum(.....), SalesOffice, Product, Date, Year))

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein
Colin-Albert

As Jonathan says, the Aggr statement must include all dimensions.

Also when using Aggr with set expressions, the set expression must appear twice, in both the Aggr expression and the containing sum expression.

Henric hic recently posted an excellent blog on using Aggr  Pitfalls of the Aggr function

Not applicable
Author

Thanks, I found the first post about Aggr() that Henric posted, but did not see the one about pitfalls. That led to a serie of interesting reads!

I'll try to fix my function before I flag this solution as answered.

Not applicable
Author

After a night of sleep, I'm less confused by the aggr() function (I think), but I don't think that my function can do what I need to do on sales office level...

I corrected my basis table for my example.

SalesOfficeInvoiceProductDateYearQuantityValue
1000123ABC12/10/201520155100
1000456ABC12/09/201420141075
1200789DEF12/08/2015201510200
1000234DEF12/08/2015201520150
1000567GHI12/08/20152015890
1200890DEF12/07/20142014545

In Excel, I would use sum.if to get this table first to calculate the selling price index by product.

The most important columns would be the last three:

  • Qty2015 with Avg2015 = Value2015 actually
  • Qty2015 with Avg2014 = Qty2015 * (If(Avg2014 > 0, Avg2014, Avg2015)
  • SPI = [Qty2015 with Avg2015]/[Qty2015 with Avg2014]*100
SalesOfficeProductQty2014Qty2015Value2014Value2015Avg2014Avg2015Qty2015 with Avg2015Qty2015 with Avg2014SPI
1000ABC105751007,52010037,5   266,67
1000DEF020015007,5150150   100,00
1000GHI08090011,259090   100,00
1200DEF5104520092020090   222,22

  

Then, I'd sum these column to have the SPI by SalesOffice

SalesOfficeQty2015 with Avg2015Qty2015 with Avg2014SPI
1000340277,5122,52
120020090222,22

I'm not sure the function I used is right for this. Is it?