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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
loos
Regular Visitor

DAX formula to get Previous month from Max Month

I have this formula that works well, it is based off of the user selecting multiple months in a filter, but using only the latest to calculate it:
 
Current Month FTE = CALCULATE(SUM(HRdata[FTE]), FILTER(HRdata,  HRdata[YearMonth]= (MAX(HRdata[YearMonth]))))
 
However, I need a second formula that calculates the previous month of the MAX month. 
 
This does not work:
 
Previous Month FTE = CALCULATE(SUM(HRdata[FTE]), FILTER(HRdata, HRdata[YearMonth]= DATEADD(MAX(HRdata[YearMonth],-1, Month))))
 
Can someone help me fix it?
 
Thank you!
1 ACCEPTED SOLUTION
loos
Regular Visitor

Thank you for your help, I was able to finally figure out the solution: 

 

Previous Year FTE = CALCULATE(sum(HRdata[FTE]),FILTER(ALL(HRdata[YearMonth]),HRdata[YearMonth] = DATE(YEAR(MAX(HRdata[YearMonth])),MONTH(MAX(HRdata[YearMonth])-1),DAY(MAX(HRdata[YearMonth])))))

View solution in original post

6 REPLIES 6
loos
Regular Visitor

Thank you for your help, I was able to finally figure out the solution: 

 

Previous Year FTE = CALCULATE(sum(HRdata[FTE]),FILTER(ALL(HRdata[YearMonth]),HRdata[YearMonth] = DATE(YEAR(MAX(HRdata[YearMonth])),MONTH(MAX(HRdata[YearMonth])-1),DAY(MAX(HRdata[YearMonth])))))

@loos 
I thought your YearMonth column is an integer data type. Just now I realized it is a Date data type. This is the importance of posting some sample data along with the expected results.

tamerj1
Super User
Super User

Hi @loos 
Allways better to have a date table. However, if you wish not not to use one please try

Previous Month FTE =
VAR CurrentYearMonth =
    MAX ( HRdata[YearMonth] )
VAR PreviousYearMonth =
    TOPN (
        1,
        FILTER ( ALL ( HRdata[YearMonth] ), HRdata[YearMonth] < CurrentYearMonth ),
        HRdata[YearMonth]
    )
RETURN
    CALCULATE (
        SUM ( HRdata[FTE] ),
        FILTER ( ALL ( HRdata ), HRdata[YearMonth] = PreviousYearMonth )
    )

I should have mentioned one more thing, there are other filters (slicers) that also exist. So if I have no filters selected other than the date slicer, your formula works. But if I use the other slicers to select the type of company, etc, the Previous Month number stays the same and does not drill down alongside the Current Month. Is there anyway to allow the other filters to affect this number? 

@loos 
You may also try

Previous Month FTE =
VAR CurrentYearMonth =
    MAX ( HRdata[YearMonth] )
VAR PreviousYearMonth =
    TOPN (
        1,
        FILTER ( ALL ( HRdata[YearMonth] ), HRdata[YearMonth] < CurrentYearMonth ),
        HRdata[YearMonth]
    )
RETURN
    CALCULATE (
        SUM ( HRdata[FTE] ),
        HRdata[YearMonth] = PreviousYearMonth,
        ALLEXCEPT ( HRdata, HRdata[FilterSafeColumn1], HRdata[FilterSafeColumn2], ....etc )
    )

@loos 
Please provide a screenshot for better understanding

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.