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
omarevp
Helper II
Helper II

Difference between rows with different categories

Hello guys,this time I need you again.

 

This is my sample data:

DateUnitExtractedWorkinghours
3/1/2019 7:14P-19274590025.798
3/1/2019 8:05BD15384296.7
3/1/2019 8:27APV2526.7775.313889
3/1/2019 8:30P-203651975.89889
3/1/2019 9:57APV29721558.49778
3/1/2019 10:11APV2793576.25
3/4/2019 10:57P-20611982.22111
3/4/2019 11:11P-201131982.30833
3/4/2019 11:20TP0598-348201
3/4/2019 12:51APV2536.1778.898889
3/4/2019 14:16APV27462594.3
3/4/2019 14:35APV29531564.17278
3/5/2019 13:21P-19290590025.798
3/5/2019 14:03P-201031992.285
3/5/2019 14:19BD15125314.65
3/5/2019 14:33APV2549.2784.253889
3/5/2019 14:42APV2568784.270556
3/5/2019 16:00APV294041568.31722
3/5/2019 16:35RE01478301.401869
3/6/2019 9:56P-19277590025.798
3/6/2019 13:16BD15110322.75
3/6/2019 14:08APV27163604.8
3/7/2019 9:44P-201032005.85944
3/7/2019 9:54DCP0122199.65
3/7/2019 10:26APV27127619.776389
3/7/2019 10:37APV291101581.94833
3/8/2019 9:30P-202042014.965

 

I need a Calculated Column that measures the difference between Workinghours in one date, and Workinghours the date before, this should be, considering the "Unit" field. Ex:

 

DateUnitExtractedWorkinghoursDIFF (desired)
3/1/2019 10:11APV2793576.25 
3/4/2019 14:16APV27462594.318.05
3/6/2019 14:08APV27163604.810.5
3/7/2019 10:26APV27127619.77638914.9763889
3/11/2019 10:14APV27137627.3055567.52916667
3/15/2019 10:32APV27295648.61222221.3066667
3/18/2019 10:34APV27492663.07514.4627778
3/22/2019 9:56APV2720370056341.925

 

I need the same calculation for every unit in the same column.

 

Thanks in advance! All of your help would be really appreciate!

Omar.

1 ACCEPTED SOLUTION
danno
Resolver V
Resolver V

Hi, You could do this in one measure, but here are 2 steps to get a result: 

Previous Working Hours =
IF(HASONEVALUE(Data[Unit]),
VAR CurrentUnit = SELECTEDVALUE(Data[Unit])
VAR CurrentDate = SELECTEDVALUE(Data[Date])
VAR PreviousDate =
    CALCULATE (
        MAX ( Data[Date] ),
        FILTER ( ALL(Data), Data[Unit] = CurrentUnit && Data[Date] < CurrentDate)
    )
RETURN
LOOKUPVALUE(Data[Workinghours], Data[Date], PreviousDate)
,BLANK())
 
Diff = IF(ISBLANK([Previous Working Hours]), BLANK(), SELECTEDVALUE(Data[Workinghours]) - [Previous Working Hours])
 
and here are the test results 
 
DateUnitExtractedWorkinghoursPrevious Working HoursDiff
03/01/2019 08:27APV2526.7775.313889  
03/04/2019 12:51APV2536.1778.898889775.3138893.585
03/05/2019 14:33APV2549.2784.253889778.8988895.355
03/05/2019 14:42APV2568784.270556784.2538890.016667
03/01/2019 10:11APV2793576.25  
03/04/2019 14:16APV27462594.3576.2518.05
03/06/2019 14:08APV27163604.8594.310.5
03/07/2019 10:26APV27127619.776389604.814.976389
03/01/2019 09:57APV29721558.49778  
03/04/2019 14:35APV29531564.172781558.497785.675
03/05/2019 16:00APV294041568.317221564.172784.14444
03/07/2019 10:37APV291101581.948331568.3172213.63111
03/01/2019 08:05BD15384296.7  
03/05/2019 14:19BD15125314.65296.717.95
03/06/2019 13:16BD15110322.75314.658.1
03/07/2019 09:54DCP0122199.65  
03/01/2019 07:14P-19274590025.798  
03/05/2019 13:21P-19290590025.798590025.7980
03/06/2019 09:56P-19277590025.798590025.7980
03/01/2019 08:30P-203651975.89889  
03/04/2019 10:57P-20611982.221111975.898896.32222
03/04/2019 11:11P-201131982.308331982.221110.08722
03/05/2019 14:03P-201031992.2851982.308339.97667
03/07/2019 09:44P-201032005.859441992.28513.57444
03/08/2019 09:30P-202042014.9652005.859449.10556
03/05/2019 16:35RE01478301.401869  
03/04/2019 11:20TP0598-348201  

View solution in original post

3 REPLIES 3
danno
Resolver V
Resolver V

Hi, You could do this in one measure, but here are 2 steps to get a result: 

Previous Working Hours =
IF(HASONEVALUE(Data[Unit]),
VAR CurrentUnit = SELECTEDVALUE(Data[Unit])
VAR CurrentDate = SELECTEDVALUE(Data[Date])
VAR PreviousDate =
    CALCULATE (
        MAX ( Data[Date] ),
        FILTER ( ALL(Data), Data[Unit] = CurrentUnit && Data[Date] < CurrentDate)
    )
RETURN
LOOKUPVALUE(Data[Workinghours], Data[Date], PreviousDate)
,BLANK())
 
Diff = IF(ISBLANK([Previous Working Hours]), BLANK(), SELECTEDVALUE(Data[Workinghours]) - [Previous Working Hours])
 
and here are the test results 
 
DateUnitExtractedWorkinghoursPrevious Working HoursDiff
03/01/2019 08:27APV2526.7775.313889  
03/04/2019 12:51APV2536.1778.898889775.3138893.585
03/05/2019 14:33APV2549.2784.253889778.8988895.355
03/05/2019 14:42APV2568784.270556784.2538890.016667
03/01/2019 10:11APV2793576.25  
03/04/2019 14:16APV27462594.3576.2518.05
03/06/2019 14:08APV27163604.8594.310.5
03/07/2019 10:26APV27127619.776389604.814.976389
03/01/2019 09:57APV29721558.49778  
03/04/2019 14:35APV29531564.172781558.497785.675
03/05/2019 16:00APV294041568.317221564.172784.14444
03/07/2019 10:37APV291101581.948331568.3172213.63111
03/01/2019 08:05BD15384296.7  
03/05/2019 14:19BD15125314.65296.717.95
03/06/2019 13:16BD15110322.75314.658.1
03/07/2019 09:54DCP0122199.65  
03/01/2019 07:14P-19274590025.798  
03/05/2019 13:21P-19290590025.798590025.7980
03/06/2019 09:56P-19277590025.798590025.7980
03/01/2019 08:30P-203651975.89889  
03/04/2019 10:57P-20611982.221111975.898896.32222
03/04/2019 11:11P-201131982.308331982.221110.08722
03/05/2019 14:03P-201031992.2851982.308339.97667
03/07/2019 09:44P-201032005.859441992.28513.57444
03/08/2019 09:30P-202042014.9652005.859449.10556
03/05/2019 16:35RE01478301.401869  
03/04/2019 11:20TP0598-348201  

@dannoThanks! I think it's really useful and it's working good..

 

There's something missing that I forgot, sometimes the workinghours might be a 0 value, so, the measure should take the previous value different than 0, Ex:

date                   workinghours    previous workinghours

12/03/2019              110                        

13/03/2019               0                             110

14/03/2019              210                          110

15/03/2019              140                          210

 

I tried to do it with your measure, but I couldn't get it.

 

Will you please tell me?

 

Thanks for your help!

Omar

parry2k
Super User
Super User

@omarevp try this didn't spent much time but i thnk it will work

 

Diff = 
VAR __minDate = CALCULATE( MAX( Table8[Date] ), FILTER( ALLEXCEPT( Table8, Table8[Unit] ), Table8[Date] < MAX( Table8[Date] ) ) )
VAR __PrevValue = CALCULATE( MIN( Table8[Workinghours] ),  ALLEXCEPT( Table8, Table8[Unit] ), Table8[Date] = __minDate) 
RETURN
   IF(NOT HASONEFILTER( Table8[Date] ), BLANK(), IF (__PrevValue = BLANK() , 0, SELECTEDVALUE( Table8[Workinghours] ) - __PrevValue)   )


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

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.