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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. 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
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.