
大家好!
我在这篇文章下附上了一个示例文件。
我目前正在尝试计算差异百分比,
由(新值-原始值)/原始值给出
在这种情况下,时间 0 作为参考(原始值)。
例如:
时间:0,部分:1 值 1:690.2
时间:50,部分:1,值 1:690.52
(690.52-690.2)/(690.2) = 0.046%
时间:100,部分:1,值 1:689.25
(689.25-690.2)/(690.2)= -0.13%
我想得到的结果是有一个新表,其中时间、id、部分不变,
同时具有百分比格式的值 1 和值 2 的新值。
我想过将不同的时间值移动到不同的表中,
然后根据时间 0 计算它并将其附加到新表,但我不知道
如何写在脚本上。
我在“Percentage Different Progress”文件中写了一些脚本,但我卡住了。
如果有人能帮助我,我真的很感激。
非常感谢你。

值 2 结果可以轻松替换值 1 结果。
不,您不需要对所有列的 JSL 进行硬编码。我扩展了我以前的代码以说明如何在数据表中的尽可能多的列上运行它。我还假设您不希望 Time=0 行更改原始 Value1 数据,但如果不是这种情况,则只需删除做出该决定的 If() 语句
Names Default To Here( 1 );
dt = Current Data Table();
// Create a subset with only the data where Time = 0
dt2 = dt << subset( Rows( dt << get rows where( :Time == 0 ) ), selected columns( 0 ) );
// Delete the Time column. In the join step below, it
// will write on top of the original Time column if
// it remains in the subsetted data table
dt2 << delete columns( "Time" );
// Create a list of the column names so all of them can be
// changed to a new name for the Join below. Eliminate
// the columns "id" and "Part" from the name change since
// they will be used in the column matching in the Join.
divisorColNames = dt2 << get column names( string );
For( i = N Items( divisorColNames ), i >= 1, i--,
If( divisorColNames[i] == "id" | divisorColNames[i] == "Part",
divisorColNames = Remove( divisorColNames, i, 1 )
)
);
// Rename all of the divisor columns by "adding divisor_"
// in front of each name
For( i = 1, i <= N Items( divisorColNames ), i++,
Column( dt2, divisorColNames[i] ) << set name(
"divisor_" || Char( Column( dt2, divisorColNames[i] ) << get name )
)
);
// Join the data tables. All of the matching id and part
// values will be joined to all rows with the same values
dtJoin = dt << Join(
With( dt2 ),
Merge Same Name Columns,
Match Flag( 0 ),
By Matching Columns( :id = :id, :Part = :Part ),
Drop multiples( 0, 0 ),
Include Nonmatches( 1, 0 ),
Preserve main table order( 1 )
);
// The dt2 data table is no longer needed so close it
Close( dt2, nosave );
// Loop through all of the columns and create the new values
// unless the Time value is 0
For( theCol = 1, theCol <= N Items( divisorColNames ), theCol++,
For( theRow = 1, theRow <= N Rows( dtJoin ), theRow++,
If( dtJoin:Time[theRow] != 0,
Column( dtJoin, divisorColNames[theCol] )[theRow] = (
Column( dtJoin, divisorColNames[theCol] )[theRow]
-Column( dtJoin, "divisor_" || divisorColNames[theCol] )[theRow]) /
Column( dtJoin, "divisor_" || divisorColNames[theCol] )[theRow]
)
)
);
// Delete all of the no longer needed divisor columns
For( i = 1, i <= N Items( divisorColNames ), i++,
dtJoin << delete columns( "divisor_" || divisorColNames[i] )
);
嗨,吉姆!
十分感谢你的帮助。
我想知道有没有办法
1) 替换 Column Value 1 初始单元格中的 Value2(结果)。
2)如果实际上有更多的列(比如30)我需要做这个操作(百分比差异),
我是否需要对每个新列进行硬编码,或者有没有一种方法可以乘法或循环创建列。
如果我明白你想要什么,我认为这是做你想做的最简单的方法。
dt = Current Data Table();
dt2 = dt << subset(
Rows( dt << get rows where( Time == 0 ) ),
columns( {"id", "Part", "Value1"} )
);
dt2:Value1 << set name( "divisor" );
dtJoin = dt << Join(
With( dt2 ),
Merge Same Name Columns,
Match Flag( 0 ),
By Matching Columns( :id = :id, :Part = :Part ),
Drop multiples( 0, 0 ),
Include Nonmatches( 1, 0 ),
Preserve main table order( 1 )
);
Close( dt2, nosave );
dtJoin << New Column( "Value2",
Numeric,
"Continuous",
Format( "Percent", 10, 4 ),
Formula( (:Value1 - :divisor) / :divisor )
);
dtJoin:Value2 << delete property( "formula" );
dtJoin << delete columns( "divisor" );