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
AlejandroPCar
Helper IV
Helper IV

Cumulative total in fixed period

Hi guys 

 

I need some help here. I want to calculate a cumulative sum but between a fixed period of 12 months. Like the following example, I made. 

 

Capture.PNG

 

 

The idea is, for example, I want to start in May 2018 so the cumulative total starts in May 2018 and ends 12 months later in April 2019. After that, in May 2019 the cumulative starts again and then finish 12 months later. Of course, the idea is that happens whatever the month I need to start. 

 

I tried with the following formula with no success. 

 

12month_test = 
    CALCULATE(
        [monthly_value],
        DATESINPERIOD(
            Dates[Dates],
            LASTDATE(Dates[Dates), -12, MONTH
        )
    )

 

 

I hope you can help me to solve this. 

 

Thank you

1 ACCEPTED SOLUTION
v-gizhi-msft
Community Support
Community Support

Hi,

 

According to your description, i create a sample to test:

100.PNG

Please follow these steps:

1)Create a slicer table:

Slicer Table = DISTINCT(SELECTCOLUMNS('Table',"Month",'Table'[Date].[Month],"MonthNo",'Table'[Date].[MonthNo]))

2)Try this measure:

Measure = 
IF (
    MAX ( 'Table'[Month] ) = SELECTEDVALUE ( 'Slicer Table'[Month] ),
    MAX ( 'Table'[Monthly Value] ),
    CALCULATE (
        SUM ( 'Table'[Monthly Value] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Date] <= MAX ( 'Table'[Date] )
                && 'Table'[Date]
                    >= IF (
                        MIN ( 'Table'[MonthNo] ) < MIN ( 'Slicer Table'[MonthNo] ),
                        DATE ( YEAR ( MIN ( 'Table'[Date] ) ) - 1, MIN ( 'Slicer Table'[MonthNo] ), 1 ),
                        DATE ( YEAR ( MIN ( 'Table'[Date] ) ), MIN ( 'Slicer Table'[MonthNo] ), 1 )
                    )
        )
    )
)

3)Create a check measure:

check = 
IF (
    MAX ( 'Table'[Date] )
        < DATE ( MINX ( ALLSELECTED ( 'Table' ), 'Table'[Date].[Year] ), CALCULATE (
            MAX ( 'Slicer Table'[MonthNo] ),
            FILTER (
                'Slicer Table',
                'Slicer Table'[Month] = SELECTEDVALUE ( 'Slicer Table'[Month] )
            )
        ), 1 ),
    0,
    1
)

4)Choose above [Measure] and [Year],[Month],[Monthly Value] as a table visual.

   Apply the above [check] to this visual by setting [check]=1.

   Choose [Month] from slicer table as a slicer.

   When select one month in slicer, the result shows:

101.PNG

Here is my test pbix file:

pbix 

Hope this helps.

 

Best Regards,

Giotto Zhi

 

View solution in original post

3 REPLIES 3
v-gizhi-msft
Community Support
Community Support

Hi,

 

According to your description, i create a sample to test:

100.PNG

Please follow these steps:

1)Create a slicer table:

Slicer Table = DISTINCT(SELECTCOLUMNS('Table',"Month",'Table'[Date].[Month],"MonthNo",'Table'[Date].[MonthNo]))

2)Try this measure:

Measure = 
IF (
    MAX ( 'Table'[Month] ) = SELECTEDVALUE ( 'Slicer Table'[Month] ),
    MAX ( 'Table'[Monthly Value] ),
    CALCULATE (
        SUM ( 'Table'[Monthly Value] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Date] <= MAX ( 'Table'[Date] )
                && 'Table'[Date]
                    >= IF (
                        MIN ( 'Table'[MonthNo] ) < MIN ( 'Slicer Table'[MonthNo] ),
                        DATE ( YEAR ( MIN ( 'Table'[Date] ) ) - 1, MIN ( 'Slicer Table'[MonthNo] ), 1 ),
                        DATE ( YEAR ( MIN ( 'Table'[Date] ) ), MIN ( 'Slicer Table'[MonthNo] ), 1 )
                    )
        )
    )
)

3)Create a check measure:

check = 
IF (
    MAX ( 'Table'[Date] )
        < DATE ( MINX ( ALLSELECTED ( 'Table' ), 'Table'[Date].[Year] ), CALCULATE (
            MAX ( 'Slicer Table'[MonthNo] ),
            FILTER (
                'Slicer Table',
                'Slicer Table'[Month] = SELECTEDVALUE ( 'Slicer Table'[Month] )
            )
        ), 1 ),
    0,
    1
)

4)Choose above [Measure] and [Year],[Month],[Monthly Value] as a table visual.

   Apply the above [check] to this visual by setting [check]=1.

   Choose [Month] from slicer table as a slicer.

   When select one month in slicer, the result shows:

101.PNG

Here is my test pbix file:

pbix 

Hope this helps.

 

Best Regards,

Giotto Zhi

 

Thank you @v-gizhi-msft 

Your solution was really useful. 

 

Als thank you @amitchandak I found some of your formulas very handy for other issues I had.

amitchandak
Super User
Super User

You can use datesytd and totalytd with end date with date Calendar

 

YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(('Date'[Date]),"4/30"))
This Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD((ENDOFYEAR('Date'[Date])),"4/30"))

Last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-1,Year),"4/301"))
Last YTD complete Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(ENDOFYEAR(dateadd('Date'[Date],-1,Year)),"4/30"))
Last to last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-2,Year),"4/30"))

Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Year))

 

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/

 

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.