TIBCO Spotfire Analyst v7.10:基于两日期列对比创建两个新计算列
Hey there, let's walk through how to build that Rel Date column exactly as you specified. The core logic is to group rows by the same Id, filter out any cDate values that are larger than the row's Date, then pick the closest (i.e., maximum valid) cDate for each entry.
Step-by-Step Implementation
- Open your table in Spotfire, then go to Insert > Calculated Column from the top menu bar.
- Name your new column
Rel Date(or whatever you prefer) in the column name field. - Paste the following expression into the expression editor:
Max(If([cDate] <= [Date], [cDate], Null)) Over (Intersect([Id]))
- Double-check that the data type is set to Date (Spotfire usually detects this automatically, but it's good to confirm).
- Click OK to generate the column.
Breaking Down the Expression
Let's unpack what each part does to make sure it aligns with your requirements:
If([cDate] <= [Date], [cDate], Null): This filters out anycDatethat's larger than the current row'sDate(per your rule b), replacing invalid values withNull.Max(...): Since we want the closest valid date toDate, taking the maximum of the filteredcDatevalues gives us exactly that—if there's acDateequal toDate, it will be the max; otherwise, it's the nearest smaller date (rule a).Over (Intersect([Id])): This ensures we only calculate the max within rows that share the sameId, so we don't mix data across different IDs.
Handling Edge Cases
If you have rows where all cDate values for an Id are larger than the row's Date, the above expression will return Null. If you want to handle this (e.g., fall back to the row's Date itself), you can modify the expression with the Coalesce function:
Coalesce(Max(If([cDate] <= [Date], [cDate], Null)) Over (Intersect([Id])), [Date])
Coalesce returns the first non-null value in the list—so if no valid cDate exists, it will use the row's Date instead.
内容的提问来源于stack exchange,提问作者PineNuts0

