英文:
Using Office365 Excel array formulas, how can a cummulative weighted average price be calculated?
问题
每天都有一个给定金额和价格的新销售。如何使用数组公式计算该天的累积总金额和加权平均价格?
不使用数组公式,只需使用以下公式并将其复制下来即可:
(sum(h3) * sum(i3) + b4 * c4) / (sum(h3) + b4)
Sum() 用于在引用第3行时返回第4行的零值,第3行是标题。每天,将前一天的累积总金额和累积加权平均价格与新一天的金额和价格相结合... 从而计算出新的累积总金额和新的累积加权平均价格。
如何使用数组公式完成相同的操作?由于引用前一行的数字时存在循环逻辑,因此这个尝试无法工作。
=LET(day, A4:A12, amt, B4:B12, price, C4:C12, prevTotalAmt, OFFSET(H4:H12,-1,), prevAvgPrice,OFFSET(I4:I12,-1,),
newTotalAmt, IF(day = 1, 0, prevTotalAmt) + amt,
newTotalPrice, (IF(day = 1, 0, prevTotalAmt * prevAvgPrice) + amt * price) / newTotalAmt,
HSTACK(newTotalAmt, newTotalPrice) )
数据:
日期 | 数量 | 价格 |
---|---|---|
1 | 100 | 1.00 |
2 | 100 | 3.00 |
3 | 250 | 2.00 |
4 | 400 | 5.00 |
5 | 100 | 2.00 |
6 | 200 | 3.00 |
7 | 100 | 7.00 |
8 | 100 | 3.00 |
9 | 100 | 2.00 |
英文:
Each day, there is a new sale for a given amount and price. How can a cummulative total amount and weighted average price be calculated for that day with an array formula?
Without an array formula, it's simple using this formula and copying it down:
(sum(h3) * sum(i3) + b4 * c4) / (sum(h3) + b4)
Sum() is used to return zero on row 4 when it references row 3, which is a heading. Each day, the previous cumulative total amount and cumulative weighted average price is combined with the new day's amt and price... which calculates a new cumulative total amount and a new cumulative weighted average price.
How can the same thing be done using an array formula? This attempt doesn't work due to circular logic when referencing the previous row's numbers.
=LET(day, A4:A12, amt, B4:B12, price, C4:C12, prevTotalAmt, OFFSET(H4:H12,-1,), prevAvgPrice,OFFSET(I4:I12,-1,),
newTotalAmt, IF(day = 1, 0, prevTotalAmt) + amt,
newTotalPrice, (IF(day = 1, 0, prevTotalAmt * prevAvgPrice) + amt * price) / newTotalAmt,
HSTACK(newTotalAmt, newTotalPrice) )
Data:
Day | Quantity | Price |
---|---|---|
1 | 100 | 1.00 |
2 | 100 | 3.00 |
3 | 250 | 2.00 |
4 | 400 | 5.00 |
5 | 100 | 2.00 |
6 | 200 | 3.00 |
7 | 100 | 7.00 |
8 | 100 | 3.00 |
9 | 100 | 2.00 |
答案1
得分: 4
以下是翻译好的部分:
这可能有点啰嗦:
E2
单元格中的公式:
=LET(amt, B2:B10, price, C2:C10,
accumFunc, LAMBDA(a, v, a + v),
totalAmt, SCAN(0, amt, accumFunc),
weightedPrice, SCAN(0, amt * price, accumFunc),
HSTACK(totalAmt, ROUND( weightedPrice / totalAmt, 2)) )
这里的技巧是使用累加运算,并应用基本数学原理。
首先,对于每一天,使用 SCAN() 计算累计金额。当与 SCAN() 一起使用时,LAMBDA() 函数的第一个参数是累加器,第二个参数是下一个数组值,a 和 v。累加器的初始值设置为零(SCAN() 函数的第一个参数)。
然后,通过将每个金额乘以价格来计算加权价格的累计值。使用 HSTACK() 来显示每天的累计金额旁边的加权平均价格。
英文:
This might be a bit too verbose:
Formula in E2
:
=LET(amt, B2:B10, price, C2:C10,
accumFunc, LAMBDA(a, v, a + v),
totalAmt, SCAN(0, amt, accumFunc),
weightedPrice, SCAN(0, amt * price, accumFunc),
HSTACK(totalAmt, ROUND( weightedPrice / totalAmt, 2)) )
The trick here is to use running totals and apply basic math.
First, for each day, the running total amount is calculated using SCAN(). The LAMBDA() function when used with SCAN() has an accumulator as the first parameter and the next array value as the the second parameter, a and v. The initial value of the accumulator is set to zero (first parameter of the SCAN() function).
The calculation is repeated for the weighted price by weighting (multiplying) each amount by the price. HSTACK() is used to show the cumulative amount next to the weighted average price for each day.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论