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
Anonymous
Not applicable

Adding new column based on difference of rows

Hi,

 

I am new to Power BI and struggling with something which otherwise must be simple I believe. I would like to have a difference of IN and OUT timings for a particular license for different days. This would basically tell the total time spent by the user with the license on a given day. 

 

I might explain it better with an example so here it goes:

 

The original source table:

timesourcedirectiontypeusernamemachinedate
10:21:36 AMARCGIS INARC/INFOvpdw2204/8/2019
10:21:36 AMARCGIS INGridvpdw2204/8/2019
10:22:06 AMARCGIS OUTARC/INFOvpdw2204/8/2019
10:22:14 AMARCGIS OUTGridvpdw2204/8/2019
11:21:36 AMARCGIS INARC/INFOvpdw2204/9/2019
11:28:06 AMARCGIS OUTARC/INFOvpdw2204/9/2019

 

The table post adding a new column:

 

 

timesourcedirectiontypeusernamemachinedateTime Spent
10:21:36 AMARCGIS INARC/INFOvpdw2204/8/20190:00:30
10:21:36 AMARCGIS INGridvpdw2204/8/20190:00:38
10:22:06 AMARCGIS OUTARC/INFOvpdw2204/8/2019NULL
10:22:14 AMARCGIS OUTGridvpdw2204/8/2019NULL
11:21:36 AMARCGIS INARC/INFOvpdw2204/9/20190:06:30
11:28:06 AMARCGIS OUTARC/INFOvpdw2204/9/2019NULL

 

Here the first row of the new column is: (Time of OUT direction for ARC/INFO license on 4/8/2019) - (Time of IN direction for ARC/INFO license on 4/8/2019) 

Similarly, the second row of the new column is: (Time of OUT direction for Grid license on 4/8/2019) - (Time of IN direction for Grid license on 4/8/2019) 

the third row of the new column is: (Time of OUT direction for ARC/INFO license on 4/9/2019) - (Time of IN direction for ARC/INFO license on 4/9/2019) 

1 ACCEPTED SOLUTION

@Anonymous  In that case, add a Rank field and will use this rank field in the logic instead of date

 

Rnk = RANKX(FILTER(Test303LkpTimeDiff,[Type]=EARLIER(Test303LkpTimeDiff[Type])),[Date]+[Time],,ASC) 

Then, the updated logic will be

TimeDiff = 
VAR _LkpOut = LOOKUPVALUE(Test303LkpTimeDiff[Time],Test303LkpTimeDiff[Type],Test303LkpTimeDiff[Type],Test303LkpTimeDiff[Direction],"OUT",[Rnk],[Rnk]+1)
RETURN IF(Test303LkpTimeDiff[Direction]="IN",_LkpOut-Test303LkpTimeDiff[Time],BLANK())

image.png





Did I answer your question? Mark my post as a solution!

Proud to be a PBI Community Champion




View solution in original post

3 REPLIES 3
PattemManohar
Community Champion
Community Champion

@Anonymous  Please add a New Column as below

 

TimeDiff = 
VAR _LkpOut = LOOKUPVALUE(Test303LkpTimeDiff[Time],Test303LkpTimeDiff[Type],Test303LkpTimeDiff[Type],Test303LkpTimeDiff[Direction],"OUT",Test303LkpTimeDiff[Date],Test303LkpTimeDiff[Date])
RETURN IF(Test303LkpTimeDiff[Direction]="IN",_LkpOut-Test303LkpTimeDiff[Time],BLANK())

image.png





Did I answer your question? Mark my post as a solution!

Proud to be a PBI Community Champion




Anonymous
Not applicable

Gives an error: 

 

Capture.PNG

 

I would like to add that there are multiple data in the sheet and not just ones I mentioned in the sample. Probably that could be causing the issue. I tried to merge the date and time column and named it 'DateTime' to apply the below DAX:

 

Lav_Inv_Score1 = 
VAR myID = 'LM_debuglog'[type]
VAR myLastDate = MAXX(FILTER('LM_debuglog','LM_debuglog'[type]=myID && 'LM_debuglog'[DateTime] < EARLIER('LM_debuglog'[DateTime])),'LM_debuglog'[DateTime])
RETURN SUMX(FILTER('LM_debuglog','LM_debuglog'[DateTime] = myLastDate && 'LM_debuglog'[type] = myID),'LM_debuglog'[DateTime])

However the above gives me an error. Any suggestions?

 

@Anonymous  In that case, add a Rank field and will use this rank field in the logic instead of date

 

Rnk = RANKX(FILTER(Test303LkpTimeDiff,[Type]=EARLIER(Test303LkpTimeDiff[Type])),[Date]+[Time],,ASC) 

Then, the updated logic will be

TimeDiff = 
VAR _LkpOut = LOOKUPVALUE(Test303LkpTimeDiff[Time],Test303LkpTimeDiff[Type],Test303LkpTimeDiff[Type],Test303LkpTimeDiff[Direction],"OUT",[Rnk],[Rnk]+1)
RETURN IF(Test303LkpTimeDiff[Direction]="IN",_LkpOut-Test303LkpTimeDiff[Time],BLANK())

image.png





Did I answer your question? Mark my post as a solution!

Proud to be a PBI Community Champion




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.