Running Totals in Power Query (Get and Transform)

TDC21

Board Regular
Joined
Mar 31, 2017
Messages
97
Our inventory table calculates a current balance and our inventory transaction table shows all transactions by date but no balance. I have rearranged the transaction table to provide a period end date and activity summed by period. I have also included an date Index column and created duplicates of the Index and period columns to self reference. What I am trying to get now is a balance by period but I cant find a way to make this work yet. My query includes the following columns,


[Index] [Index_B] [END_DATE] [END_DATE_B] [PART] [PERIOD ACTIVITY]


I am looking for a way to sum [PERIOD ACTIVITY] by [PART] where [END_DATE] <= [END_DATE_B]

or

sum [PERIOD ACTIVITY] by [PART] where [Index] <= [Index_B]


Any help is greatly appreciated
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
this is a pretty simple version of what I am working with, it is transactions by date and part. The date has been converted to EOMONTH and I also added a index by date column. I made the duplicate columns thinking some kind of self reference might work. I have had issues finding the correct function or query stmt but in a simple way of saying it I am trying to

SUM [PERIOD ACTIVITY] BY [PART] WERE [END_DATE] <= [END_DATE_B] Just a running balance based on transactions by part and date


here is a simple example of my data set,

TransactionIndexIndex_BPARTEND_DATEEND_DATE_BPERIOD ACTIVITY
11105000X135SA516701/31/20171/31/201720
21106250SA2403041/31/20171/31/201710
31105000X135SA516701/31/20171/31/2017-5
42206250SA2403042/28/20172/28/20172
52205000X135SA516702/28/20172/28/2017-10
63306250SA2403043/31/20173/31/2017-8
73305000X135SA516703/31/20173/31/201710
84406250SA2403044/30/20174/30/20175
DESIRED RESULTS
PARTEND_DATEPERIOD ACTIVITY
05000X135SA516701/31/201715
06250SA2403041/31/201710
05000X135SA516702/28/20175
06250SA2403042/28/201712
05000X135SA516703/31/201715
06250SA2403043/31/20174
05000X135SA516704/30/201715
06250SA2403044/30/20179

<colgroup><col><col span="2"><col><col><col span="2"></colgroup><tbody>
</tbody>
 
Upvote 0
I have not tried power pivot with a date table, ideally i am hoping to find a way to do this as a query so the results themselves can then be linked back to several other tables.
 
Upvote 0
Dropbox Link

The dropbox link will connect you to an example of a running total in Power Query using your data set to start and then finishing with the desired results.

The solution required some advanced steps such as custom M code for merging and appending data sets created in the query, a Cartesian join of dates and parts to handle cases where there is no part activity in a given month (see April in source and April in desired results), and a custom function to handle the actual running total.

Most of the work was in preparing the data. Once the basic table is prepared using a group by on part and date (summing the Activity Total), the function receives parameters from each row for part, date and a copy of the prepared table. Inside the function, the table copy is filtered for required dates and part and then a "group by" is used to sum the period activity column for the running total.

Tom
PowerPivotPro - Transforming your Business with Power Pivot and Power BI
 
Last edited:
Upvote 0
Thank you Tallan,

I am fairly new to M-Code so Im sure it will take me some time to get my head wrapped around all of that but it is an excellent example so I will defiantly spend some time on it.

Do you know if the code will operate on large data sets efficiently? Roughly 400,000 total lines and 80,000 after the initial grouping (part & period). If data size is an issue I could run the first layer or two of groupings as a custom view in SQL to limit the data source file size for excel. Not sure if that would be beneficial or not.
 
Upvote 0
Hi TDC21,

This approach will not be efficient for datasets that are still relatively large after the first grouping. For an efficient solution with large data sets, I recommend grouping data in Power Query on balance date and part, then importing into the Excel Data Model (the Power Pivot window), followed by creating a measure to handle the running total, and then creating a pivot table with the Display option set for "Show items with no data on rows".

Tom
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,454
Members
448,898
Latest member
drewmorgan128

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top