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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
SBatHB
Frequent Visitor

How to Calculate Cumulative Running Total by Period, showing multiple years

How can I calculate the running total within a group/ timerange, but do so across multiple years whil using the date slicer?

 

I'm trying to calculate a running total for amounts, but do so in a way that I can compare prior years to each other in the same trending line graph. It is not a YTD calculation, but rather a slice of time (example: March 10 - April 15) that can be determined by the user. I have found ways to do this within a single year, but not across multiple years. So I need a running total within that slice, grouped by the year.

 

Additionally, I have been asked to use the date sliding range slicer, becaues it is so intuitive and easy for the user. So I would have a slicer for 2020, and need to have data from 2019 and 2018 still show up. When I user Power BI's native date filters, the past years are literally filtered out.

 

Here is a link to the Power BI .pbix File with what I have vs. what I'm trying to do. There are a couple of text boxes in there with notes.

https://drive.google.com/open?id=1sVBNi51i4h1KV3cZhnYeIRo7byRMERfS 

 

Thank you!

1 ACCEPTED SOLUTION

Hi @SBatHB ,

 

I've created this file as an example: Download PBIX 

 

Please, check if it helps.

 

If you consider it as a solution, please mark it as a solution, so we can help others.

 

Ricardo



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



View solution in original post

5 REPLIES 5
v-shex-msft
Community Support
Community Support

Hi @SBatHB,

If you want your date slicer works as a 'selector' and not directly filter on your records, please not use date field which related to fact table fields.

You can create a new table with date field as the source of slicer, then add a condition in your dax formula to compare with current date fields and select date ranges.

 

Measure =
VAR currDate =
    MAX ( 'Fact'[Date] )
RETURN
    CALCULATE (
        'Fact'[Amount],
        FILTER (
            ALLSELECTED ( 'Fact' ),
            YEAR ( 'Fact'[Date] ) = YEAR ( currDate )
                && 'Fact'[Date] <= currDate
                && 'Fact'[Date] IN ALLSELECTED ( Selector[Date] )
        )
    )

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Hi @SBatHB ,

 

I've created this file as an example: Download PBIX 

 

Please, check if it helps.

 

If you consider it as a solution, please mark it as a solution, so we can help others.

 

Ricardo



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



For those lokking at this, the solution in the Power BI workbook provided by camargos88 is here:

Running Total = 
VAR _dtMin = CALCULATE(MINX(Sheet1, Sheet1[Date]), ALLSELECTED())
VAR _dtMax = CALCULATE(MAXX(Sheet1, Sheet1[Date]), ALLSELECTED())
VAR _tblCross = SELECTCOLUMNS(ADDCOLUMNS(CROSSJOIN(DATESBETWEEN(Sheet1[Date], _dtMin, _dtMax), VALUES(Sheet1[Date].[Year])), "N_DATE", DATE(Sheet1[Date].[Year], MONTH(Sheet1[Date]), DAY(Sheet1[Date]))), "N_DATE", [N_DATE])
VAR _currentDate = SELECTEDVALUE(Dates[Date])
VAR _minDate = CALCULATE(MIN(Dates[Date]), FILTER(ALL(Dates), Dates[Date] in _tblCross && Dates[Date].[Year] = YEAR(_currentDate)))
RETURN SUMX(FILTER(Dates, Dates[Date] in _tblCross), CALCULATE(SUM(Sheet1[Amount]), FILTER(ALL(Sheet1[Date]), Sheet1[Date] >= _minDate && Sheet1[Date] <= MAX(Dates[Date]))))

 

I don't fully understand it, but it looks to use variables to

  1. Calculate the min and max dates in a range
  2. Create a date table as a variable with all dates between that min and max range
  3. Figure out the current year
  4. Find the Cumulative sum within the min and max date, using ALL in the filter to ignore the sected / current year filter.

 

So it's essentially finding a date range, and then calculating between those. I'm not sure, but it looks like the lack of relationship between the date table and my Fact table could be important.

 

Thanks for providing this solution!

Greg_Deckler
Super User
Super User

By using ALL or ALLEXCEPT or REMOVEFILTERS. 

 

See if my Time Intelligence the Hard Way provides a different way of accomplishing what you are going for.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TIT...


Follow on LinkedIn
@ 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...

ALL and ALLEXCEPT were both solutions that I came across, but I must be calculating the running total incorrectly for them to properly work. Applicable in other DAX functions though; thanks!

Helpful resources

Announcements
July 2024 Power BI Update

Power BI Monthly Update - July 2024

Check out the July 2024 Power BI update to learn about new features.

PBI_Carousel_NL_June

Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.

Top Solution Authors