简体   繁体   English

如何在POWER BI中减去不同行上的日期

[英]How to subtract dates on different rows in POWER BI

I have a table that looks like this: 我有一个看起来像这样的表:

在此处输入图片说明

Bear with me this is going to get a little confusing; 忍受这会有点混乱;

I want to get the values in the desired column. 我想获取所需列中的值。 The calculation is: StartTime-(previous row)Endtime . 计算公式为: StartTime-(上一行)Endtime So row 2 (Starttime) minus row 1 Endtime would be 17:04:48 minus 17:04:31 (=17sec). 因此,第2行(开始时间)减去第1行的结束时间为17:04:48减去17:04:31(= 17秒)。 However I want to exclude rows where CustAgentFl = 0 AND Transferflag = 0 before doing the date subtract calculation. 但是我想在进行日期减法计算之前排除 CustAgentFl = 0 AND Transferflag = 0的行。 Also, if Starttime- Endtime is <0 then just 0. 另外,如果Starttime- Endtime <0,则仅为0。

The rows are all grouped by the same NID, so of course the DAX query will need to group by the NID. 所有行均按相同的NID分组,因此DAX查询当然需要按NID分组。

If you can use the database to calculate these values for you, you can use LAG windowing function to get value from the previous row. 如果可以使用数据库为您计算这些值,则可以使用LAG窗口函数从上一行获取值。 However, in databases there is no obvious previous row, because the ordering of rows is undefined. 但是,在数据库中没有明显的前一行,因为行的顺序是不确定的。 So to get the value of the previous row, you must define the order. 因此,要获取上一行的值,必须定义顺序。 This is the purpose of the ORDER BY in the OVER clause. 这是OVER子句中ORDER BY的目的。 I will assume that your order is by StartTime. 我将假设您的订单是在StartTime之前完成的。 In this case you can use code like this: 在这种情况下,您可以使用如下代码:

declare @Table table(NID int, DgAcs char(1), CustAgentFl int, AgentId int, StartTime datetime, EndTime datetime, TransferFlag int, Desired int)

insert into @Table values
    (4565,  'C', 1, 358746, '2018-09-08 17:02:37', '2018-09-08 17:04:31', 1, 0), 
    (4565,  'C', 1, 358714, '2018-09-08 17:04:48', '2018-09-08 17:08:17', 1, 17), 
    (4565,  'C', 1, 359548, '2018-09-08 17:07:07', '2018-09-08 17:13:41', 1, 0), 
    (4565,  'C', 1, 358749, '2018-09-08 17:13:54', '2018-09-08 17:21:09', 1, 13), 
    (4565,  'A', 1, 351897, '2018-09-08 17:19:09', '2018-09-08 17:20:36', 0, 0), 
    (4565,  'C', 1, 358896, '2018-09-08 17:21:08', '2018-09-08 17:26:00', 0, 0)


; with cte as (
select
    *
    , LAG(EndTime, 1) over(order by StartTime) as PrevEndTime
    , DATEDIFF(s, LAG(EndTime, 1) over(order by StartTime), StartTime) as SecondsSinceLastEndNullable
    , ISNULL(DATEDIFF(s, LAG(EndTime, 1) over(order by StartTime), StartTime), 0) as SecondsSinceLastEnd
from @Table 
)

select
    *
    , iif(SecondsSinceLastEnd <= 0, '00:00:00', concat(SecondsSinceLastEnd / 3600, ':', FORMAT((SecondsSinceLastEnd / 60) % 60, 'D2'), ':', FORMAT(SecondsSinceLastEnd % 60, 'D2')))
    , iif(SecondsSinceLastEnd <= 0, '00:00:00', CONVERT(varchar, DATEADD(s, SecondsSinceLastEnd, 0), 108))
from cte

You formatted your desired output as time, but please note the difference between two moments in time isn't exactly time. 您将所需的输出格式设置为时间,但是请注意,两个时间之间的时差并非完全是时间。 It could be more than 24 hours. 可能会超过24小时。 I showed you two ways to convert the number of seconds difference in the desired format. 我向您展示了两种以所需格式转换秒数差异的方法。 The second one uses CONVERT to get TIME value, but as I said, this will not work for gaps longer than 24 hours. 第二个使用CONVERT来获取TIME值,但是正如我所说,这对于超过24小时的间隔不起作用。 The first way will give you the duration in H:MM:SS format, where H is the number of hours, which could be greater than 23. 第一种方法将以H:MM:SS格式提供持续时间,其中H是小时数,可以大于23。

In Power BI you can write this as custom query . 在Power BI中,您可以将其编写为自定义查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM