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
PaisleyPrince
Helper II
Helper II

Calculate running sum of previous n rows

Hi,

I'm trying to create a loop in power query to calculate the total of the previous 8 rows as per the screenshot below. Any advice would be much appreciated.

thanks

Scott

sm1.png

3 ACCEPTED SOLUTIONS
shafiz_p
Super User
Super User

Hi @PaisleyPrince  Try this in custom column:

= if [Index] < 8 then "" else List.Sum(List.Range(#"Changed Type"[Value], [Index] - 8, 8))

 

See image:

shafiz_p_0-1732419240736.png

 

 

 

Hope this helps!!

If this solved your problem, please accept it as a solution and a kudos!!

 

 

Best Regards,
Shahariar Hafiz

View solution in original post

AlienSx
Super User
Super User

this one works 100 times (literally - one hundred times) faster on a small set of 5000 rows.

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], 
    values = List.Buffer(Source[Value]),
    rt = List.Generate(
        () => [i = 0, ttl = values{0}],
        (x) => x[i] < List.Count(values),
        (x) => [i = x[i] + 1, ttl = x[ttl] + values{i}],
        (x) => x[ttl]
    ), 
    last_n = List.Transform(List.Zip({rt, List.Repeat({null}, 8 - 1) & {0} & List.RemoveLastN(rt, 8)}), (x) => x{0} - x{1}),
    result = Table.FromColumns(Table.ToColumns(Source) & {last_n}, Table.ColumnNames(Source) & {"Running Sum"})
in
    result

 

View solution in original post

slorin
Super User
Super User

Hi @PaisleyPrince 

 

Adapt @shafiz_p's  solution with List.Buffer

simplier but twice slower than @AlienSx 's proposition with 20000 rows

 

let
Source = YourSource,
Values = List.Buffer(Source[Value]),
#"Running Sum" = Table.AddColumn(Source, "Running Sum",
each if [Index] < 8 then "" else List.Sum(List.Range(Values, [Index] - 8, 8)))
in
#"Running Sum"

Stéphane

View solution in original post

5 REPLIES 5
slorin
Super User
Super User

Hi @PaisleyPrince 

 

Adapt @shafiz_p's  solution with List.Buffer

simplier but twice slower than @AlienSx 's proposition with 20000 rows

 

let
Source = YourSource,
Values = List.Buffer(Source[Value]),
#"Running Sum" = Table.AddColumn(Source, "Running Sum",
each if [Index] < 8 then "" else List.Sum(List.Range(Values, [Index] - 8, 8)))
in
#"Running Sum"

Stéphane

AlienSx
Super User
Super User

this one works 100 times (literally - one hundred times) faster on a small set of 5000 rows.

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], 
    values = List.Buffer(Source[Value]),
    rt = List.Generate(
        () => [i = 0, ttl = values{0}],
        (x) => x[i] < List.Count(values),
        (x) => [i = x[i] + 1, ttl = x[ttl] + values{i}],
        (x) => x[ttl]
    ), 
    last_n = List.Transform(List.Zip({rt, List.Repeat({null}, 8 - 1) & {0} & List.RemoveLastN(rt, 8)}), (x) => x{0} - x{1}),
    result = Table.FromColumns(Table.ToColumns(Source) & {last_n}, Table.ColumnNames(Source) & {"Running Sum"})
in
    result

 

Anonymous
Not applicable

Hi @PaisleyPrince ,

It looks like shafiz_p's answer can help you solve your problem. If solved please accept the reply in this post which you think is helpful as a solution to help more others facing the same problem to find a solution quickly, thank you very much!

Best Regards,
Dino Tao

Omid_Motamedise
Super User
Super User

= if [Index] < 8 then "" else List.Sum(List.LastN(List.FirstN(Source[Value], [Index] ), 8))

 

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
shafiz_p
Super User
Super User

Hi @PaisleyPrince  Try this in custom column:

= if [Index] < 8 then "" else List.Sum(List.Range(#"Changed Type"[Value], [Index] - 8, 8))

 

See image:

shafiz_p_0-1732419240736.png

 

 

 

Hope this helps!!

If this solved your problem, please accept it as a solution and a kudos!!

 

 

Best Regards,
Shahariar Hafiz

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.