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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Galat
Frequent Visitor

How do you calculate the last 4 months from month was choice on month slicer ?

How do you calculate the last 4 months from month was choice on month slicer ?

if choice month 10 in slicer I want to see sum amount for per month of 10,9,8,7 only

 

My solution does not work because it shows me a cumulative amount for the last selected month:

 

last_4_month_sum =
VAR start_of_selected_month = STARTOFMONTH('Dates'[Date])
VAR start_of_4_month = EDATE(start_of_selected_month,-3)
VAR end_of_selected_month = ENDOFMONTH('Dates'[Date])

RETURN
CALCULATE(
SUM('Table1'[Sum]),
DATESBETWEEN
(
'Dates'[Date],
start_of_4_month,
end_of_selected_month
)
)

aa.jpg

1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

To do this, you first need to make a DAX calculated table with just your Month values to be used in the slicer (e.g., with SlicerMonths = VALUES('Date'[MonthNumber])

 

Then you can use a measure like this to get your desired result (bar chart should use MonthNumber column from the Date table, and there is no relationship needed between Date and SlicerMonths table.

 

Just Last 4 Months =
VAR thismonth =
    SELECTEDVALUE ( SlicerMonths[Monthnumber] )
RETURN
    CALCULATE (
        [Total Sales],
        KEEPFILTERS ( 'Date'[Monthnumber] <= thismonth
            && 'Date'[Monthnumber] >= thismonth - 3 )
    )

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

6 REPLIES 6
Galat
Frequent Visitor

Thanks friends,

With your help I understood where there was a problem
A filter with an unlinked external table should be used.

I have not encountered it until today))

 

Can you give a short explanation of why I need an external and unlinked table for filtering ??

Galat
Frequent Visitor

bbb.jpg

 

It did not open for me 4 previous months .....

Did you try the file I provided?
AntrikshSharma
Super User
Super User

After thinking for 10 minutes I basically came up with the somewhat same solution as Mahoney, but now that I had spent time on this so thought of sharing it with the file, you can access the file from here. https://drive.google.com/file/d/1WlcVuytdqxbT1ndyu-Bwn0tz-AjEdgED/view?usp=sharing

1.PNG

 

Measure =
VAR MaxDate =
    CALCULATE (
        MAX ( DisconnectedDates[Month Number] ),
        ALLSELECTED ( DisconnectedDates )
    )
VAR PreviousMonths =
    FILTER (
        ALL ( Dates[Month Number] ),
        Dates[Month Number] <= MaxDate
            && Dates[Month Number] >= MaxDate - 3
    )
VAR Result =
    CALCULATE ( [Total Sales], KEEPFILTERS ( PreviousMonths ) )
RETURN
    Result

 

amitchandak
Super User
Super User

@Galat , refer to this

https://www.youtube.com/watch?v=duMSovyosXE

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
mahoneypat
Microsoft Employee
Microsoft Employee

To do this, you first need to make a DAX calculated table with just your Month values to be used in the slicer (e.g., with SlicerMonths = VALUES('Date'[MonthNumber])

 

Then you can use a measure like this to get your desired result (bar chart should use MonthNumber column from the Date table, and there is no relationship needed between Date and SlicerMonths table.

 

Just Last 4 Months =
VAR thismonth =
    SELECTEDVALUE ( SlicerMonths[Monthnumber] )
RETURN
    CALCULATE (
        [Total Sales],
        KEEPFILTERS ( 'Date'[Monthnumber] <= thismonth
            && 'Date'[Monthnumber] >= thismonth - 3 )
    )

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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