anyone know how to calc weighted averages

  • I have a cube containing bonds that I would like to calculate the the average moody's rating based on the amount.  The non-mdx formula would look something like this.

    sum of ((moody_rating_weight_number * amount) / total_amount)

    I can't seem to get the detial level data as a measure to do the calc and I get errors if I try to use dimension level data.

    asset

    ratingamountcalc
    11100.066667
    21200.133333
    32300.4
    43400.8
    53501
    1502.4

    This sample returns the weighted average value of 2.4

    Thanks

  • Are you using AS 2000 or 2005?

    For the fields you outlined, where are these found (ie fact table or dimension/s)?  If we can assume that the rating is found in the fact table (a potentially poor assumption as it very well could be time related) but anyways...  you need to do the detail level part of the calculation prior to loading to AS otherwise the engine will sum(amount) and sum(rating) and then perform A * B which obviously isn't correct.

    If you can get the rating in to the same transaction record as the Amount, then you could create a new fact table field (if you're using a view, this would be easy, alternatively you could even use a claculated field in a table, depending on data sizing) that is simply the rating * amount and then create calc member (in the cube) that uses the new field / the total of amount.

     

    Steve.

  • That worked.  I had tried doing it with a calculation based on non-additive measures, but that didn't work.

    Anyone know how to do lookups with measures and display character values?  Now that  have the weighted average, I would like to look up the letter rating that goes with the numerica average and display it as a measure.  Not sure if this is possible.

    Thanks.

  • Again, not sure which version you're using but if it's AS2K5, then you could write a Stored Procedure (look them up in BOL) which returns the character value based on the input numerical value.  Alternatively, if you have a small enough range (e.g. A through E) then you could simply implement a case statement (or several nexted IIF's) and achieve the same result.

    Steve.

  • Thanks for the advise.  I am using as2k5.  If I can use a sp, that would be great since I could easily pass in the weighted average, round it and then look up the value.  How do you call a sp from inside metrics section?  Do you happend to have any samples?

    Thanks.

  • I don't personally have any samples but the following links should give you some clear info on how to implement them and also things to be aware of when using stored procs in AS.

    Mosha -> Best Practices for ADOMD.Net Stored Procs -> here

    Many  -> AS Stored PRoc public project -> here

    Chris Webb -> Full source of a stored proc for finding current member across all attribute dims -> here

     

    Steve.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply