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
aleivar
Frequent Visitor

Calculate Previous Month Sales up to a specific date

Hi,

I have the following functions:

 

Sales MTD = 
CALCULATE(
    [Total Sales],
    DATESMTD(
        Calendar_Lookup[Date]
    )
)

 

 

 

Sales MTD Previous = 
CALCULATE(
    [Sales MTD],
    DATEADD(
        Calendar_Lookup[Date],
        -1,
        MONTH
        )
)

 

 

I want to calculate the Sales MTD Previous but up to a specific date (max date).

My model looks like this:

aleivar_0-1682041471980.png

 



Any help is appreciated.

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

I tried to create a sample pbix file like below.

Please check the below picture and the attached pbix file.

I hope the below can provide some ideas on how to create a solution for your datamodel.

 

Jihwan_Kim_0-1682047319364.png

 

Jihwan_Kim_1-1682047945654.png

 

Sales MTD prevmonth upto specific date: =
VAR _selectedyear =
    YEAR ( MAX ( 'Calendar'[Date] ) )
VAR _selectedmonth =
    MONTH ( MAX ( 'Calendar'[Date] ) )
VAR _prevmonthdate =
    CALCULATE ( MAX ( 'Calendar'[Date] ), DATEADD ( 'Calendar'[Date], -1, MONTH ) )
RETURN
    IF (
        NOT ISBLANK ( _prevmonthdate ),
        IF (
            _selectedmonth <> 1,
            CALCULATE (
                [Total sales:],
                FILTER (
                    ALL ( 'Calendar' ),
                    YEAR ( [Date] ) = _selectedyear
                        && MONTH ( [Date] ) = _selectedmonth - 1
                        && 'Calendar'[Date] <= _prevmonthdate
                )
            ),
            CALCULATE (
                [Total sales:],
                FILTER (
                    ALL ( 'Calendar' ),
                    YEAR ( [Date] ) = _selectedyear - 1
                        && MONTH ( [Date] ) = 12
                        && 'Calendar'[Date] <= _prevmonthdate
                )
            )
        )
    )

 

 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

1 REPLY 1
Jihwan_Kim
Super User
Super User

Hi,

I tried to create a sample pbix file like below.

Please check the below picture and the attached pbix file.

I hope the below can provide some ideas on how to create a solution for your datamodel.

 

Jihwan_Kim_0-1682047319364.png

 

Jihwan_Kim_1-1682047945654.png

 

Sales MTD prevmonth upto specific date: =
VAR _selectedyear =
    YEAR ( MAX ( 'Calendar'[Date] ) )
VAR _selectedmonth =
    MONTH ( MAX ( 'Calendar'[Date] ) )
VAR _prevmonthdate =
    CALCULATE ( MAX ( 'Calendar'[Date] ), DATEADD ( 'Calendar'[Date], -1, MONTH ) )
RETURN
    IF (
        NOT ISBLANK ( _prevmonthdate ),
        IF (
            _selectedmonth <> 1,
            CALCULATE (
                [Total sales:],
                FILTER (
                    ALL ( 'Calendar' ),
                    YEAR ( [Date] ) = _selectedyear
                        && MONTH ( [Date] ) = _selectedmonth - 1
                        && 'Calendar'[Date] <= _prevmonthdate
                )
            ),
            CALCULATE (
                [Total sales:],
                FILTER (
                    ALL ( 'Calendar' ),
                    YEAR ( [Date] ) = _selectedyear - 1
                        && MONTH ( [Date] ) = 12
                        && 'Calendar'[Date] <= _prevmonthdate
                )
            )
        )
    )

 

 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

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.