I have a problem about the difference calculation for the rows using calculated column in Spotfire.
I am wondering if it is possible to create a calculated column that will count the difference between the current row with the next row which have different attribute. A sample table could be like this:
the result could be like this:
the basic row is:
- When type=1, then calculate the difference between its current value and its next nearest row which type=0, then add the result to a new calculated column.
- btw, the VALUE is always increase :)
- for example, for the first result 2, current value is 20, next row is the nearest type with 0, and the value of next row is 22, then the result is 2
- but for the next type=1, the current value is 25, and its nearest type=0 is on the sixth row, so the result could be 29-25=4
my method which i have tried:
- i added a new RowID column
then try the code:
if([type]=1),[value] - Sum([value]) OVER (PreviousPeriod([RowID])),null)
but it just show the difference between type 1, no type 1 and type 0 :(
Any help or suggestion would be greatly appreciated:)
thanks!
RowId()
and name itRowNum
([value] - first([value]) over (intersect(previous([type]),AllNext([RowNum])))) * -1
Here is what it will look like. I named the column
t1
. You can also ignore theVal
column:Explanation:
The trick here is to limit the values in the
OVER
clause to those that will come after the current row. Furthermore, we want to get the first, next available value which meets our criteria. So, we take the first value,first([value])
, that has it's previous[type]
.This always be 0 since there isn't any negative values for[type]
, thus this limits the rows we are working with to those with[type] = 1
by usingprevious([type])
. Now, to limit it to only the rows that come after the current row, we useAllNext([RowNum])
. TheIntersect
statement states take the value where both of these rules are met. So looking atRowNum = 4
it is evaluated like this.@ZAWD - Another way of solving this:
Step 1: Inserted a calculated column 'RowID' using the expression RowId()
Step 2: Inserted a calculated column 'test0' using the expression below
Step 3: Inserted a calculated column 'test' using the expression below
Step 4: Inserted a calculated column 'myresult' using the expression below
Note: 'test0' and 'test' columns run in the background. They need not be included in the main table
Final table looks like below:
Also, this solution works fine in whichever order the values are in. I have tested this solution with different scenarios and seems to be working fine.