
大家好!
我在這篇文章下附上了一個示例文件。
我目前正在嘗試計算差異百分比,
由(新值-原始值)/原始值給出
在這種情況下,時間 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" );