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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

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

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.