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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

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...


@ 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!:
Mastering Power BI 2nd Edition

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
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors