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
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
Employee
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
Community Champion
Community Champion

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

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

Top Solution Authors