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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
JapieTeunissen
New Member

Sum of last 3 rows

Hi

 

In my mind I scowerd the web to find examples, but failed.

 

I need sum the last 3 rows.

 

The left column is the index/rowno.

Middle column the values.

I need a way to calc the last column: which is the sum of this row's value + the values of the last 2 rows.

(I can't get Sum and TOPN to work.)

 

RowValueSumOfLast3Values
155
21015
31530
42045
52560
63075
73590
840105
945120
1050135

 

Regards

1 ACCEPTED SOLUTION
johnt75
Super User
Super User

You could do this either as a calculated column or as a measure, both have their advantaged and disadvantages. A measure would take account of any filters or slicers applied which a calculated column would not, but if it is a large dataset then a calculated column would likely give better performance.

To create a column you could use

Sum of last 3 values =
var currentRow = 'Table'[Row]
return CALCULATE( SUMX( TOPN(3, 'Table', 'Table'[Row]), 'Table'[Value]),
REMOVEFILTERS('Table'), 'Table'[Row] <= currentRow )

or as a measure

Sum of last 3 values =
var currentRow = SELECTEDVALUE('Table'[Row])
return CALCULATE( SUMX( TOPN(3, 'Table', 'Table'[Row]), 'Table'[Value]),
'Table'[Row] <= currentRow )

View solution in original post

4 REPLIES 4
moizsherwani
Continued Contributor
Continued Contributor

Hi @JapieTeunissen ,

 

I am sure there are multiple ways to do this, this was the first one which came to my mind. You need to create a measure which sums the "Value", second you need to create a rank of these SumValue and then you need to have a final running total which sums the rank of the last 3. See snippet, code and attached file

 

moizsherwani_0-1649172098570.png

 

 

 

ValueTotal =
SUM ( SampleData[Value] )

 

 

 

ValueTotalRank =
RANKX ( ALL ( SampleData ), [ValueTotal],, ASC )

 

 

 

ValueTotalRunning =
VAR CurrentValue =
    SELECTEDVALUE ( SampleData[Value] )
VAR CurrentRank = [ValueTotalRank]
VAR CurrentRankMinusThree = CurrentRank - 2
RETURN
    CALCULATE (
        [ValueTotal],
        FILTER (
            ALL ( SampleData ),
            [ValueTotalRank] >= CurrentRankMinusThree
                && [ValueTotalRank] <= CurrentRank
        )
    )

 

 

Thanks,

Moiz
Was I able to answer your question? Mark my post as a solution to help others. Kudos if you liked the solution.

Both solutions worked perfectly.

I have 30k rows, so I get "not enough memory..." for both, but that is not your problem.

johnt75
Super User
Super User

You could do this either as a calculated column or as a measure, both have their advantaged and disadvantages. A measure would take account of any filters or slicers applied which a calculated column would not, but if it is a large dataset then a calculated column would likely give better performance.

To create a column you could use

Sum of last 3 values =
var currentRow = 'Table'[Row]
return CALCULATE( SUMX( TOPN(3, 'Table', 'Table'[Row]), 'Table'[Value]),
REMOVEFILTERS('Table'), 'Table'[Row] <= currentRow )

or as a measure

Sum of last 3 values =
var currentRow = SELECTEDVALUE('Table'[Row])
return CALCULATE( SUMX( TOPN(3, 'Table', 'Table'[Row]), 'Table'[Value]),
'Table'[Row] <= currentRow )

I have 30k rows, so I get "not enough memory..." for both, but that is not your problem.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors