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
Lucy64
Helper III
Helper III

Running Total

I am trying to calculate a running total for a production line based on the Machine name, there is no date value.  I am using a Direct Query to SQL database.  I want to add an exta column for running total of FNV, is this possible?

 

eg1.PNG

1 ACCEPTED SOLUTION
jahida
Impactful Individual
Impactful Individual

Eyy, I think I got it. Try this:

 

RunningTotal2 = MAXX(Table1, SUMX(
FILTER(
	SUMMARIZE(CALCULATETABLE(Table1, ALLEXCEPT(Table1, Table1[Building]), ALLSELECTED(Table1[Building])), Table1[MachineName], "NetVal", SUM(Table1[Fabrication Net Value])),
	[NetVal] >=
		SUMX(FILTER(CALCULATETABLE(Table1, ALLEXCEPT(Table1, Table1[Building]), ALLSELECTED(Table1[Building])), Table1[MachineName]= EARLIER(Table1[MachineName], 2)), Table1[Fabrication Net Value]))
, [NetVal]))

 

Hopefully you can see how that would scale out to other columns if you had multiple filters.

View solution in original post

30 REPLIES 30
joglidden
Advocate III
Advocate III

Where would the running total start, and what would be the next value added to the total?

I would sort the list in descending order of Fabrication Net Value and it would start with the first row and continue adding each row.  Here is the result I am looking for.

 

Machine NameFabrication Net ValueRunning Total
Rollpacks1675216752
Tightwinder1239029142
T8 ( 2 )1021739359
T8 ( 4 )582145180
Cut off saw580150981
T8 ( 3 )250953490
T8 (1 )215555645
Tarp saw56856213
C52 Pillow Machine30956522
Bun Roller Machine22956751
Chinese Carousel12456875
Habib
Responsive Resident
Responsive Resident

To achieve this you need to add some order number to you machines for DAX to identify how it should calculate running total. In your example i have added SrNo column and gave sequence values to this. Next I added a new column with below formula and it gave me desired results....

 

Another idea other than SrNo is to use the row number based on sorted column of your choice

 

RunningTotal = CALCULATE(SUM(Machine[Fabrication Net Value]),all(Machine),Machine[SrNo]<=EARLIER(Machine[SrNo]))

 

Below is the output

 

runningtotal.png

But what if the order changes, because of the magnitude of Fabrication Net Value? I.e., Tightwinder outperforms Rollpacks... maybe that will still work. The best example I've found so far is here: 

 

http://www.daxpatterns.com/cumulative-total/

 

But the problem is that this example uses dates.

Habib
Responsive Resident
Responsive Resident

Hi @joglidden In your mentioned post, all calculations are based on date and its easy to calculate running totoal in that scenario.... In given scenario, where date is not available only option left is to use the some sequence number....

You are probably right. I'm going to try anyway. But I think the solution will require an index, or row number (as an index), as you suggest. 

jahida
Impactful Individual
Impactful Individual

You don't necessarily need an index, here's an example of a Measure that doesn't use one:

 

RunningTotal = MAXX(Table2, CALCULATE(SUM(Table2[Fabrication Net Value]), Table2[Fabrication Net Value] >= EARLIER(Table2[Fabrication Net Value]), ALL(Table2)))

 

As written right now, filters won't work. If you are using filters (on the columns not used in the calculations), change the ALL to ALLEXCEPT(Table2, Table2[FilteredColumn]...) and it should work. If you have filters on the columns in the calculation, things will get a bit more complicated with this method.

@jahida wins! 

 

Capture.JPG

Habib
Responsive Resident
Responsive Resident

Give it a try... you can use RANKX for row numbers

Thanks for the tips @Habib. I learn a lot from you smart ones.

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.