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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Pan_Forex
Helper III
Helper III

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.

1 ACCEPTED SOLUTION
AntrikshSharma
Super User
Super User

@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

AntrikshSharma_0-1718397223228.png

 

View solution in original post

7 REPLIES 7
AntrikshSharma
Super User
Super User

@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

AntrikshSharma_0-1718397223228.png

 

Kaviraj11
Super User
Super User

How large is the dataset?

 

Here are the articles to resolve the memory issues.

 How to Fix Memory Allocation Error in Power BI Desktop - Monocroft




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

Proud to be a Super User!





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

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




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

Proud to be a Super User!





Pan_Forex_1-1718388740132.png

 

I solved this problem and as you can see in the picture sometimes it counts right and sometimes wrong. What could this be due to?

Kaviraj11
Super User
Super User

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 )

Kaviraj11_0-1718374101262.png

 




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

Proud to be a Super User!





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

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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