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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch 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
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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.