Forum Discussion

omarevp's avatar
omarevp
Helper II
7 years ago
Solved

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.

  • Anonymous's avatar
    Anonymous
    7 years ago

    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  

3 Replies

  • 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)   )
  • Anonymous's avatar
    Anonymous
    Not applicable

    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  
    • omarevp's avatar
      omarevp
      Helper II

      AnonymousThanks! 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