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
Anonymous
Not applicable

Add Column Cumulative Total in Power Query

I have a table containing Month and Value and would like to add one column with cumulative total. 

Below is the example I had:

 

Aziz_Arrashid_0-1639982488358.png

 

The logic is:

MONTH 1 = 0

MONTH 2 = Value MONTH 1

MONTH 3 = Value MONTH 1 + Value MONTH 2

MONTH 4 = Value MONTH 1 + Value MONTH 2 + Value MONTH 3

...

MONTH 9 = Value MONTH 1 + Value MONTH 2 + Value MONTH 3 + Value MONTH 4 + Value MONTH 5 + Value MONTH 6 + Value MONTH 7 + Value MONTH 8.

 

Since I have value until MONTH 8, therefore new row should be added which is MONTH 9 (summation of value MONTH 1 until MONTH 8). 

 

So far my solution is to add column and use this condition (which I find probably make POWER BI become slower) and I am unable to add one more row which is MONTH 9:

= Table.AddColumn(#"Previous Query", "CUMULATIVE", each if [MONTH] = 1 then 0 else if
[MONTH] = 2 then #"Previous Query"[VALUE]{0} else if
[MONTH] = 3 then #"Previous Query"[VALUE]{0}+#"Previous Query"[VALUE]{1} else if
[MONTH] = 4 then #"Previous Query"[VALUE]{0}+#"Previous Query"[VALUE]{1}+#"Previous Query"[VALUE]{2} else if
[MONTH] = 5 then #"Previous Query"[VALUE]{0}+#"Previous Query"[VALUE]{1}+#"Previous Query"[VALUE]{2}+#"Previous Query"[VALUE]{3} else if
[MONTH] = 6 then #"Previous Query"[VALUE]{0}+#"Previous Query"[VALUE]{1}+#"Previous Query"[VALUE]{2}+#"Previous Query"[VALUE]{3}+#"Previous Query"[VALUE]{4} else if
[MONTH] = 7 then #"Previous Query"[VALUE]{0}+#"Previous Query"[VALUE]{1}+#"Previous Query"[VALUE]{2}+#"Previous Query"[VALUE]{3}+#"Previous Query"[VALUE]{4}+#"Previous Query"[VALUE]{5} else if
[MONTH] = 8 then #"Previous Query"[VALUE]{0}+#"Previous Query"[VALUE]{1}+#"Previous Query"[VALUE]{2}+#"Previous Query"[VALUE]{3}+#"Previous Query"[VALUE]{4}+#"Previous Query"[VALUE]{5}+#"Previous Query"[VALUE]{6} else if
[MONTH] = 9 then #"Previous Query"[VALUE]{0}+#"Previous Query"[VALUE]{1}+#"Previous Query"[VALUE]{2}+#"Previous Query"[VALUE]{3}+#"Previous Query"[VALUE]{4}+#"Previous Query"[VALUE]{5}+#"Previous Query"[VALUE]{6}+#"Previous Query"[VALUE]{7} else if
[MONTH] = 10 then #"Previous Query"[VALUE]{0}+#"Previous Query"[VALUE]{1}+#"Previous Query"[VALUE]{2}+#"Previous Query"[VALUE]{3}+#"Previous Query"[VALUE]{4}+#"Previous Query"[VALUE]{5}+#"Previous Query"[VALUE]{6}+#"Previous Query"[VALUE]{6}+#"Previous Query"[VALUE]{7}+#"Previous Query"[VALUE]{8} else if
[MONTH] = 11 then #"Previous Query"[VALUE]{0}+#"Previous Query"[VALUE]{1}+#"Previous Query"[VALUE]{2}+#"Previous Query"[VALUE]{3}+#"Previous Query"[VALUE]{4}+#"Previous Query"[VALUE]{5}+#"Previous Query"[VALUE]{6}+#"Previous Query"[VALUE]{6}+#"Previous Query"[VALUE]{7}+#"Previous Query"[VALUE]{8}+#"Previous Query"[VALUE]{9} else
#"Previous Query"[VALUE]{0}+#"Previous Query"[VALUE]{1}+#"Previous Query"[VALUE]{2}+#"Previous Query"[VALUE]{3}+#"Previous Query"[VALUE]{4}+#"Previous Query"[VALUE]{5}+#"Previous Query"[VALUE]{6}+#"Previous Query"[VALUE]{7}+#"Previous Query"[VALUE]{8}+#"Previous Query"[VALUE]{9}+#"Previous Query"[VALUE]{9})

 

I would appreciate if someone can provide me much more simpler calculation

1 ACCEPTED SOLUTION
HotChilli
Super User
Super User

I'm posting some code for the cumulative column here:

Table.AddColumn(#"Previous Query", "Custom", each List.Sum(List.Range(#"Previous Query"[VALUE], 0, [MONTH]-1)))

 

May I request that you post data (not a picture) next time, for quicker answers?

---

If you want to add a row, you can create a one row table and append it (before adding the custom column above).  I haven't tried it but it should work

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi  @Anonymous ,

 

Please use the following M to add a custom column:

=List.Sum(List.FirstN(#"Changed Type"[VALUE],[MONTH]-1))

Eyelyn9_0-1640227713065.png

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

HotChilli
Super User
Super User

I'm posting some code for the cumulative column here:

Table.AddColumn(#"Previous Query", "Custom", each List.Sum(List.Range(#"Previous Query"[VALUE], 0, [MONTH]-1)))

 

May I request that you post data (not a picture) next time, for quicker answers?

---

If you want to add a row, you can create a one row table and append it (before adding the custom column above).  I haven't tried it but it should work

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.