英文:
Weighted average when item inventory reaches zero (Excel)
问题
我有一个包含交易数据的Excel工作表(示例图像),在加权平均方面遇到了问题。问题发生在项目完全售出/卸载后,当再次购买时,计算会包括先前已售出的项目的旧价格信息。我可能需要使用一个函数,在特定项目达到零并以新价格再次购买后“重新启动”加权平均计算。我不知道如何做到这一点,或者是否在Excel中可能。我真的很感激任何想法或帮助!
目前使用的公式是:
Col H =SUMIFS($F$2:F2; $D$2:D2; D2)
Col I =IF(B2="Buy";SUMPRODUCT(--($C$2:C2=C2); --($B$2:B2="Buy");$E$2:E2;$F$2:F2)/SUMIFS($F$2:F2;$C$2:C2;C2;$B$2:B2;B2);" ")
英文:
I have an excel worksheet containing transaction data (example image) and ran into a problem with weighted averages. The problem occurs after an item is fully sold/unloaded when it is bought again, the calculation includes the old price information of the items that were previously sold. I would probably need to use a function that "restarts" calculating the weighted average after the position reached zero for a specific item and is bought again at a new price. I do not know how to do this or if this is possible in Excel at all. I would really appreciate any ideas or help!
Formulas currently used are:
Col H =SUMIFS($F$2:F2; $D$2:D2; D2)
Col I =IF(B2="Buy";SUMPRODUCT(--($C$2:C2=C2); --($B$2:B2="Buy");$E$2:E2;$F$2:F2)/SUMIFS($F$2:F2;$C$2:C2;C2;$B$2:B2;B2);" ")
答案1
得分: 1
=IF(H2=0,"-",
IFERROR(LET(prevZero,XMATCH(1,(H1:H$1=0)*(C1:C$1=C2),0,-1)+1,
SUMIFS(INDEX(G:G,prevZero):G2,INDEX(C:C,prevZero):C2,C2)/SUMIFS(INDEX(H:H,prevZero):H2,INDEX(C:C,prevZero):C2,C2)),
SUMIFS(G$2:G2,C$2:C2,C2)/H2))
assuming all items are sold in one transaction as in your sample data.
Another way of looking it which agrees with your idea is that for buy transactions:
New average = (Previous average * previous number of items on hand + Current price * number of items purchased) / Current number of items on hand
while for sell transactions the number of items on hand is updated but the average price is just carried forward.
This actually gives a simpler formula:
=IF(H2=0,0,
IFERROR(LET(prevTrans,XMATCH(TRUE,C1:C$1=C2,0,-1),
IF(B2="Sell",INDEX(J:J,prevTrans),(INDEX(H:H,prevTrans)*INDEX(J:J,prevTrans)+G2)/H2)),
E2))
英文:
If you are able to use xmatch, you can search back for a previous occurrence of a zero and only include transactions after that:
=IF(H2=0,"-",
IFERROR(LET(prevZero,XMATCH(1,(H1:H$1=0)*(C1:C$1=C2),0,-1)+1,
SUMIFS(INDEX(G:G,prevZero):G2,INDEX(C:C,prevZero):C2,C2)/SUMIFS(INDEX(H:H,prevZero):H2,INDEX(C:C,prevZero):C2,C2)),
SUMIFS(G$2:G2,C$2:C2,C2)/H2))
assuming all items are sold in one transaction as in your sample data.
Another way of looking it which agrees with your idea is that for buy transactions:
New average = (Previous average * previous number of items on hand + Current price * number of items purchased) / Current number of items on hand
while for sell transactions the number of items on hand is updated but the average price is just carried forward.
This actually gives a simpler formula:
=IF(H2=0,0,
IFERROR(LET(prevTrans,XMATCH(TRUE,C1:C$1=C2,0,-1),
IF(B2="Sell",INDEX(J:J,prevTrans),(INDEX(H:H,prevTrans)*INDEX(J:J,prevTrans)+G2)/H2)),
E2))
I have added a new item C which ends up with an average price of $15 for the 100 remaining units as required.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论