Forum Discussion

Pan_Forex's avatar
Pan_Forex
Helper III
2 years ago
Solved

row minus previous row

Hello, I am trying to count the difference in points between the latest ID and the previous one for each employee. I have a very simple bug somewhere and I can't find it... 

id        employe         points        output
200A100-10
200B15010
200C13050
190A110 
190B14040
190C8030
180C50 
180B100 

 

output =
VAR CurrentPoints = 'Table'[points]
VAR PreviousPoints =
    CALCULATE(
        MAX('Table'[points]),
        FILTER(
             'Table',
            'Table'[emloye] = EARLIER('Table'[emloye]) &&
            'Table'[id] < EARLIER('Table'[id])
            )
        )
   
RETURN
    IF(ISBLANK('Table'[id]), BLANK(), CurrentPoints - PreviousPoints)


My function returns different values than the output from the table as if it was looking for the maximum value from all rows.

  • Pan_Forex 

    Total Points = SUM ( pan[points] )
    Output = 
    VAR PreviousValue = 
        CALCULATE ( 
            [Total Points],
            OFFSET ( 
                -1,
                ALL ( pan[employee], pan[id] ),
                ORDERBY ( pan[id], ASC ),
                PARTITIONBY ( pan[employee] )
            )
        )
    VAR CurrentValue = 
        [Total Points]
    VAR GroupingByID = 
        ISINSCOPE ( pan[id] )
    VAR Result = 
        IF ( 
            NOT ISBLANK ( PreviousValue )
                && GroupingByID,
            CurrentValue - PreviousValue
        )
    RETURN
        Result

     

7 Replies

  • Hi,

     

    Create a new column to calculate the difference in points between the current row and the previous row for the same employee:

    Points Difference =
    VAR CurrentPoints = [points]
    VAR PreviousPoints =
        CALCULATE (
            MAX ( 'Table'[points] ),
            FILTER (
                ALL ( 'Table' ),
                'Table'[id] < EARLIER ( 'Table'[id] )
                && 'Table'[employee] = EARLIER ( 'Table'[employee] )
            )
        )
    RETURN
    IF ( NOT ISBLANK ( PreviousPoints ), CurrentPoints - PreviousPoints )

     

    • Pan_Forex's avatar
      Pan_Forex
      Helper III

      Thanks for your answer 🙂 It should work but yet a message about insufficient memory appears. 

  • AntrikshSharma's avatar
    AntrikshSharma
    Community Champion

    Pan_Forex 

    Total Points = SUM ( pan[points] )
    Output = 
    VAR PreviousValue = 
        CALCULATE ( 
            [Total Points],
            OFFSET ( 
                -1,
                ALL ( pan[employee], pan[id] ),
                ORDERBY ( pan[id], ASC ),
                PARTITIONBY ( pan[employee] )
            )
        )
    VAR CurrentValue = 
        [Total Points]
    VAR GroupingByID = 
        ISINSCOPE ( pan[id] )
    VAR Result = 
        IF ( 
            NOT ISBLANK ( PreviousValue )
                && GroupingByID,
            CurrentValue - PreviousValue
        )
    RETURN
        Result

     

    • Pan_Forex's avatar
      Pan_Forex
      Helper III

      It is not large, as it has about 100,000 records

      • Kaviraj11's avatar
        Kaviraj11
        Solution Sage

        Would need more information to understand the root cause. Are you getting the error after creating a calculated column? and make sure the dataset is sorted as well