Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
rocky09
Solution Sage
Solution Sage

TAT Calculation based on Next Row

Here is my situation, I need to find TAT for a Ticket.  A Ticket may contains many activities. Basically, the activities types will be Outbound and Inbound. 

 

Requirement:

* Calculate TAT only when the first Row Has Outbound and next Row has Inbound.

* If only One Inbound/One outbound seen, just ignore calculating tat.

* TAT also should based on Ticket ID.

 

Please see below, so, you will get a clear picture of what I am after.

 

25452010/2/2017 11:03Outbound
25452010/2/2017 0:33Inbound
25452010/2/2017 18:40Outbound
25452010/2/2017 18:58Inbound
25452010/2/2017 19:58Outbound
25452510/3/2017 10:03Inbound
25452510/3/2017 10:48Outbound
25452510/4/2017 14:06Inbound

 

Expecting solution

 

IDDateTypeTAT
25452010/2/2017 11:03Outbound0.020833
25452010/2/2017 11:33Inbound 
25452010/2/2017 18:40Outbound0.0125
25452010/2/2017 18:58Inbound 
25452010/2/2017 19:58Outbound 
25452510/3/2017 10:03Inbound 
25452510/3/2017 10:48Outbound1.1375
25452510/4/2017 14:06Inbound 

 

see above Bold, there is outbound and inbound but the ticket ids are not matching. So, we can skip to calculate TAT.

2 ACCEPTED SOLUTIONS
MarcelBeug
Community Champion
Community Champion

It looks like TAT stands for Turn Around Time, at least the following Power Query solution returns the same results as your examples.

 

By adding a base-0 index and a base-1 index, you can merge the table with itself so you get the data from the next row on the current row.

 

let
    Source = Tickets,
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),
    #"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 1, 1),
    #"Merged Queries" = Table.NestedJoin(#"Added Index1",{"Index.1"},#"Added Index1",{"Index"},"Next",JoinKind.LeftOuter),
    #"Expanded Next" = Table.ExpandTableColumn(#"Merged Queries", "Next", {"ID", "Date", "Type"}, {"Next.ID", "Next.Date", "Next.Type"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Next", "TAT", each if [ID] = [Next.ID] and [Type] = "Outbound" and [Next.Type] = "Inbound" then [Next.Date] - [Date] else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index", "Index.1", "Next.ID", "Next.Date", "Next.Type"})
in
    #"Removed Columns"
Specializing in Power Query Formula Language (M)

View solution in original post

Ashish_Mathur
Super User
Super User

Hi,

 

Try this calculated column formula

 

=if(AND(LOOKUPVALUE([Type],[Date],CALCULATE(MIN(Data[Date]),FILTER(Data,[ID]=EARLIER([ID])&&Data[Date]>EARLIER(Data[Date]))))="inbound",[Type]="outbound"),CALCULATE(MIN(Data[Date]),FILTER(Data,[ID]=EARLIER([ID])&&Data[Date]>EARLIER(Data[Date])))-Data[Date],BLANK())

Download the workbook from here.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi,

 

Try this calculated column formula

 

=if(AND(LOOKUPVALUE([Type],[Date],CALCULATE(MIN(Data[Date]),FILTER(Data,[ID]=EARLIER([ID])&&Data[Date]>EARLIER(Data[Date]))))="inbound",[Type]="outbound"),CALCULATE(MIN(Data[Date]),FILTER(Data,[ID]=EARLIER([ID])&&Data[Date]>EARLIER(Data[Date])))-Data[Date],BLANK())

Download the workbook from here.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thank you @Ashish_Mathur and @MarcelBeug

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
MarcelBeug
Community Champion
Community Champion

It looks like TAT stands for Turn Around Time, at least the following Power Query solution returns the same results as your examples.

 

By adding a base-0 index and a base-1 index, you can merge the table with itself so you get the data from the next row on the current row.

 

let
    Source = Tickets,
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),
    #"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 1, 1),
    #"Merged Queries" = Table.NestedJoin(#"Added Index1",{"Index.1"},#"Added Index1",{"Index"},"Next",JoinKind.LeftOuter),
    #"Expanded Next" = Table.ExpandTableColumn(#"Merged Queries", "Next", {"ID", "Date", "Type"}, {"Next.ID", "Next.Date", "Next.Type"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Next", "TAT", each if [ID] = [Next.ID] and [Type] = "Outbound" and [Next.Type] = "Inbound" then [Next.Date] - [Date] else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index", "Index.1", "Next.ID", "Next.Date", "Next.Type"})
in
    #"Removed Columns"
Specializing in Power Query Formula Language (M)

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.