简体   繁体   中英

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 . So row 2 (Starttime) minus row 1 Endtime would be 17:04:48 minus 17:04:31 (=17sec). However I want to exclude rows where CustAgentFl = 0 AND Transferflag = 0 before doing the date subtract calculation. Also, if Starttime- Endtime is <0 then just 0.

The rows are all grouped by the same NID, so of course the DAX query will need to group by the 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. 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. I will assume that your order is by 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. 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. 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.

In Power BI you can write this as custom query .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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