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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
hemann
Helper I
Helper I

Cumulative Sum in Power Query

Hi, 

I am trying to perform a cumulative sum in Power Query. Below is an example of the data and the deisred result. Can somebody please provide M language logic in Power Query to solve this. 

Current Data

End of MonthAttributeValue
31/12/2014Product A20
31/12/2014Product B2290
31/12/2014Product C0
31/12/2014Product D0
31/12/2014Product E0
31/12/2014Product F0
31/05/2015Product A3
31/05/2015Product B528
31/05/2015Product C0
31/05/2015Product D0
31/05/2015Product E0
31/05/2015Product F0
31/03/2015Product A10
31/03/2015Product B1472
31/03/2015Product C1
31/03/2015Product D0
31/03/2015Product E0
31/03/2015Product F0
31/01/2015Product A19
31/01/2015Product B4084
31/01/2015Product C1
31/01/2015Product D0
31/01/2015Product E0
31/01/2015Product F0
30/04/2015Product A5
30/04/2015Product B946
30/04/2015Product C1
30/04/2015Product D0
30/04/2015Product E0
30/04/2015Product F0
28/02/2015Product A16
28/02/2015Product B2492
28/02/2015Product C1
28/02/2015Product D0
28/02/2015Product E0
28/02/2015Product F0

 

Desired Result (Cumulative Sum)

End of MonthAttributeValueCumulative Sum
31/12/2014Product A2020
31/12/2014Product B22902290
31/12/2014Product C00
31/12/2014Product D00
31/12/2014Product E00
31/12/2014Product F00
31/05/2015Product A323
31/05/2015Product B5282818
31/05/2015Product C00
31/05/2015Product D00
31/05/2015Product E00
31/05/2015Product F00
31/03/2015Product A1033
31/03/2015Product B14724290
31/03/2015Product C11
31/03/2015Product D00
31/03/2015Product E00
31/03/2015Product F00
31/01/2015Product A1952
31/01/2015Product B40848374
31/01/2015Product C12
31/01/2015Product D00
31/01/2015Product E00
31/01/2015Product F00
30/04/2015Product A557
30/04/2015Product B9469320
30/04/2015Product C13
30/04/2015Product D00
30/04/2015Product E00
30/04/2015Product F00
28/02/2015Product A1673
28/02/2015Product B249211812
28/02/2015Product C14
28/02/2015Product D00
28/02/2015Product E00
28/02/2015Product F00
1 ACCEPTED SOLUTION
ryan_mayu
Super User
Super User

@hemann 

It looks like that the data is not accumulating by date. Not sure if this is what you want.

If you want to accumulate by date, you can try this

 

= Table.AddColumn(#"Added Index", "Custom", each List.Sum( Table.SelectRows(#"Added Index",(x)=>x[End of Month]<=[End of Month] and x[Attribute]=[Attribute] )[Value]))

 

if just need to accumulate by the row order, you need to create a index column , then try this

 

= Table.AddColumn(#"Added Custom", "Custom.1", each List.Sum( Table.SelectRows(#"Added Custom",(x)=>x[Index]<=[Index] and x[Attribute]=[Attribute] )[Value]))

 

11.png

 

pls see the attachment below





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

5 REPLIES 5
Royel
Solution Supplier
Solution Supplier

Hi @hemann your shared results Date colum is not sorted properly. 

However you can try this m-code to get the cumulative sum 

let
    Source = Excel.Workbook(File.Contents("C:\Users\RejaulIslamRoyel\Desktop\Cumulative Sum in Power Query.xlsx"), null, true),
    Sheet3_Sheet = Source{[Item="Sheet3",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet3_Sheet, [PromoteAllScalars=true]),
    
    // Fix: Handle DD/MM/YYYY date format conversion
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{
        {"End of Month", type text},  // Keep as text first
        {"Attribute", type text}, 
        {"Value", Int64.Type}
    }),
    
    // Convert DD/MM/YYYY text to proper date
    #"Converted Dates" = Table.TransformColumns(#"Changed Type", {
        {"End of Month", each Date.FromText(_, [Format="dd/MM/yyyy"]), type date}
    }),
    #"Sorted Rows" = Table.Sort(#"Converted Dates",{{"End of Month", Order.Ascending}}),
    
    // Step 2: Add Index column (required for cumulative calculation)
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1),
    
    // Step 3: Add Cumulative Sum column
    #"Added Cumulative Sum" = Table.AddColumn(#"Added Index", "Cumulative Sum", 
        each List.Sum(
            List.FirstN(
                Table.SelectRows(#"Added Index", 
                    (row) => row[Attribute] = [Attribute] and row[Index] <= [Index]
                )[Value], 
                [Index] + 1
            )
        ), 
        type number
    ),
    
    // Step 4: Remove helper Index column
    #"Removed Index" = Table.RemoveColumns(#"Added Cumulative Sum", {"Index"})
in
    #"Removed Index"

 

Note: Update source with your Current Data 

 

Output: 

Royel_0-1755503850713.png

 

Did it Helped? ✔ Give a Kudo • Mark as Solution – help others too! 

ryan_mayu
Super User
Super User

@hemann 

It looks like that the data is not accumulating by date. Not sure if this is what you want.

If you want to accumulate by date, you can try this

 

= Table.AddColumn(#"Added Index", "Custom", each List.Sum( Table.SelectRows(#"Added Index",(x)=>x[End of Month]<=[End of Month] and x[Attribute]=[Attribute] )[Value]))

 

if just need to accumulate by the row order, you need to create a index column , then try this

 

= Table.AddColumn(#"Added Custom", "Custom.1", each List.Sum( Table.SelectRows(#"Added Custom",(x)=>x[Index]<=[Index] and x[Attribute]=[Attribute] )[Value]))

 

11.png

 

pls see the attachment below





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Yes I wanted it to accumulate on date and product. Your first solution worked - thanks legend!!

you are welcome





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




jaineshp
Memorable Member
Memorable Member

Hey @hemann,

Here's the solution for calculating cumulative sum in Power Query using M language:

Solution Steps

Step 1: Sort Your Data First, ensure your data is properly sorted by Date and Attribute:

 

#"Sorted Rows" = Table.Sort(#"Previous Step",{{"End of Month", Order.Ascending}, {"Attribute", Order.Ascending}})

 

Step 2: Group by Attribute Group the data by Attribute to calculate cumulative sum for each product separately:

 

#"Grouped Rows" = Table.Group(#"Sorted Rows", {"Attribute"}, {
{"Data", each _, type table}
})

 

Step 3: Add Cumulative Sum Column Within each group, add the cumulative sum calculation:

 

#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each
Table.AddIndexColumn(
Table.AddColumn([Data], "Cumulative Sum",
each List.Sum(List.FirstN([Data][Value], [Index] + 1))
), "Index", 0
)
)

 

Step 4: Alternative Simpler Approach You can also use this more straightforward method by adding an index first:

 

Source = your_table,
#"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),
#"Added Custom Column" = Table.AddColumn(#"Added Index", "Cumulative Sum",
each List.Sum(
List.FirstN(
Table.SelectRows(#"Added Index",
each [Attribute] = #"Added Index"{[Index]}[Attribute] and
[Index] <= #"Added Index"{[Index]}[Index]
)[Value],
[Index] + 1
)
)
)

 

Step 5: Clean Up Remove the helper Index column and expand the results:

 

#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"End of Month", "Attribute", "Value", "Cumulative Sum"})

 

This approach will give you the exact cumulative sum pattern shown in your desired result, where each product maintains its own running total across different time periods.

 

Fixed? ✓ Mark it • Share it • Help others!


Best Regards,
Jainesh Poojara | Power BI Developer

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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