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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
jagostinhoCT
Post Partisan
Post Partisan

Splitting table rows from value

Hello,

 

I need to calculate the duration between Events for each particular user working in a specific file in a datasource that contains info from multiple users. Sample table below.

 

The goal is to get the Duration column to calculate the duration for the next user action in the same FileName.

 

A manual calculation in excel would be something like the following image.

So, basically, I want to subtract the next datetime row for the same user and filename to the current row.

If such next row does not exist then set the duration to 0 (as for the FileClose at the end, for instance).

 

Thank you for your help.

 

Duration.jpg

 

DateTimeUsernameFileNameEventNameViewNameDuration
18/02/2020 14:00:00AnnAFileOpen-00:00:00
18/02/2020 14:10:00AnnAViewActivatedLevel 2 - WRK01:00:00
18/02/2020 14:20:00AnnBFileOpen-00:00:00
18/02/2020 14:30:00AnnBViewActivatedLevel 300:50:00
18/02/2020 14:40:00BenAFileOpen-00:00:00
18/02/2020 14:50:00BenAViewActivatedLevel 2 - WRK00:10:00
18/02/2020 15:00:00BenAViewActivatedLegend00:30:00
18/02/2020 15:10:00AnnAIdle-00:30:00
18/02/2020 15:20:00AnnBFileClose-00:00:00
18/02/2020 15:30:00BenAFileClose-00:00:00
18/02/2020 15:40:00AnnAFileClose 00:00:00

 

1 ACCEPTED SOLUTION

Try

DateDiff = 'view-activated collected'[DateTime] - MINX(FILTER('view-activated collected',
					'view-activated collected'[DocPath] = EARLIER('view-activated collected'[DocPath]) 
				&& 'view-activated collected'[Username] = EARLIER('view-activated collected'[Username]) 
				&& 'view-activated collected'[DateTime] > EARLIER('view-activated collected'[DateTime])
				),'view-activated collected'[DateTime])

View solution in original post

5 REPLIES 5
ChrisMendoza
Resident Rockstar
Resident Rockstar

@jagostinhoCT -

You can do in Power Query as well:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDSN7TQNzIwMlAwNLEyMAAiJR0lx7w8EAnEbpk5qf4FqSCuLhAbQJXE6qBrNcTQGpaZWu6YXJJZlliSmgLk+6SWpeYoGCnoKoQHeQP5hjjNMkIxy4kUZxhjaMXuDGOwKaY4TDGBmuKUSnI4mGJoJRQOBtCwQzfLFB4d+MxKT81LARtijMMQzIjxTMlJhfsDlzbsceCck1+cSiAMTOGRgBp8xOk1wZoEYXqRtcYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [DateTime = _t, Username = _t, FileName = _t, EventName = _t, ViewName = _t, Duration = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DateTime", type text}, {"Username", type text}, {"FileName", type text}, {"EventName", type text}, {"ViewName", type text}, {"Duration", type time}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Username", "FileName"}, {{"Grouped", each _, type table [DateTime=text, Username=text, FileName=text, EventName=text, ViewName=text, Duration=time]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "AddIndex", each Table.AddIndexColumn([Grouped],"Index",1,1)),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom", each let tblName = [AddIndex] in Table.AddColumn([AddIndex],"NextDateTime", each tblName{[Index]}[DateTime])),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom1", "Custom", {"DateTime", "EventName", "ViewName", "Duration", "NextDateTime"}, {"DateTime", "EventName", "ViewName", "Duration", "NextDateTime"}),
    #"Replaced Errors" = Table.ReplaceErrorValues(#"Expanded Custom", {{"NextDateTime", null}}),
    #"Removed Columns" = Table.RemoveColumns(#"Replaced Errors",{"Grouped", "AddIndex"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns",{{"DateTime", type datetime}, {"NextDateTime", type datetime}}),
    #"Added Custom2" = Table.AddColumn(#"Changed Type1", "DurationCalculated", each Duration.TotalMinutes([NextDateTime]-[DateTime]))
in
    #"Added Custom2"





Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



amitchandak
Super User
Super User

Try like. Add or remove conditions as per need

Datediff = table[datetime] = minx(filter(table,table[file name] = earlier(table[file name]) && table[user name] = earlier(table[user name])
										 && table[datetime] > earlier(table[datetime])),table[datetime])

 

Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution.
In case it does not help, please provide additional information and mark me with @

Thanks. My Recent Blogs -Decoding Direct Query - Time Intelligence, Winner Coloring on MAP, HR Analytics, Power BI Working with Non-Standard TimeAnd Comparing Data Across Date Ranges
Proud to be a Datanaut Connect on Linkedin

thank you for such quick reply  , 

I am getting an error of type

"Too few arguments were passed to the MINX function. The minimum argument count for the function is 2."

the actual formula I am using is this

 

DateDiff = 'view-activated collected'[DateTime] = MINX(FILTER('view-activated collected','view-activated collected'[DocPath] = EARLIER('view-activated collected'[DocPath] && 'view-activated collected'[Username] = EARLIER('view-activated collected'[Username]) && ('view-activated collected'[DateTime] > EARLIER('view-activated collected'[DateTime])),'view-activated collected'[DateTime])))

 

@amitchandak

Try

DateDiff = 'view-activated collected'[DateTime] - MINX(FILTER('view-activated collected',
					'view-activated collected'[DocPath] = EARLIER('view-activated collected'[DocPath]) 
				&& 'view-activated collected'[Username] = EARLIER('view-activated collected'[Username]) 
				&& 'view-activated collected'[DateTime] > EARLIER('view-activated collected'[DateTime])
				),'view-activated collected'[DateTime])

It is working very well, thank you.

 

Found an unintended consequence of the requirement, though.

Can the last DateDiff calculation for the last entry for user+docpath be set to 0?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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