Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

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!

2 ACCEPTED SOLUTIONS

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)))



Did I answer your question, or help you along the way?
Please give Kudos or Mark as a Solution!


https://www.linkedin.com/in/forrest-hill-04480730/

Proud to give back to the community!
Thank You!




View solution in original post

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?
 
image.png
 

 




Did I answer your question, or help you along the way?
Please give Kudos or Mark as a Solution!


https://www.linkedin.com/in/forrest-hill-04480730/

Proud to give back to the community!
Thank You!




View solution in original post

15 REPLIES 15
fhill
Resident Rockstar
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




Did I answer your question, or help you along the way?
Please give Kudos or Mark as a Solution!


https://www.linkedin.com/in/forrest-hill-04480730/

Proud to give back to the community!
Thank You!




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

Since you will want the Last Purchase Date, and we'll use it minus 1 year in the CAL formula, start with that as it's own measure:

LastPurchaseDate = CALCULATE(LASTDATE('1 Year Trend'[Date]), FILTER(ALLEXCEPT('1 Year Trend', '1 Year Trend'[Part#]), 1=1))
** We have to include the more complicated Filter, to include this same value in the next Calculation.
 
I wasn't sure if you were looking for an Average of the 'Spend' or 'Spend divided by Qty'?
1_Year_Avg_Price = CALCULATE(AVERAGE('1 Year Trend'[Total Spend]), FILTER('1 Year Trend', '1 Year Trend'[Date] > DATE((YEAR([LastPurchaseDate])-1), MONTH([LastPurchaseDate]), DAY([LastPurchaseDate]))))
 
1_Year_Avg_Price_PerQty = CALCULATE((SUM('1 Year Trend'[Total Spend]) / SUM('1 Year Trend'[Qty])), FILTER('1 Year Trend', '1 Year Trend'[Date] > DATE((YEAR([LastPurchaseDate])-1), MONTH([LastPurchaseDate]), DAY([LastPurchaseDate]))))

 

 

image.png

 

 

 

 

 




Did I answer your question, or help you along the way?
Please give Kudos or Mark as a Solution!


https://www.linkedin.com/in/forrest-hill-04480730/

Proud to give back to the community!
Thank You!




Anonymous
Not applicable

Hello @fhill 

 

That first formula worked like a charm.

 

I'm looking for the second option you provided for the average. I'm getting a error with the formula though. Any ideas?2020-10-22_9-44-13.png

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)))



Did I answer your question, or help you along the way?
Please give Kudos or Mark as a Solution!


https://www.linkedin.com/in/forrest-hill-04480730/

Proud to give back to the community!
Thank You!




Anonymous
Not applicable

You my friend, a genius! Thank you! What a journey haha. 

 

One last thing... is it possible to turn that first formula you did into telling me what the price was on that date?

 

Orginal formula = 

CALCULATE(LASTDATE(tbReceivalHistory[Document Date]),FILTER(ALLEXCEPT(tbReceivalHistory,tbReceivalHistory[Part Number (Material)]),1=1))
 
I want this to tell me the piece price on that date it's finding.

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?
 
image.png
 

 




Did I answer your question, or help you along the way?
Please give Kudos or Mark as a Solution!


https://www.linkedin.com/in/forrest-hill-04480730/

Proud to give back to the community!
Thank You!




Anonymous
Not applicable

Thank you again! This will do just what I need. I would imagine the average would be the best practice. You're a life saver. Thank you again for all your help!

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.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

@Ashish_Mathur  Let me know if this helps

 

Part Number (Material)Part Price (USD) (Piece Price)QuantityUOM (Unit of Measure)USD SpendDocument Date
PN-272679$35.059EA$315.423/14/2019
PN-272679$23.543EA$70.634/1/2019
PN-272679$21.3796EA$2,051.784/1/2019
PN-272679$23.5430EA$706.284/1/2019
PN-272679$34.889EA$313.904/15/2019
PN-272679$21.6269EA$1,491.704/16/2019
PN-272679$21.5775EA$1,617.494/29/2019
PN-272679$24.9424EA$598.674/29/2019
PN-272679$23.4424EA$562.525/10/2019
PN-272679$24.8336EA$894.035/30/2019
PN-272679$22.4248EA$1,076.225/30/2019
PN-272679$25.1324EA$603.116/10/2019
PN-272679$25.1324EA$603.116/27/2019
PN-272679$25.1324EA$603.116/27/2019
PN-272679$22.1196EA$2,122.197/4/2019
PN-272679$25.5724EA$613.657/25/2019
PN-272679$22.1184EA$1,856.927/25/2019
PN-272679$25.5724EA$613.657/25/2019
PN-272679$22.0060EA$1,320.208/14/2019
PN-272679$25.2224EA$605.368/31/2019
PN-272679$25.2224EA$605.368/22/2019
PN-272679$25.2224EA$605.368/31/2019
PN-272679$22.0072EA$1,584.258/28/2019
PN-272679$25.2224EA$605.368/31/2019
PN-272679$25.2524EA$606.069/20/2019
PN-272679$25.3624EA$608.5210/10/2019
PN-272679$25.3536EA$912.7710/19/2019
PN-272679$25.3624EA$608.5210/24/2019
PN-272679$25.2824EA$606.6311/5/2019
PN-272679$22.8248EA$1,095.3811/14/2019
PN-272679$22.0560EA$1,322.9811/14/2019
PN-272679$25.2824EA$606.6311/14/2019
PN-272679$25.2824EA$606.6311/29/2019
PN-272679$21.85150EA$3,277.9811/27/2019
PN-272679$25.5624EA$613.441/2/2020
PN-272679$25.8824EA$621.141/14/2020
PN-272679$25.5624EA$613.441/14/2020
PN-272679$25.8824EA$621.141/23/2020
PN-272679$23.0240EA$920.802/6/2020
PN-272679$23.0240EA$920.802/10/2020
PN-272679$23.0240EA$920.802/21/2020
PN-272679$23.0240EA$920.802/20/2020
PN-272679$22.0140EA$880.333/2/2020
PN-272679$21.6840EA$867.353/25/2020
PN-272679$21.7140EA$868.254/1/2020
PN-272679$20.7480EA$1,659.144/7/2020
PN-272679$22.2940EA$891.465/15/2020
PN-272679$21.5160EA$1,290.496/4/2020
PN-272679$22.0660EA$1,323.377/6/2020
PN-272679$22.0681EA$1,786.557/21/2020
PN-272679$22.5260EA$1,351.368/13/2020
PN-272679$22.8360EA$1,370.029/3/2020
PN-272679$22.4560EA$1,347.1010/9/2020

 

Just saw you updated sample, please let me know if the code I just posted helps you get to your solution without me having to re-do all the code.

 

Thank You,

Forrest




Did I answer your question, or help you along the way?
Please give Kudos or Mark as a Solution!


https://www.linkedin.com/in/forrest-hill-04480730/

Proud to give back to the community!
Thank You!




fhill
Resident Rockstar
Resident Rockstar

 

Is you Date column in the original data formatted to be a Date Value in Power BI?

 

FOrrest




Did I answer your question, or help you along the way?
Please give Kudos or Mark as a Solution!


https://www.linkedin.com/in/forrest-hill-04480730/

Proud to give back to the community!
Thank You!




Anonymous
Not applicable

So the measure works when I have all material numbers. When I filter down to just one of them I get the error. Any way to stop this?

Hmmmm, I'm going to promote this up on the channel.  I took your longer sample data, changed every other Part Number, but was still able to produce a calcualtion with 1 Part Number filtered...  

 

fhill_0-1603384461941.png

 




Did I answer your question, or help you along the way?
Please give Kudos or Mark as a Solution!


https://www.linkedin.com/in/forrest-hill-04480730/

Proud to give back to the community!
Thank You!




Anonymous
Not applicable

Hello, 

 

Yes it is. 

kfitz20_0-1603378556680.png

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors