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

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Weighted Average Aggregation

I am trying to create an expression that will give the weighted average of a field at the total and any grouping layers.  I am able to create a secondary field that displays the weighted avg, but the underlying rows do not return the true value, but a useless weighted value.  See the data in the below table.  I want to create a weighted aggregation of the 'Book_Yield' field.  I am able to create the aggregate value with the expression:

sum(aggr((sum(BASE_BOOK_VALUE)/sum(TOTAL BASE_BOOK_VALUE))*sum(BOOK_YIELD),PACE_SEC_TYP_LVL1_DESC,SECURITY_KEY))

However, at the underlying rows, the data is useless, i need to return the data i see in the Book_Yield field, but with the total row from the complex aggr() function.

ACCT_SYSTEM_PORTF_NO_HOLDPACE_SEC_TYP_LVL1_DESCSECURITY_KEYsum(BASE_BOOK_VALUE)sum(TOTAL BASE_BOOK_VALUE)BOOK_YIELDsum(aggr((sum(BASE_BOOK_VALUE)/sum(TOTAL BASE_BOOK_VALUE))*sum(BOOK_YIELD),PACE_SEC_TYP_LVL1_DESC,SECURITY_KEY))
22933308.8422933308.84-4.0330495427354
PortABCCASH OR EQUIVALENT1_77542153943.7522933308.8400
PortABCFIXED INCOME1_376486105448.122933308.844.4900980.020645616653798
PortABCFIXED INCOME1_585372360390.2522933308.844.290870.06742976876151
PortABCFIXED INCOME1_366033458010.2822933308.844.2196190.08427169812986
PortABCFIXED INCOME1_1781661018529.9122933308.843.6411710.16171419470251
PortABCFIXED INCOME1_3764841099673.0422933308.844.4900980.21530428740164
PortABCFIXED INCOME1_3659781149754.3822933308.844.3495370.21806269874147
PortABCFIXED INCOME1_3659791555221.7222933308.844.2196190.2861533485948
PortABCFIXED INCOME1_3659571661872.0522933308.844.3495370.31519106209974
PortABCFIXED INCOME1_1784682067110.3622933308.844.0403920.36418365181974
PortABCFIXED INCOME1_1698242549920.3222933308.843.6504490.40588796615289
PortABCFIXED INCOME1_1694932552652.9722933308.843.6803180.40964758896276
PortABCFIXED INCOME1_1843112739331.0322933308.843.6549880.43657991581287
PortABCFIXED INCOME1_5763502685161.1522933308.844.290870.50239926144476
PortABCFIXED INCOME1_1785652876289.5322933308.844.3500210.54557848345708
1 Solution

Accepted Solutions
Not applicable
Author

I actually just figured it out...finally.  So simple, but frustrating to get enough understanding of how qlikview calculates its aggregations that it took so long.

sum(BOOK_YIELD*BASE_BOOK_VALUE)/sum(BASE_BOOK_VALUE)

View solution in original post

9 Replies
Not applicable
Author

I've found various options of providing dimensions to both AGGR() and sum(TOTAL <> ), but neither are getting me closer. 

Not applicable
Author

I've gotten a bit further, but an certainly stuck now.  I have a function that will return the values that need to be summed, but i can not get these values to sum correctly. 

BASE_BOOK_VALUE/

sum(total <ACCT_SYSTEM_PORTF_NO_HOLD,

  PACE_SEC_TYP_LVL1_DESC,

  PACE_SEC_TYP_LVL2_DESC,

  PACE_SEC_TYP_LVL3_DESC> BASE_BOOK_VALUE)

  *BOOK_YIELD

That being the expression, I can not seem to implement sum(aggr(your expression, your dimensions)).  the above returns the correct value at the lowest level of the pivot table, but not at any of the partial sums.  If i change the above to:

sum(BASE_BOOK_VALUE)/

sum(total <ACCT_SYSTEM_PORTF_NO_HOLD,

  PACE_SEC_TYP_LVL1_DESC,

  PACE_SEC_TYP_LVL2_DESC,

  PACE_SEC_TYP_LVL3_DESC> BASE_BOOK_VALUE)

  *sum(BOOK_YIELD)

Changing it to the above retuns the wrong number because at the partial_sum row, it calculates the sum of each 3 columns without summing the values from the lower level which were properly calculated.

JonnyPoole
Former Employee
Former Employee

Can you share a QVW with a snippet of where u are ?

Not applicable
Author

See the blue asdf Chart on the Holdings Test2 Sheet.

JonnyPoole
Former Employee
Former Employee

Thanks. I took a look and have two questions:

The calculation you are working on seems to be :     ( sum( bookvalue)  /  sum( subtotal bookvalue ) )   * Book yield

1) Is the subtotal value the grand total for the whole chart or is it always the subtotal ? In your example there are 3 subtotals and 1 grand total. Just trying to figure out what is my denonominator and does its value change for every level of the pivot chart ...

2) How do you calculate Book Yield at the subtotal level.  I'm guessing its not just a sum...

Jonny

Not applicable
Author

Technically, the ideal calculation is actually: sum( ( bookvalue  /  sum( subtotal bookvalue ) )   * Book yield )

1) the subtotal value is essentially sum(bookvalue) of the next higher Dimension (in the case of the first dimension, it is of the entire data set).  The denominator changes at each dimension.  I was going to handle this by creating a pick() and use different calculations at each dimension, but I can't seem to get the first subtotal to work on dimension 3.

2)  Book Yield at the lowest level (the raw data which corresponds to the security_key level) is a direct data point from the database.  It is not calculated. I am merely trying to aggregate the value at each dimension subtotal/grandtotal.

JonnyPoole
Former Employee
Former Employee

Its ugly but I think I have #1 with this expression. I've added it to the chart with a new field called 'denom'. Please validate it is what you are after.

if( Dimensionality()=4,  aggr( NODISTINCT sum( BASE_BOOK_VALUE) , ACCT_SYSTEM_PORTF_NO_HOLD,PACE_SEC_TYP_LVL1_DESC,PACE_SEC_TYP_LVL2_DESC),
if(Dimensionality()=3, aggr( NODISTINCT sum( BASE_BOOK_VALUE) , ACCT_SYSTEM_PORTF_NO_HOLD,PACE_SEC_TYP_LVL1_DESC),
if(Dimensionality()=2, aggr( NODISTINCT sum( BASE_BOOK_VALUE) , ACCT_SYSTEM_PORTF_NO_HOLD),
sum(TOTAL BASE_BOOK_VALUE)
)
)
)


For #2 I'm not sure what to multiple the subtotaled fields by. Book yield values exist at the lowest level, but at the subtotal what is the aggregated book yield. If you can give me the business logic or a quick sample it would help.  IE:  if I have 3 detailed records each with a book yield of 1 , 2, and 3.  Is the subtotaled book yield 6 (sum) , 2 (average) .... ?

Not applicable
Author

Thx Johnny,  #1 does return the correct denominators.  If you see the d3_Total field in my qvw, you would see the same thing, though done using the sum(total <dim>) instead of the aggr() function in order to run faster on large data sets, which mine eventually will be.  Please see a short example of weighted avg below:

Book_yieldBook Value
Total Row=(1*100/600)+(2*300/600)+(3*200/600)=2.16666667600
1100
2300
3200

if you were to do the same calculation in Excel it would be:

=SUMPRODUCT(B2:B4,C2:C4)/SUM(C2:C4)

Not applicable
Author

I actually just figured it out...finally.  So simple, but frustrating to get enough understanding of how qlikview calculates its aggregations that it took so long.

sum(BOOK_YIELD*BASE_BOOK_VALUE)/sum(BASE_BOOK_VALUE)