Spotfire date difference using over function

六眼飞鱼酱① 提交于 2019-12-02 04:22:49

EDIT 2015.07.15

got it, so if you want the difference from the last customer-date pair. this expression will give you the table you've listed above. spacing for readability:

DateDiff('day',
  First([Date) OVER (Intersect([ClientId], Previous([Date]))),
  [Date]
)


EDIT 2015.07.13

if you want to reduce this so that you can accurately aggregate [Days], you can surround the above expression with an If(). I'll add some spacing to make this more readable:

If(
    [Date] = Min([Date]) OVER Intersect([ClientId], [Item]), 
    DateDiff( 'day', 
        Min([Date]) OVER Intersect([ClientId], [Item]), 
        Max([Date]) OVER Intersect([ClientId], [Item])
    )
    , 0
)

in English: "If the value of the [Date] column in this row matches the earliest date for this [ItemId] and [ClientId] combination, then put the number of days difference between the first and last [Date] for this [ItemId] and [ClientId] combination; otherwise, put zero."

it results in something like:

Item    ClientId    Date        Days
A       102         2014.12.12  1
A       102         2014.12.13  0
B       141         2014.12.12  5
B       141         2014.12.17  0
C       123         2014.12.01  2
C       123         2014.12.02  0
C       123         2014.12.03  0

WARNING that filters may break this calculation. for example, if you are filtering based on [Date] and, with the above table as an example, filter OUT all dates before 2014.12.13, Sum([Date]) will be 7 instead of 8 (because the first row has been filtered out).


you can use Spotfire's OVER functions to look at data points with common IDs across rows.

it looks like you've only got two rows per Client ID and Item ID, which helps us out! use the following formula:

DateDiff('day', Min([Date]) OVER Intersect([ClientId], [Item]), Max([Date]) OVER Intersect([ClientId], [Item]))

this will give you a column with the number of days difference between the two dates in each row:

Item    ClientId    Date        Days
A       102         2014.12.12  1
A       102         2014.12.13  1
B       141         2014.12.12  5
B       141         2014.12.17  5
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!