You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spotfire技术问询:按TYPE分组计算指定活动的日期天数差

How to Calculate Date Difference Between Specific Activities per TYPE in Spotfire

Hey there! Totally get where you’re coming from—when you’re new to Spotfire, combining functions like DateDiff and OVER can feel a bit tricky at first, especially when you need to pull specific dates based on activity labels. Let’s break this down into simple, actionable steps to get you the result you need.

Core Approach

We need to:

  • First, extract the ACTIVITY 1 START date for each unique TYPE
  • Then, extract the ACTIVITY 3 FINISH date for each unique TYPE
  • Finally, calculate the day difference between these two dates using DateDiff, grouped by TYPE

Step-by-Step Calculated Column Expression

You can create a new calculated column directly in Spotfire with this combined expression:

DateDiff(
    "day",
    Max(CASE WHEN [ACTIVITY] = "ACTIVITY 1" THEN [ACTIVITY 1 START] END) OVER (Partition By [TYPE]),
    Max(CASE WHEN [ACTIVITY] = "ACTIVITY 3" THEN [ACTIVITY 3 FINISH] END) OVER (Partition By [TYPE])
)

Let’s break down what each part does:

  • CASE WHEN [ACTIVITY] = "ACTIVITY 1" THEN [ACTIVITY 1 START] END: Filters your data to only grab the start date when the activity matches exactly "ACTIVITY 1" (double-check spelling and case match your actual data!)
  • OVER (Partition By [TYPE]): Ensures we’re only evaluating records within the same TYPE group—so we never mix dates from different types
  • Max(...): Since each TYPE should have one entry for "ACTIVITY 1" and one for "ACTIVITY 3", Max (or First/Last) will reliably grab that single valid date value
  • DateDiff("day", start_date, end_date): Computes the number of days between the start and finish dates, using "day" as the measurement unit

Handling Edge Cases

If some TYPE entries are missing either "ACTIVITY 1" or "ACTIVITY 3", the result will be NULL. To handle this (e.g., replace NULL with 0 or a custom placeholder), wrap the entire expression in IsNull():

IsNull(
    DateDiff(
        "day",
        Max(CASE WHEN [ACTIVITY] = "ACTIVITY 1" THEN [ACTIVITY 1 START] END) OVER (Partition By [TYPE]),
        Max(CASE WHEN [ACTIVITY] = "ACTIVITY 3" THEN [ACTIVITY 3 FINISH] END) OVER (Partition By [TYPE])
    ),
    0  -- Replace 0 with whatever value you want for missing data
)

内容的提问来源于stack exchange,提问作者Victoria Grant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:11:19