Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
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_HOLD | PACE_SEC_TYP_LVL1_DESC | SECURITY_KEY | sum(BASE_BOOK_VALUE) | sum(TOTAL BASE_BOOK_VALUE) | BOOK_YIELD | sum(aggr((sum(BASE_BOOK_VALUE)/sum(TOTAL BASE_BOOK_VALUE))*sum(BOOK_YIELD),PACE_SEC_TYP_LVL1_DESC,SECURITY_KEY)) |
22933308.84 | 22933308.84 | - | 4.0330495427354 | |||
PortABC | CASH OR EQUIVALENT | 1_775421 | 53943.75 | 22933308.84 | 0 | 0 |
PortABC | FIXED INCOME | 1_376486 | 105448.1 | 22933308.84 | 4.490098 | 0.020645616653798 |
PortABC | FIXED INCOME | 1_585372 | 360390.25 | 22933308.84 | 4.29087 | 0.06742976876151 |
PortABC | FIXED INCOME | 1_366033 | 458010.28 | 22933308.84 | 4.219619 | 0.08427169812986 |
PortABC | FIXED INCOME | 1_178166 | 1018529.91 | 22933308.84 | 3.641171 | 0.16171419470251 |
PortABC | FIXED INCOME | 1_376484 | 1099673.04 | 22933308.84 | 4.490098 | 0.21530428740164 |
PortABC | FIXED INCOME | 1_365978 | 1149754.38 | 22933308.84 | 4.349537 | 0.21806269874147 |
PortABC | FIXED INCOME | 1_365979 | 1555221.72 | 22933308.84 | 4.219619 | 0.2861533485948 |
PortABC | FIXED INCOME | 1_365957 | 1661872.05 | 22933308.84 | 4.349537 | 0.31519106209974 |
PortABC | FIXED INCOME | 1_178468 | 2067110.36 | 22933308.84 | 4.040392 | 0.36418365181974 |
PortABC | FIXED INCOME | 1_169824 | 2549920.32 | 22933308.84 | 3.650449 | 0.40588796615289 |
PortABC | FIXED INCOME | 1_169493 | 2552652.97 | 22933308.84 | 3.680318 | 0.40964758896276 |
PortABC | FIXED INCOME | 1_184311 | 2739331.03 | 22933308.84 | 3.654988 | 0.43657991581287 |
PortABC | FIXED INCOME | 1_576350 | 2685161.15 | 22933308.84 | 4.29087 | 0.50239926144476 |
PortABC | FIXED INCOME | 1_178565 | 2876289.53 | 22933308.84 | 4.350021 | 0.54557848345708 |
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)
I've found various options of providing dimensions to both AGGR() and sum(TOTAL <> ), but neither are getting me closer.
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.
Can you share a QVW with a snippet of where u are ?
See the blue asdf Chart on the Holdings Test2 Sheet.
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
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.
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) .... ?
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_yield | Book Value | |
---|---|---|
Total Row | =(1*100/600)+(2*300/600)+(3*200/600)=2.16666667 | 600 |
1 | 100 | |
2 | 300 | |
3 | 200 |
if you were to do the same calculation in Excel it would be:
=SUMPRODUCT(B2:B4,C2:C4)/SUM(C2:C4)
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)