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
zuber85
Frequent Visitor

Help with cumulative sum by month year with a category filter

Hi,

I am a newbie to Powerbi and want to create a line chart graph that shows a cumulative total broken down by years but the graph would show it on a monthly, I also would like to have a filter option that when selected would only show me the figures for that category.  See table below:

 

Datecategoryprofit
01/01/20231966
07/01/20232825
10/01/20231413
05/02/20231468
05/02/20231692
07/03/20232404
14/03/20231341
14/05/20232391
19/05/20231300
19/05/20233323
19/05/20231503
20/06/20232322
20/06/20232441
17/07/20231930
19/07/20231237
02/09/20232368
10/10/20231793
11/11/20232349
13/11/20231756
12/12/20231932
24/12/20232692
01/01/20241273
07/01/20242849
10/01/20242770
05/02/20241898
05/02/20241104
07/03/20241450
14/03/20242339
14/05/20241207
19/05/20242703
19/05/20241405
19/05/20243502
20/06/20241580
20/06/20242352
17/07/20241552
19/07/20241624
02/09/20241493
10/10/20242398
11/11/20242299
13/11/20241407
12/12/20241698
24/12/20241911

 

So the Chart would show months at the bottom, amounts on the left and would have a line per year as a cumulative sum, I can then filter by the category

 

I hope I have made some sense here. 

2 ACCEPTED SOLUTIONS
Corey_M
Resolver II
Resolver II

Datetables are a very common theme in powerBI and will help you greatly here. 

 

here is a sample table that might help get you started (https://chandoo.org/wp/power-query-calendar-table-best-method/ )

let
    Source = List.Dates(#date(2023,1,1),365, #duration(1,0,0,0)),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Renamed Columns" = Table.RenameColumns(#"Converted to Table",{{"Column1", "Date"}}),
    #"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date", type date}}),
    #"Inserted Year" = Table.AddColumn(#"Changed Type", "Year", each Date.Year([Date]), Int64.Type),
    #"Inserted Month" = Table.AddColumn(#"Inserted Year", "Month", each Date.Month([Date]), Int64.Type),
    #"Inserted Month Name" = Table.AddColumn(#"Inserted Month", "Month Name", each Date.MonthName([Date]), type text),
    #"Inserted Day of Week" = Table.AddColumn(#"Inserted Month Name", "Day of Week", each Date.DayOfWeek([Date]), Int64.Type),
    #"Inserted Day Name" = Table.AddColumn(#"Inserted Day of Week", "Day Name", each Date.DayOfWeekName([Date]), type text),
    #"Added Conditional Column" = Table.AddColumn(#"Inserted Day Name", "Is Weekend?", each if [Day of Week] = 6 then "Yes" else if [Day of Week] = 0 then "Yes" else "No"),
    #"Inserted Start of Month" = Table.AddColumn(#"Added Conditional Column", "Start of Month", each Date.StartOfMonth([Date]), type date),
    #"Added Custom" = Table.AddColumn(#"Inserted Start of Month", "Year Month", each [Year] * 100 + [Month]),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Current Month", each let cm = Date.StartOfMonth(DateTime.LocalNow())
in Date.Year(cm) * 100 + Date.Month(cm)),
    #"Inserted Subtraction" = Table.AddColumn(#"Added Custom1", "Subtraction", each [Year Month] - [Current Month], type number),
    #"Added Conditional Column1" = Table.AddColumn(#"Inserted Subtraction", "Month Type", each if [Subtraction] = 0 then "This Month" else if [Subtraction] = 1 then "Next Month" else if [Subtraction] = -1 then "Previous Month" else "Other Month"),
    #"Removed Columns" = Table.RemoveColumns(#"Added Conditional Column1",{"Current Month", "Subtraction"})
in
    #"Removed Columns"

  

one you have a datetable setup, create a relationship between the dates in your table and the datetable.

then just create a linechart with Month in the X-axis, Sum of Profit in the Y-axis, and Year in the Legend

Corey_M_0-1714584075705.png

The last thing is to add a slicer which you can put the category in allowing you to filter to that category. 

View solution in original post

v-junyant-msft
Community Support
Community Support

Hi @zuber85 ,

@Corey_M Good Answer! But I think you forget to calculate the cumulative total broken down by years, you just calculate the total of each month of each year.

And @zuber85  If you don't need all months showed at the bottom, you can either use the date hierarchy that comes with Power BI Desktop directly or use DAX:

Month = MONTH('Table'[Date])

And I choose to add a new Table Slicer for filter by the category (In order to remove some of the effects on filtering, I would suggest adding a new table) :

vjunyantmsft_0-1714616799327.png

vjunyantmsft_1-1714617001197.png

Then use this DAX to create a measure to calculate the cumulative total broken down by years:

cumulative total broken down by years = 
CALCULATE(
    SUM('Table'[profit]),
    ALL('Table'),
    YEAR('Table'[Date]) = YEAR(MAX('Table'[Date])) && MONTH('Table'[Date]) <= MONTH(MAX('Table'[Date])) && 'Table'[category] IN VALUES('Slicer'[category])
)

vjunyantmsft_2-1714617152804.png

vjunyantmsft_3-1714617166993.png

And the final output is as below:

vjunyantmsft_4-1714617191389.png

vjunyantmsft_5-1714617200168.png

vjunyantmsft_6-1714617210136.png

vjunyantmsft_7-1714617237120.png


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

View solution in original post

3 REPLIES 3
v-junyant-msft
Community Support
Community Support

Hi @zuber85 ,

@Corey_M Good Answer! But I think you forget to calculate the cumulative total broken down by years, you just calculate the total of each month of each year.

And @zuber85  If you don't need all months showed at the bottom, you can either use the date hierarchy that comes with Power BI Desktop directly or use DAX:

Month = MONTH('Table'[Date])

And I choose to add a new Table Slicer for filter by the category (In order to remove some of the effects on filtering, I would suggest adding a new table) :

vjunyantmsft_0-1714616799327.png

vjunyantmsft_1-1714617001197.png

Then use this DAX to create a measure to calculate the cumulative total broken down by years:

cumulative total broken down by years = 
CALCULATE(
    SUM('Table'[profit]),
    ALL('Table'),
    YEAR('Table'[Date]) = YEAR(MAX('Table'[Date])) && MONTH('Table'[Date]) <= MONTH(MAX('Table'[Date])) && 'Table'[category] IN VALUES('Slicer'[category])
)

vjunyantmsft_2-1714617152804.png

vjunyantmsft_3-1714617166993.png

And the final output is as below:

vjunyantmsft_4-1714617191389.png

vjunyantmsft_5-1714617200168.png

vjunyantmsft_6-1714617210136.png

vjunyantmsft_7-1714617237120.png


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

@v-junyant-msft @Corey_M 

Thank you very much.

 

As a newbie took me a little time, but got there. 

Corey_M
Resolver II
Resolver II

Datetables are a very common theme in powerBI and will help you greatly here. 

 

here is a sample table that might help get you started (https://chandoo.org/wp/power-query-calendar-table-best-method/ )

let
    Source = List.Dates(#date(2023,1,1),365, #duration(1,0,0,0)),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Renamed Columns" = Table.RenameColumns(#"Converted to Table",{{"Column1", "Date"}}),
    #"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date", type date}}),
    #"Inserted Year" = Table.AddColumn(#"Changed Type", "Year", each Date.Year([Date]), Int64.Type),
    #"Inserted Month" = Table.AddColumn(#"Inserted Year", "Month", each Date.Month([Date]), Int64.Type),
    #"Inserted Month Name" = Table.AddColumn(#"Inserted Month", "Month Name", each Date.MonthName([Date]), type text),
    #"Inserted Day of Week" = Table.AddColumn(#"Inserted Month Name", "Day of Week", each Date.DayOfWeek([Date]), Int64.Type),
    #"Inserted Day Name" = Table.AddColumn(#"Inserted Day of Week", "Day Name", each Date.DayOfWeekName([Date]), type text),
    #"Added Conditional Column" = Table.AddColumn(#"Inserted Day Name", "Is Weekend?", each if [Day of Week] = 6 then "Yes" else if [Day of Week] = 0 then "Yes" else "No"),
    #"Inserted Start of Month" = Table.AddColumn(#"Added Conditional Column", "Start of Month", each Date.StartOfMonth([Date]), type date),
    #"Added Custom" = Table.AddColumn(#"Inserted Start of Month", "Year Month", each [Year] * 100 + [Month]),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Current Month", each let cm = Date.StartOfMonth(DateTime.LocalNow())
in Date.Year(cm) * 100 + Date.Month(cm)),
    #"Inserted Subtraction" = Table.AddColumn(#"Added Custom1", "Subtraction", each [Year Month] - [Current Month], type number),
    #"Added Conditional Column1" = Table.AddColumn(#"Inserted Subtraction", "Month Type", each if [Subtraction] = 0 then "This Month" else if [Subtraction] = 1 then "Next Month" else if [Subtraction] = -1 then "Previous Month" else "Other Month"),
    #"Removed Columns" = Table.RemoveColumns(#"Added Conditional Column1",{"Current Month", "Subtraction"})
in
    #"Removed Columns"

  

one you have a datetable setup, create a relationship between the dates in your table and the datetable.

then just create a linechart with Month in the X-axis, Sum of Profit in the Y-axis, and Year in the Legend

Corey_M_0-1714584075705.png

The last thing is to add a slicer which you can put the category in allowing you to filter to that category. 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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