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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
clarainesgg
Helper I
Helper I

Calculate Moving monthly Sales (acumulative) bring from previous year

Hi, 

 

I need to calculate the acumulative sales starting from a certain period (something similiar to Sales YTD

 

I used this: 

Sales YTD =
IF(
    ISFILTERED('financials'[Date]),
    ERROR("Time intelligence quick measures can only be grouped or filtered by the Power BI-provided date hierarchy or primary date column."),
    TOTALYTD(SUM('financials'[ Sales]), 'Calendar'[Date])
)
 
However, i need to acumulate the sales from previous month:
 
clarainesgg_0-1727272683980.png

 

Below is my mock up data.

 

Thanks,

 

YearQuarterMonth  Sales  Acum Sales
2013Qtr 4November                                                6.350 
2013Qtr 4December                                             60.000 
2014Qtr 1January                                             50.000 
2014Qtr 1February                                             70.000                                             6.350
2014Qtr 1March                                             45.000                                          66.350
2014Qtr 2April                                             25.000                                        116.350
2014Qtr 2May                                             30.000                                        186.350
2014Qtr 2June                                             45.000                                        231.350
2014Qtr 3July                                             10.000                                        256.350
2014Qtr 3August                                             50.000                                        286.350
     
                                             391.350 
4 REPLIES 4
Sergii24
Super User
Super User

Hi @clarainesgg, here is an algorithm:

  1. Create a ranking based on a necessary condition, so you can offset from "current period" to N previous "periods"
  2. use Calculate() to overwrite the current filter context and calculate the formula over all rows previous to the offset (you can add an upper limit as well if needed)
  3. Display a result on canvas

Attached you can find an example for your specific case when there is only one flat table and your numbers are available at monthly level only. Read it in the following way:

  1. Calcualted column "Month Rank" is created to obtain a month number as number instead of text
  2. Calcualted column "Year Month Number" is created to obtain a number for both month and year together (because month number is repeating over years)
  3. Calculated column "RowNumberByYearMonthNumber" is created for the ranking
  4. Create a measure "Accum Sales"

 

 

Acum Sales = 
VAR _RowNumberByYearMonthNumber = MIN( 'Table'[RowNumberByYearMonthNumber] ) //get current value of the column
VAR _Offset= 3      //when you want to start capturing previous data
VAR _CurrentOffset = _RowNumberByYearMonthNumber - _Offset      //to check if you arrived to a necessary month
VAR _Result =
    IF(
        _CurrentOffset >= 0,            //when we are at a necessary month or after
        CALCULATE(                      //than calculate a sum
            SUM( 'Table'[  Sales ] ),
            ALL( 'Table' ),             //necessary to remove filters from other columns such as year, month etc.
            'Table'[RowNumberByYearMonthNumber] <= _CurrentOffset
        ),
        BLANK()                         //otherwise keep it blank
    )

RETURN _Result

 

 

 

Here is the final result:

Sergii24_0-1727281302589.png

In case you have a proper data model with Calendar table marked as "Date table" (Do You Need a Date Dimension? - RADACAD) and sales at daily granularity you can follow @rajendraongole1's suggestion and use date column for the ranking system.

Good luck with your project! 🙂

I got an error when i want to create a row_number

clarainesgg_0-1727285590225.png

 

rajendraongole1
Super User
Super User

Hi @clarainesgg -can you check the below modified dax formaule

Cumulative Sales =
VAR CurrentDate = MAX('Calendar'[Date])
VAR FirstDateOfPreviousMonth = EDATE(CurrentDate, -1) // Shifts date back by one month
RETURN
CALCULATE(
SUM('financials'[Sales]),
FILTER(
ALL('Calendar'),
'Calendar'[Date] >= FirstDateOfPreviousMonth && 'Calendar'[Date] <= CurrentDate
)
)

 

I am assuming if the current context is March 2014, this DAX formula will accumulate sales starting from February 2014 and continue to accumulate as you move forward.Hope this works 





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

Proud to be a Super User!





I need to start from february to bring my sales from nov-23 and so on.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.