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
cpdanielmc21
Helper I
Helper I

Running Total solution for slicer

Hi community

I've been trying to apply solutions proposed in this forum for similar issues to mine, but have not been successful so far.

 

I want to create either a measure or a column that can help me visualize a running total that always calculates from the begining even when slider applied. Let me explain.

 

This is my table "data"

MonthAccountDateAmount
11011/15/202010
11021/15/20204
11011/31/202010
21012/15/202015
21022/15/20208
21012/29/202015

 

And when I write a DAX measure like this:

 

Running Total = 
CALCULATE(
	SUM('data'[Amount]),
	FILTER(
		ALL('data'[Date]),
		ISONORAFTER('data'[Date], MAX('data'[Date]), DESC)
	)
)

 

 

I get this result (assume I filter account 101)

MonthAccountDateAmountRunning Tot
11011/15/20201010
11011/31/20201020
21012/15/20201535
21012/29/20201550

 

And when I further filter Month 2, I expect to get the following:

MonthAccountDateAmountRunning Tot
21012/15/20201535
21012/29/20201550

 

Note the first row is 35 because it includes the cumulated values from last month (20), or in other words, is including the opening balance that comes from January.

 

However the Running Total starts from 15 , instead of 35,

 

Any ideas for this?

 

I was thining of adding a new column that calculates the running total but, got the same.

1 ACCEPTED SOLUTION

Ok.  I played with this some more.  Please try this expression.  Having account in both the table and the slicer makes this a challenge. 

 
RunningTotal = var thisdate = CALCULATE(MAX(Total[Date]), ALLSELECTED(Total[Account]))
return CALCULATE(SUM(Total[Amount]), ALLEXCEPT(Total,Total[Account]), Total[Date] <= thisdate)
 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

12 REPLIES 12
mahoneypat
Employee
Employee

Please try this expression as a measure (replacing Table name "Total" with 'data').

 

RunningTotal =
VAR thisdate =
SELECTEDVALUE ( Total[Date] )
RETURN
CALCULATE (
SUM ( Total[Amount] ),
ALL ( Total ),
VALUES ( Total[Account] ),
Total[Date] <= thisdate
)

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Hey @mahoneypat , Thanks!

This worked! Just want to ask if there is something else it can be done.

This only works fine if I filter a specific account, but when I want to show all accounts, it doesnt calculate the "global" running total, it is doing it by account.

 

This is what I see when I filter Month 2 and show all accounts. The running total starts fine on 35, but then on account 102 it starts again from zero. I would also like to see a sort of "global" running total ("Expected running total" down below)

AccountMonthDateAmountRunning TotalExpected Running Total
10122/15/2020153535
10122/29/2020155050
10222/15/202081262

 

So, if I filter account 101 the running total should show 50 at the end (even if I filter Month 2 or if show all months), also, if I filter account 102, running total should be 12 (no matter what Month I filter or if there is no month filter) and If i show All accounts, it should calculate 62 at the end (even if I filter Month 2 or show all months).

 

If you got any idea for this, it would be great!

 

 

Yes.  You can just take the VALUES( ) clause out of the CALCULATE().  You only had one account in the example data, so assumed (incorrectly) you would want it that way.

 

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Hi @mahoneypat 

That's what I thought, so I erased that line, but get the following result (no filters applied):

Capture.JPG

Running total is weird now, and If suppose, I apply filter month "2" and no filer on account, running total should be 62, but shows 47 on last line. If i happen to also add filter Account "102", the running total now should be 12, but nope, it shows 47.

 

😞

Try writing a new measure that references the original measure. This one will iterate over each account and add all of the running totals together. See if that works instead. Typing on a tablet. Sorry. 

 

New = Sumx(values(table[account]), [original  measure])

 

If not will look more later. 

 

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


@mahoneypat 

Thanks Pat, but still getting same result, 😞

I just returned the original formula for running total and created the new measure, but it gives same numbers

Capture2.JPG

NewMeasure = SUMX(VALUES('Table'[Account]),[RunningTotal])
 
RunningTotal =
VAR thisdate =
SELECTEDVALUE ( 'Table'[Date])
RETURN
CALCULATE (
SUM ( 'Table'[Amount]),
ALL('Table'),
VALUES('Table'[Account]),
'Table'[Date]<= thisdate
)

The reason you are seeing the weird #s is because the max date for Account 102 is less than the max for Account 101.  You need to get the max date in the selected month in the variable first with this type of change.

 

RunningTotalAll = var thisdate = CALCULATE(MAX(Total[Date]), ALL(Total), VALUES(Total[Month]))
return CALCULATE(SUM(Total[Amount]), ALL(Total), Total[Date]<= thisdate)
 
Regards,
Pat
 




Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


@mahoneypat 

Hi Pat, thanks. I think is closer.

It shows 24 for Month 1 and 62 for Month 2, (indeed correct), but not showing the "development" of 10, 20, then 24, then 39, etc up until 62.

Capture3.JPG

 

Also, if let's say I filter Account 102, should get 12, but get 62. In this case the original measure is the correct

Capture4.JPG

 

Ok.  I played with this some more.  Please try this expression.  Having account in both the table and the slicer makes this a challenge. 

 
RunningTotal = var thisdate = CALCULATE(MAX(Total[Date]), ALLSELECTED(Total[Account]))
return CALCULATE(SUM(Total[Amount]), ALLEXCEPT(Total,Total[Account]), Total[Date] <= thisdate)
 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


That is a great measure.

RunningTotal = var thisdate = CALCULATE(MAX(Total[Date]), ALLSELECTED(Total[Account]))
return CALCULATE(SUM(Total[Amount]), ALLEXCEPT(Total,Total[Account]), Total[Date] <= thisdate)

But, I'm facing the situation when I'd need to have all accounts with balances even if they are not in the selection range "From ... To ...."
So, it would be 0 for the Amount but some Running Total that came from all previouse/before the selected dates range periods.
Could this measure be modified to do it?

@mahoneypat 

Oh! This works!!

I just took out the account column from the table, just kept it in the slider and now all numbers show as expected.

 

Thank you so much Pat!

@mahoneypat ,

try like with a date table

Cumm Sales = CALCULATE(SUM(Table[Amount]),filter(allselected(date),date[date] <=maxx(date,date[date])))
Cumm Sales = CALCULATE(SUM(Table[Amount]),filter(allselected(date),date[date] <=max(table[Date])))

 

To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :
https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions
https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi
https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

See if my webinar on Time Intelligence can help: https://community.powerbi.com/t5/Webinars-and-Video-Gallery/PowerBI-Time-Intelligence-Calendar-WTD-Y...


Appreciate your Kudos.

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.