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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
stephanie116
Frequent Visitor

Obtaining Monthly Totals from YTD Data

I receive a monthly report that shows YTD totals for a few different categories. I'm trying to find a solution within Power Query to convert these YTD totals into monthly totals. Any ideas?

 

Raw Data: Monthly Reports

JANUARY REPORT

Category A50
Category B100
Category X0

FEBRUARY REPORT

Category A125
Category B197
Category X15

MARCH REPORT

Category A185
Category B251
Category X39

 

Current Dataset: 

MONTHCATEGORYYTD TOTAL
JanuaryCategory A50
JanuaryCategory B100
JanuaryCategory X0
FebruaryCategory A125
FebruaryCategory B197
FebruaryCategory X15
MarchCategory A185
MarchCategory B251
MarchCategory X39

 

Target Dataset: 

MONTHCATEGORYMONTHLY TOTAL
JanuaryCategory A50
JanuaryCategory B100
JanuaryCategory X0
FebruaryCategory A75
FebruaryCategory B97
FebruaryCategory X15
MarchCategory A60
MarchCategory B54
MarchCategory X24

 

1 ACCEPTED SOLUTION
edhans
Super User
Super User

@stephanie116 see if this works. Here is what I did - as requested - in Power Query

  1. I created an ad-hoc date table to give me dates, then added the names of the months and month numbers to that table.
  2. In your table, I merged with my date table by the Month Name column. Then I expanded the Month number and sorted by month. Now I can ensure the data is correctly sorted for the next steps. It looks like this:
    1. 2020-03-15 21_04_32-20200315 - Cumulative Total in Power Query - Power Query Editor.png
  3. Then I added an Index column
  4. Then I added the table back into itself as a nested table by simply referring to the previous step. So now every record of the table has a nested version of itself. Note: I don't know how this will perform on very large record sets, but your data doesn't seem like it will be in the millions of records.
    1. 2020-03-15 21_05_58-20200315 - Cumulative Total in Power Query - Power Query Editor.png
  5. Next I filter that nested table to only include items in the current category where the index is less than or equal to the current index.
    1. 2020-03-15 21_08_27-20200315 - Cumulative Total in Power Query - Power Query Editor.png
    2. It is assigining to varIndex and varCategory the index number and category of the main table, then it does a filter (Table.SelectRows) on the nested table. The way I've assigned variables is similar to how variables work in DAX, or the EARLIER() function in DAX if you know how that works.
  6. Finally I add a List.Sum to total the [Total] column still showing in my now filtered table.
  7. Get rid of all of the unnecessary columns.

End result:

2020-03-15 21_11_24-20200315 - Cumulative Total in Power Query - Power Query Editor.png

 

You can see my PBIX file here if you want to play with it.

EDIT: I improved it a bit by only nesting the necessary data and doing the total calculation (List.Sum()) in a single setp. The step immediately after adding the index is now:

= Table.AddColumn(#"Added Index", "Cumulative YTD Total", 
each let varCategory = [Category], varIndex = [Index]
in
List.Sum(
     Table.SelectRows(#"Added Index", each [Category] = varCategory and [Index] <= varIndex)[Total]
), Int64.Type)

For a smaller data set (under 100,000 records probably, maybe more) this should work fine. I strongly encourage you to read @ImkeF's links and study them. I plan to dig into those later today and see if I can improve this even more for my own use going forward.

The original longer query is still in the PBIX linked to called [Table (Original Long Way)] to follow along, but [Table] consolidates a number of steps as shown above and pulls in only the necessary data for each record vs all the data for every record and then filtering in subsequent steps.

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

4 REPLIES 4
edhans
Super User
Super User

@stephanie116 see if this works. Here is what I did - as requested - in Power Query

  1. I created an ad-hoc date table to give me dates, then added the names of the months and month numbers to that table.
  2. In your table, I merged with my date table by the Month Name column. Then I expanded the Month number and sorted by month. Now I can ensure the data is correctly sorted for the next steps. It looks like this:
    1. 2020-03-15 21_04_32-20200315 - Cumulative Total in Power Query - Power Query Editor.png
  3. Then I added an Index column
  4. Then I added the table back into itself as a nested table by simply referring to the previous step. So now every record of the table has a nested version of itself. Note: I don't know how this will perform on very large record sets, but your data doesn't seem like it will be in the millions of records.
    1. 2020-03-15 21_05_58-20200315 - Cumulative Total in Power Query - Power Query Editor.png
  5. Next I filter that nested table to only include items in the current category where the index is less than or equal to the current index.
    1. 2020-03-15 21_08_27-20200315 - Cumulative Total in Power Query - Power Query Editor.png
    2. It is assigining to varIndex and varCategory the index number and category of the main table, then it does a filter (Table.SelectRows) on the nested table. The way I've assigned variables is similar to how variables work in DAX, or the EARLIER() function in DAX if you know how that works.
  6. Finally I add a List.Sum to total the [Total] column still showing in my now filtered table.
  7. Get rid of all of the unnecessary columns.

End result:

2020-03-15 21_11_24-20200315 - Cumulative Total in Power Query - Power Query Editor.png

 

You can see my PBIX file here if you want to play with it.

EDIT: I improved it a bit by only nesting the necessary data and doing the total calculation (List.Sum()) in a single setp. The step immediately after adding the index is now:

= Table.AddColumn(#"Added Index", "Cumulative YTD Total", 
each let varCategory = [Category], varIndex = [Index]
in
List.Sum(
     Table.SelectRows(#"Added Index", each [Category] = varCategory and [Index] <= varIndex)[Total]
), Int64.Type)

For a smaller data set (under 100,000 records probably, maybe more) this should work fine. I strongly encourage you to read @ImkeF's links and study them. I plan to dig into those later today and see if I can improve this even more for my own use going forward.

The original longer query is still in the PBIX linked to called [Table (Original Long Way)] to follow along, but [Table] consolidates a number of steps as shown above and pulls in only the necessary data for each record vs all the data for every record and then filtering in subsequent steps.

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Hi there,

I come across a question like this before and made a blogpost about the solution: https://www.thebiccountant.com/2018/08/31/unravel-cumulative-totals-to-their-initial-elements-in-pow... 

 

@edhans solution has a very nice and transparent logical path, but the performance could be improved if the logic would be applied on category-level, like I've described here: 

https://www.thebiccountant.com/2017/05/29/performance-tip-partition-tables-crossjoins-possible-power... 

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

amitchandak
Super User
Super User

@stephanie116 


You need to assign month a number like 1,2, 3 4 or month year 202001, 2020002, etc. Assume that column is Month No. Then try a new column

 

MONTHLY TOTAL = table[YTD TOTAL] -maxx(filter(Table,table[Month No] <= earlier(table[Month No]) && table[CATEGORY] = earlier(table[CATEGORY])),table[YTD TOTAL])

 

Greg_Deckler
Super User
Super User

I don't know about Power Query, but I did write a solution in DAX:

https://community.powerbi.com/t5/Quick-Measures-Gallery/Periodic-Revenue-Reverse-YTD/m-p/373185#M111

 

@ImkeF might know how to solve it in M.

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.