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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
Dan_SP
Regular Visitor

Get Start & End Times for each state change

Hi there,

 

I want to help to extract the start and end times for each state change within a day and put it in a table to visualize the same. We have record for every 2 mins of the status of the agent. Note an agent can have same status multiple times in a day so everytime he changes state I want to know the start time and end time of the same.

 

My data looks something like this.

IndexUsernameStatusTimestamp
1ABAvailable4/22/2022 13:50
2ABAvailable4/22/2022 13:52
3ABAvailable4/22/2022 13:54
4ABBreak4/22/2022 13:56
5ABBreak4/22/2022 13:58
6ABBreak4/22/2022 14:00
7ABLunch 4/22/2022 14:02
8ABLunch 4/22/2022 14:04
9ABAvailable4/22/2022 14:06
10CDAvailable4/22/2022 13:30
11CDAvailable4/22/2022 13:32
12CDBreak4/22/2022 13:34
13CDBreak4/22/2022 13:36
14CDLunch 4/22/2022 13:38
15CDLunch 4/22/2022 13:40
16CDAvailable4/22/2022 13:42
17CDAvailable4/22/2022 13:44
18CDBreak4/22/2022 13:46
19CDBreak4/22/2022 13:48
20ABAvailable4/23/2022 13:00
21ABAvailable4/23/2022 13:02

 

What I want to achieve is 

UsernameStatusStartTime EndTime
ABAvailable4/22/2022 13:504/22/2022 13:54
ABBreak4/22/2022 13:564/22/2022 14:00
ABLunch4/22/2022 14:024/22/2022 14:04
ABAvailable4/22/2022 14:064/22/2022 14:06
CDAvailable4/22/2022 13:304/22/2022 13:32
CDBreak4/22/2022 13:344/22/2022 13:36
CDLunch4/22/2022 13:384/22/2022 13:40
CDAvailable4/22/2022 13:424/22/2022 13:44
CDBreak4/22/2022 13:464/22/2022 13:48
ABAvailable4/23/2022 13:004/23/2022 13:02

 

I tried getting previous state using this link and try something but I am getting a memory error (right now this table has 2 lakh records). I'd appreciate any help

1 ACCEPTED SOLUTION
Vijay_A_Verma
Super User
Super User

This can be very easily done through PQ using GroupKind.Local property of Table.Group. See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hdPPCoJAEMfxV5E9C+7Mjv+6ZR17A/FgIRRJh6Cev21nNwJ15iKCn8Pvu2rfGzC52Xffy3u8zeN5nvw9FYgFWsQM3K60Zsh7gzrEAJ0OKUBKsHtO432BqoBKGTUBVQKineWAOqHT63G5ZgvF6xtF8fRWafSQ54P1Dw5H6TQczwPQJU8ETHL1TBxPBCeruI+SWuv1jA8YSplRbKjUBooNtS5jRyN2UOxoZcUZaDfem/vJ+LHg1n/xJ33J8AE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, Username = _t, Status = _t, Timestamp = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"Username", type text}, {"Status", type text}, {"Timestamp", type datetime}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Username", "Status"}, {{"StartTime", each List.Min([Timestamp]), type nullable datetime}, {"EndTime", each List.Max([Timestamp]), type nullable datetime}}, GroupKind.Local)
in
    #"Grouped Rows"

 

 

View solution in original post

4 REPLIES 4
Vijay_A_Verma
Super User
Super User

This can be very easily done through PQ using GroupKind.Local property of Table.Group. See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hdPPCoJAEMfxV5E9C+7Mjv+6ZR17A/FgIRRJh6Cev21nNwJ15iKCn8Pvu2rfGzC52Xffy3u8zeN5nvw9FYgFWsQM3K60Zsh7gzrEAJ0OKUBKsHtO432BqoBKGTUBVQKineWAOqHT63G5ZgvF6xtF8fRWafSQ54P1Dw5H6TQczwPQJU8ETHL1TBxPBCeruI+SWuv1jA8YSplRbKjUBooNtS5jRyN2UOxoZcUZaDfem/vJ+LHg1n/xJ33J8AE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, Username = _t, Status = _t, Timestamp = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"Username", type text}, {"Status", type text}, {"Timestamp", type datetime}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Username", "Status"}, {{"StartTime", each List.Min([Timestamp]), type nullable datetime}, {"EndTime", each List.Max([Timestamp]), type nullable datetime}}, GroupKind.Local)
in
    #"Grouped Rows"

 

 

@Vijay_A_Verma , this is what I was looking for. Thank you so much for your help.

amitchandak
Super User
Super User

@Dan_SP , Create a new table like

summarize(Table,[Username],[Status], "Start Time", Min(Table[Timestamp]) , "End Time", Min(Table[Timestamp]))

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

@amitchandak, thanks for your response but this does not give me the desired result as it summarizes the status and gives the overall start and end time of a particular status. But what I want is every time the status changes the start time of it and the end time when the status changes to something different.

So if an agent went for a Break twice in a day I would want the start and end time of 1st break (same day) and start and end time of 2nd break (same day). But your solution gives Start time of 1st break and End Time of 2nd break. Hope I could put my point across correctly.

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

Check out the April 2025 Power BI update to learn about new features.

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors