Spotfire技术咨询:提取字符串间文本与基于数据计算新列
Hey there! Let's break down these two Spotfire tasks step by step—they’re super handy when working with text manipulation and calculated columns.
Spotfire doesn’t have a built-in "extract between" function, but we can combine Mid(), Find(), and If() to get the job done. Here’s how:
Basic Scenario (Strings Exist in the Text)
Suppose you have a column [RawText] with values like "ID:1234|Name:JohnDoe|Status:Active" and you want to extract the text between "Name:" and "|". Use this expression in a calculated column:
Mid([RawText], Find([RawText], "Name:") + Len("Name:"), Find([RawText], "|", Find([RawText], "Name:")) - (Find([RawText], "Name:") + Len("Name:")))
Let’s break this down:
Find([RawText], "Name:")gets the starting position of your first target string- Add
Len("Name:")to skip past the first string itself - The third argument in
Mid()calculates the length of the text between the two strings by finding the position of the second string after the first one, then subtracting the start position of your desired text
Handle Cases Where Strings Might Be Missing
To avoid errors if either string isn’t present, wrap it in an If() statement:
If(Find([RawText], "Name:") > 0 AND Find([RawText], "|", Find([RawText], "Name:")) > 0, Mid([RawText], Find([RawText], "Name:") + Len("Name:"), Find([RawText], "|", Find([RawText], "Name:")) - (Find([RawText], "Name:") + Len("Name:"))), "N/A")
This will return "N/A" instead of an error if the target strings aren’t found.
The approach here depends on whether you’re calculating values from existing columns using math formulas, or converting text-based formulas into calculated values. Let’s cover both:
Case 1: Calculate Values Using Math Formulas Directly
If you need to compute things like squared values, square roots, or complex equations, use Spotfire’s built-in functions in a calculated column. For example:
- To compute
(ColumnA² + √ColumnB) / ColumnC, use:(Pow([ColumnA], 2) + Sqrt([ColumnB])) / [ColumnC] - For trigonometric functions (like sine of an angle in radians):
Sin([AngleColumn])
Spotfire supports most standard math operations—just use the function list in the calculated column editor to browse options.
Case 2: Evaluate Text-Based Math Formulas
If your data has a column with stored formulas (e.g., [FormulaText] has values like "ColumnA * 3 + Log(ColumnD)"), you can use the Evaluate() function to compute the result:
Evaluate([FormulaText])
⚠️ Note: Evaluate() requires that the formula string references existing columns in your data table, and it won’t work with custom functions unless they’re registered in Spotfire.
Formatting the Result (e.g., Scientific Notation, Currency)
After calculating the value, you can format the column by right-clicking it > Column Properties > Formatting. Choose from options like scientific notation, currency, or custom number formats to match your needs.
内容的提问来源于stack exchange,提问作者user6499479




