1

我试图弄清楚如何使用 Spotfire(在线版)中的变量来构建评分机制并用最终结果填充计算列。

我有几个值存储在列中,我将使用这些值来评估和归因于这样的分数:

如果 column1<10 则段 1 = 段 1 + 1

如果 column1>10 那么段 2 = 段 2+1

...ETC...

最后,每个“段”都应该有一个分数,我想简单地显示得分最高的段的名称。

前任:

Segment1 的最终值为 10

Segment2 的最终值为 22

Segment3 的最终值为 122

我会将 Segment3 显示为计算列的值

仅使用“IF”会导致我使用复杂的 IF 结构,因此我更倾向于寻找看起来更像脚本的东西。

有没有办法通过 Spotfire 实现这一目标?

谢谢洛朗

4

1 回答 1

2

要循环浏览数据行并计算运行分数,您可以使用 IronPython 脚本。下面的脚本正在从名为“数据表”的数据表的 Col1 和 Col2 中读取数值数据。它计算每一行的分数值并将其写入制表符分隔的文本字符串。完成后,它会使用 Add Columns 函数将其添加到 Spotfire 表中。请注意,现有数据需要具有唯一标识符。如果没有,RowId() 函数可用于为唯一行 ID 创建计算列。

from Spotfire.Dxp.Data import *
from System.IO import StringReader, StreamReader, StreamWriter, MemoryStream, SeekOrigin
from Spotfire.Dxp.Data.Import import *
from System import Array

def add_column(table, text, col_name):
    # read the text data into memory
    mem_stream = MemoryStream()
    writer = StreamWriter(mem_stream)
    writer.Write(text)
    writer.Flush()
    mem_stream.Seek(0, SeekOrigin.Begin)

    # define the structure of the text data
    settings = TextDataReaderSettings()
    settings.Separator = "\t"
    settings.SetDataType(0, DataType.Integer)
    settings.SetColumnName(0, 'ID')
    settings.SetDataType(1, DataType.Real)
    settings.SetColumnName(1, col_name)

    # create a data source from the in memory text data
    data = TextFileDataSource(mem_stream, settings)

    # define the relationship between the existing table (left) and the new data (right)
    leftColumnSignature = DataColumnSignature("Store ID", DataType.Integer)
    rightColumnSignature = DataColumnSignature("ID", DataType.Integer)
    columnMap = {leftColumnSignature:rightColumnSignature}
    ignoredColumns = []
    columnSettings = AddColumnsSettings(columnMap, JoinType.LeftOuterJoin, ignoredColumns)

    # now add the column(s)
    table.AddColumns(data, columnSettings)

#get the data table
table=Document.Data.Tables["Data Table"]

#place data cursor on a specific column
cursorCol1 = DataValueCursor.CreateFormatted(table.Columns["Col1"])
cursorCol2 = DataValueCursor.CreateFormatted(table.Columns["Col2"])
cursorColId = DataValueCursor.CreateFormatted(table.Columns["ID"])
cursorsList = Array[DataValueCursor]([cursorCol1, cursorCol2, cursorColId])

text = ""
rowsToInclude = IndexSet(table.RowCount,True)
#iterate through table column rows to retrieve the values
for row in table.GetRows(rowsToInclude, cursorsList):
    score = 0
    # get the current values from the cursors
    col1Val = cursorCol1.CurrentDataValue.ValidValue
    col2Val = cursorCol2.CurrentDataValue.ValidValue
    id = cursorColId.CurrentDataValue.ValidValue
    # now apply rules for scoring
    if col1Val <= 3:
        score -= 3
    elif col1Val > 3 and col2Val > 50:
        score += 10
    else:
        score += 5
    text += "%d\t%f\r\n" % (id, score)

add_column(table, text, 'Score_Result')

对于没有脚本但也没有积累的方法,您可以使用计算列。要获得分数,您可以使用带有案例语句的计算列。对于第 1 段,您可能有:

case 
when [Col1] > 100 then 10
when [Col1] < 100 and [Col2] > 600 then 20
end

获得分数后,您可以创建一个计算列,例如 [MaxSegment]。表达式为 Max([Segment1],[Segment2],[Segment3]...)。然后显示 [MaxSegment] 的值。

在这种情况下,max 函数充当行表达式,并计算给定列的行的最大值。

于 2019-06-26T16:39:06.480 回答