Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Weight Average

Hello,

 

I'm looking to get help with a formula that will get me the weighted average based off a set of paramters. I would like the formula to take the sum of spend per material number and divide it by the sum of quantites for that material number. I would like it to do this based off the most recent purchase though and have it look 365 days backwards. So if I make a purchase today on material 12345 then the formula will take today and the last 365 days of purchases for that material and give me the weighted average based off that date range. 

 

Secondly, if there could be a formula that brings me back the most recent purchase price. So the formula goes out and looks for the most recent time we purchased that material and brings back that price.

 

Any help with this would be great

 

Thank you!

  • fhill's avatar
    fhill
    5 years ago

    This the DateAdd (-1 Year) option instead of the DATE command.....   (Sorry for the change in TABLE name and columns in the version below vs. my original post.)  But you only have to change the FILTER Portion to use DATEADD vs. DATE(Y)(M)(D)

     

    1_Year_Avg_Spend = CALCULATE( SUM( 'Table'[USD Spend]) / SUM('Table'[Qty]), FILTER( 'Table', 'Table'[Document Date] > DATEADD('Table'[Document Date], -1, YEAR)))
  • fhill's avatar
    fhill
    5 years ago

    Try this....

    Last_Pur_Price = CALCULATE( AVERAGE( 'Table'[Part Price]), FILTER('Table', 'Table'[Document Date] = [Last_Pur_Date]))
     
    If you had multiple sales on the same day, assuming you want the average of the two?
     
     

     

15 Replies

  • fhill's avatar
    fhill
    Resident Rockstar

     

    Please consider posting some sample mock data, with desired result.  Particaully, more information on how you want the values weighted based on a date range.  It doesn't sound horribly difficult, but it would help to know how your data is structured before trying to provide you code samples...

    Forrest

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello fhill ,

       

      So organized the data below how we would expect it to be set up.

       

      Purchase 1 - PN-12345 Spend 500 Quantity 50 Date 10/1/2020

      Purchase 2 - PN-12345 Spend 400 Quantity 40 Date 1/1/2020

      Purchase 3 - PN-12345 Spend 600 Quantity 60 Date 5/1/2020

      Purchase 5 - PN-12345 Spend 100 Quantity 10 Date 6/1/2019

       

      So for the data above. The calc would only take the purchases that fall into the 365 date range from the most recent purchase (purchase 1). Thus purchase 5 would fall out of the average since its outside of that timeline. The calc for the weighted average would just be during that 365 day timeframe. Take the sum of spend and divide it by the sum of the quanitity. 

       

      Let me know if this helps

      • Ashish_Mathur's avatar
        Ashish_Mathur
        Super User

        Hi,

        Share some data to work with.  The data should be such that can be pasted in an MS Excel file.  Alternatively, share the link from where i can download your PBI file.