带条件的前一行值的滚动总和我的数据以两列开头:批次号和数量。
我需要帮助创建脚本来计算前一行值的滚动总和(当前行 + 前 2 行)。
逻辑解释如下表。

高级统计建模自动化和脚本编写大规模定制回复:具有条件的前一行值的滚动总和感谢您发布问题。 这是一个有趣但需要解决的曲折问题。
我向 JSL 添加了一些注释,希望它是有意义的,而不只是看起来像一个巨大的魔术公式。
老实说,起初我认为这几乎是不可能解决的,但喝了一大杯咖啡后,……还不错。
回复:具有条件的前一行值的滚动总和谢谢拜伦。
回复:具有条件的前一行值的滚动总和将此脚本添加到您的数据表并试一试
dt1=current data table();
//I want to get the max quantity by lot, so I'm adding a new column to change all the lots back to their non-decimal value.
dt1<< New Column("Lot Actual", formula(If( :Qty == Col Maximum( :Qty, Char( Round( Num( :Lot# ), 0 ) ) ),
:Lot#,
.
)));<BR />//this next formula column excludes rows that aren't the max Qty by lot
dt1<< New Column( "Lot Values in One Formula",
Formula(
If( :Lot Actual == "",
Row State( row() ) = Excluded State( 1 );
Excluded( Row State( row() ) );
1;
)
)
);
dt1<<RUN formulas=""></RUN>//once all the formulas for tagging the data are added, we can use Tabulate to get a subset of just the important rows.<BR />//There are lots of other ways to do this, this just happened to be the method of the day
obj1=dt1 << Tabulate(
Add Table(
Column Table( Analysis Columns( :Qty ) ),
Row Table( Grouping Columns( :Lot# ) )
)
);
dt2=obj1 << Make Into Data Table;
column(dt2,2)<<SET name=""></SET><BR />//Finally here is the rolling sum of the last three lots<BR />//there is moving average function we could have used, it would have looked simpler but more abstract
dt2<<NEW column=""></NEW>//<BR />//This part joins the values from the separate table back to the original table<BR />
dt1 << Update(
With( dt2 ),
Match Columns( :Lot# = :Lot# )
);
close(dt2, nosave);
obj1<<CLOSE window=""></CLOSE>//I feel like this step is cheating a little. This step just repeats the missing values down to the next non-missing<BR />//row. Its some code I got from Brady Brady a ways back. Different iterations of come in handy from time to time.
<BR />dtnr=current data table();
dtcollist=dtnr<<GET column="" names=""></GET>回复:具有条件的前一行值的滚动总和对就这样。
但是我想维护原始数据表而不排除一些行来得到这个结果。
这可以做到吗?
回复:具有条件的前一行值的滚动总和这是正确的吗。 (图中最右边一栏)?

回复:具有条件的前一行值的滚动总和我会弄清楚如何得到这个。
以防万一有人对此有解决方案,也请分享。
谢谢。
回复:具有条件的前一行值的滚动总和对,我现在明白了。
需要找到每手的最大值,然后取当前和最后两个最大手数的滚动总和。
我觉得有一个潜在的解决方案,可能需要多考虑一下。
回复:具有条件的前一行值的滚动总和感谢您证明使用公式的想法。
我的数据集无法直接使用公式进行滚动求和,因为一些批次数据未被计算在内。
例子,
Lot# 3 和 Lot# 3.2,只有 Lot# 3.2 数量被计算在滚动总和中,因为它是最终批次数量。
Lot#3 中添加了额外的数量。 它成为 Lot#3.2。
Lot# 7、7,2、7,3 和 Lot# 10、10.3 相同。
Lot# 7.3 数量和 10.3 计入滚动总和,因为它是最终批次数量。
需要社区的指导。

回复:具有条件的前一行值的滚动总和一种方法可能是使用这样的公式:

If( Row() == 1,
:Qty,
If( Row() == 2,
Lag( :Qty, 1 ) + :Qty,
:Qty + Lag( :Qty, 1 ) + Lag( :Qty, 2 )
)
)
对于第一行,没有什么可以求和的,所以它只使用第 1 行
对于第二行只有两个值,当前值和上一个值,所以它的 row+lag(1)
第二个之后的所有行都采用当前行加上前两行的总和:lag(1) 和 lag(2)