cancel
Showing results for
Did you mean:
rdurkin Member

## Cumulative (Running) Totals in DAX

A common Measure that you’ll probably find useful in PowerPivot or SSAS Tabular Models is finding running totals.  For example, you may want to see total sales of a product as it accumulates over time, or for inventory models the total on hand at a given time.  You can find more tips and tricks at my blog, www.bipatterns.com.

Let’s start with a base measure in a very simple pivot table.
Total Sales :=
CALCULATE ( SUM ( FactSales[SalesAmount] ) ) Now lets take our first attempt at computing a running total.  This is the most intuitive formula, but it has one common pitfall that isn’t necessarily easy to see right away.
Cumulative Total Sales :=
CALCULATE (
[Total Sales],
FILTER (
ALL ( DimDate[Datekey] ),
DimDate[Datekey] <= MAX ( ( DimDate[Datekey] ) )
)
)
Key parts of the Formula: The use of ALL(DimDate[DateKey]) results in the current context being ignored, so dates outside of the current pivot row context will be analyzed.  The second key step is the comparison of DimDate[Datekey] <= MAX ( ( DimDate[Datekey] ).  This means that all dates in the DateKey column that are before the current pivot table row context will be calculated.

If we put this measure on a table, we’ll get the correct numbers but we will have one issue remaining. The formula returns a number for dates that have no sales.  We need to add some error handling, which is outlined below.
Cumulative Sales (Correct) :=
IF (
COUNTROWS ( FactSales ) > 0,
CALCULATE (
[Total Sales],
FILTER (
ALL ( DimDate[Datekey] ),
DimDate[Datekey] <= MAX ( ( DimDate[Datekey] ) )
)
),
BLANK ()
)

The IF Function checks to make sure that there are sales in the current selected context, otherwise returning blank.  You can see the difference between the two measures below: If you have any questions for me, you can reach me via LinkedIn or in the PowerBI Community.

1 ACCEPTED SOLUTION

Accepted Solutions
rdurkin Member

## Re: Cumulative (Running) Totals in DAX

@sdjensen

Give the following a try:

Cumulative Sales:=IF(MIN(DimDate[Datekey])<=CALCULATE(MAX(FactSales[DateKey]),ALL(FactSales)),CALCULATE([Total Sales],FILTER(All(DimDate[Datekey]),DimDate[Datekey]<=MAX((DimDate[Datekey])))),BLANK())

Please mark it as a solution or give a kudo if it works for you, otherwise let me know if you run into an issue and I'll do my best to assist.

Thanks,

Ryan Durkin

9 REPLIES 9
sdjensen Senior Member

## Re: Cumulative (Running) Totals in DAX

Do you have an alternative solution instead of using IF ( COUNTROWS(FactSales) > 0? Lets say you drill down to day level and some days don't have any rows in your fact then a value will not be calculated for these days.

```IF (
COUNTROWS ( FactSales ) > 0,
...
...
...
BLANK()
)```
/sdjensen
rdurkin Member

## Re: Cumulative (Running) Totals in DAX

@sdjensen

Give the following a try:

Cumulative Sales:=IF(MIN(DimDate[Datekey])<=CALCULATE(MAX(FactSales[DateKey]),ALL(FactSales)),CALCULATE([Total Sales],FILTER(All(DimDate[Datekey]),DimDate[Datekey]<=MAX((DimDate[Datekey])))),BLANK())

Please mark it as a solution or give a kudo if it works for you, otherwise let me know if you run into an issue and I'll do my best to assist.

Thanks,

Ryan Durkin

ggp Frequent Visitor

## Re: Cumulative (Running) Totals in DAX

This suggestion has gotten me closer.  Thank you!

Instead of Blank() I used 0, however, since it's a cumulative count I'm going for, I'm thinking instead of zero I would actually need the value show before.  So that 0 plus the last value.  Any ideas on getting that for the true part of this IF statement?

Cumulative Sales (Correct) :=
IF (
COUNTROWS ( FactSales ) > 0,
CALCULATE (
[Total Sales],
FILTER (
ALL ( DimDate[Datekey] ),
DimDate[Datekey] <= MAX ( ( DimDate[Datekey] ) )
)
),
BLANK ()
) Anonymous
Not applicable

## Re: Cumulative (Running) Totals in DAX

Hello,

thank you for this solution. But the running total starts from the very first day of the data base and keeps totaling to the last day.

Would it be possible to restart totaling every year or even month to be able to do cumulative total comparisons vs previous year.

Regards

Mark

yeszone Frequent Visitor

## Re: Cumulative (Running) Totals in DAX

Super article! Thanks. yeszone Frequent Visitor

## Re: Cumulative (Running) Totals in DAX

Hi @Anonymous  ,

Have you tried using a visual level filter for the time period you want? Hope it will work.

Ex: If you are showing data in a 'Table' or 'LineChart' visual, add a 'visual level filter' for the required time period i.e. current month, last 3 months, next 3 months, etc.

Luddish Frequent Visitor

## Re: Cumulative (Running) Totals in DAX

I want to make a graph to display "orders delivered late/orders delivered" over time.

Our companys goal is to reach 95% within 30 minutes from the planned delivery time.

%30 = 1-([SumLate]/[SumDelivered]) works fine when we don't look at a specific time.

The problem I have is that the current percentage shown is only at that specific time and not the total up untill that time.

I have [SumLate] which is

"SumLate = CALCULATE(COUNT('Ecommerce Order'[Order id]);FILTER('Ecommerce Order';'Ecommerce Order'[Order status]="Delivered" && [LateDelivery]>30))"

[LateDelivery] is 'Ecommerce Order'[Actual Delivery Time] - 'Ecommerce Order'[Planned Delivery Time]

And I have [SumDelivered]

SumDelivered = CALCULATE(COUNT('Ecommerce Order'[Order id]);FILTER('Ecommerce Order';'Ecommerce Order'[Order status]="Delivered"))

I want to make this work as a Linegraph but I'm not sure how to apply the Datefilter-function since I'm only looking at times in the current day.

james_m Frequent Visitor

## Re: Cumulative (Running) Totals in DAX

`Best solution:`
Running Total =
VAR MaxDate=CALCULATE(MAX(DimDate[Full Date]), FctTable)
RETURN CALCULATE([Amount], FILTER(ALL(DimDate), DimDate[Full Date]<=MaxDate))
Highlighted
james_m Frequent Visitor

## Re: Cumulative (Running) Totals in DAX

Hi, a better solution is:

Running Total =
VAR MaxDate=CALCULATE(MAX(DimDate[Full Date]), FctTable)
RETURN CALCULATE([Amount], FILTER(ALL(DimDate), DimDate[Full Date]<=MaxDate))

Announcements #### Challenge: Can You Solve These?

Find out how to participate in the first Power BI 'Can You Solve These?' challenge. #### Incoming: New and Improved Badges

Exciting news: We've given our badges an overhaul and added brand news ones. #### Exclusive LIVE Community Event No. 2 – Ask Amir Anything

Next in our Triple A series: Ask Amir Netz questions about the latest updates, features and future. #### Analytics in Azure virtual event

Experience a limitless analytics service built to ingest, prep, manage, and serve data for immediate use in Power BI. Top Kudoed Authors
Users Online
Currently online: 347 members 3,684 guests
Recent signins:
• valentin • thabz_mogale • dethompson97 • arichards • brenavelino • Petr_555 • HarshaTU • vasanthkumar • bilalkNP • alyrazik • New_User_2019 • MarkW • phuber • kyriakopoulosk 