sql—power bi计算两条记录之间的日期差异时出现的问题

wrrgggsh  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(687)

我一直在做一个powerbi报告,它从jira的数据库中提取数据,并显示关于jira问题的各种信息。我正在为“状态中的时间”的一个度量而挣扎,它试图显示每个状态之间的日期差异。数据如下所示:

| Issue_ID  |  Old_Status  |  New_Status  |  Status_Change_Date   |  Issue_Created_Date  |  Sequence  |
=======================================================================================================
| JIRA-001  |    To Do     |  In Progress |  06/05/2020 08:00 AM  | 06/01/2020 04:00 PM  |     1      |
-------------------------------------------------------------------------------------------------------
| JIRA-001  |  In Progress |   Testing    |  06/07/2020 02:00 PM  | 06/01/2020 04:00 PM  |     2      |
-------------------------------------------------------------------------------------------------------
| JIRA-001  |   Testing    |     Done     |  06/10/2020 09:45 AM  | 06/01/2020 04:00 PM  |     3      |
-------------------------------------------------------------------------------------------------------
| JIRA-002  |    To Do     |  In Progress |  06/03/2020 09:00 AM  | 06/02/2020 11:30 AM  |     1      |
-------------------------------------------------------------------------------------------------------
| JIRA-002  |  In Progress |   Testing    |  06/03/2020 03:45 PM  | 06/02/2020 11:30 AM  |     2      |

我一直在尝试创建一个度量,它查看序列号,然后计算特定行的状态更改日期和序列中上一条记录的状态更改日期之间的日期/时间差。如果序列号为“1”,则应计算问题创建日期和状态更改日期之间的日期/时间差。
以下是我试图得到的结果(有些过于简单化):

| Issue_ID |    Status    |        Time_in_Status         |
===========================================================
| JIRA-001 |    To Do     |        3 Days, 8 Hours        |
-----------------------------------------------------------
| JIRA-001 |  In Progress |        2 Days, 6 Hours        |
-----------------------------------------------------------
| JIRA-001 |    Testing   |    2 Days, 19 Hours, 15 Mins  |
-----------------------------------------------------------
| JIRA-002 |    To Do     |        21 Hours , 30 Mins     |
-----------------------------------------------------------
| JIRA-002 |  In Progress |        6 Hours 45 Mins        |

我可以用状态为的时间显示一个带小数位的数字(例如2.25天)。但是,我尝试创建的度量方法似乎不起作用。

Time_in_Status = IF(x.[Sequence] = 1, 
                    DATEDIFF(x.[Issue_Created_Date], x.[Status_Change_Date], DAYS),
                    [I don't know how to do determine the DATEDIFF from the previous row by [Sequence]])

我知道这个方法很草率,但我对dax和power-bi还是很了解的。
有人知道我是如何用这些数据达到我想要的结果的吗?
更新:还有第二个表没有在datediff度量中使用,而是用于将其他数据拉入报表,并使用滑块过滤报表。这两个表由issue\u id连接,并且issue\u id在这两个表中都不不同。这是第二张table:

| Issue_ID  |  Issue_Title  |  Product_Team  |  Epic_ID   |  Epic_Version  |     Sprint     |   Component   |
=============================================================================================================
| JIRA-001  |  Do a thing   |      Mobile    |  JIRA-101  |     1.1.31     |   Sprint 1.1   |      iOS      |
-------------------------------------------------------------------------------------------------------------
| JIRA-001  |  Do a thing   |      Mobile    |  JIRA-101  |     1.1.31     |   Sprint 1.2   |      iOS      |
-------------------------------------------------------------------------------------------------------------
| JIRA-002  |  Do another   |      Mobile    |  JIRA-101  |     1.1.33     |   Sprint 1.1   |      iOS      |
-------------------------------------------------------------------------------------------------------------
| JIRA-003  |  Yet another  |       Web      |  JIRA-102  |     1.1.40     |   Sprint 1.1   |     Python    |
-------------------------------------------------------------------------------------------------------------

我使用的滑块/过滤器都来自第二个表(y)。这些过滤器是:
产品团队
史诗版
冲刺
成分

d5vmydt9

d5vmydt91#

另一个用户提供了答案,但是所有的评论都被删除了。他们提供的答案让我有95%的成功率,但有一个错误,我花了几个星期才找到答案。
实际的答案是创建两个度量:“状态分钟数”和“当前状态天数”
状态分钟数:

Minutes in Status = 
SUMX(
    'x',
    VAR Current_Sequence = 'x'[Sequence]
    VAR Current_Change_Date = 'x'[Change_Date]
    VAR Current_Created_Date = 'x'[Created_Date]
    VAR Issue_ID = 'x'[Issue_ID]
    VAR Previous_Change_Date = 
            CALCULATE(
                MAX('x'[Change_Date]),
                ALLEXCEPT('x', 'x[Issue_ID]),
                'x'[Sequence] = Current_Sequence - 1
            )
    VAR Previous_Date = 
            IF(
                Current_Sequence = 1, Current_Created_Date, Previous_Change_Date)
RETURN
    CALCULATE(
        DATEDIFF(
            Previous_Date, Current_Change_Date, MINUTE))
)

当前状态天数:

Days in Current Status = 
    VAR Total_Minutes = [Minutes in Status]
    VAR Total_Hours = INT(Total_Minutes/60)
    VAR Remaining_Minutes = MOD(Total_Minutes, 60)
    VAR Total_Days = INT(Total_Hours/24)
    VAR Remaining_Hours = MOD(Total_Hours, 24)

    VAR Formatted_Days = IF(Total_Days > 0, Total_Days & " d ")
    VAR Formatted_Hours = IF(Remaining_Hours > 0, Remaining_Minutes & " h ")
    VAR Formatted_Minutes = IF(Remaining_Minutes > 0, Remaining_Minutes & " m")
    VAR Result = Formatted_Days & Formatted_Hours & Formatted_Minutes

    RETURN
            Result

原始答案的问题是因为此人在格式化的if语句中输入了“days in current status”measure“”作为值\u if \u false。一旦我删除了值\u if \u false,一切都开始正常工作。

相关问题