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
aellison
Helper I
Helper I

Calculate SUM on previous month, but previous month changes based on day of month

I'm trying to figure out how to compare 2 months totals based on a date range. The date range required changes on the 15th of the month. Also, this needs to be a measure. I am trying to help someone with an issue where the data source is a tabular model, so creating columns is not an option without changing the cube (which requires change control and a longer time to wait)

 

Example:

     If the report was run on from April 1st - 14th, then the sum comparison would be for Feb vs March.

     If the report was run on April 15th throug the end of the month , then the sum comparison would be for March vs April.

 

    Any prevous months would use it's own previous month (or parellelperiod) to compare. Only the current month's comparison changes based on the day of month.

 

Thanks in advance,

1 ACCEPTED SOLUTION
v-yulgu-msft
Employee
Employee

Hi @aellison ,

 

Please refer to below measures:

Current Month = 
IF (
    DAY ( TODAY () ) <= 14,
    IF (
        MONTH ( TODAY () ) <> 1,
        CALCULATE (
            SUM ( Sample1[Sales] ),
            FILTER (
                ALLSELECTED ( Sample1 ),
                YEAR ( Sample1[Date] ) = YEAR ( TODAY () )
                    && MONTH ( Sample1[Date] )
                        = MONTH ( TODAY () ) - 1
            )
        ),
        CALCULATE (
            SUM ( Sample1[Sales] ),
            FILTER (
                ALLSELECTED ( Sample1 ),
                YEAR ( Sample1[Date] )
                    = YEAR ( TODAY () ) - 1
                    && MONTH ( Sample1[Date] ) = 12
            )
        )
    ),
    CALCULATE (
        SUM ( Sample1[Sales] ),
        FILTER (
            ALLSELECTED ( Sample1 ),
            YEAR ( Sample1[Date] ) = YEAR ( TODAY () )
                && MONTH ( Sample1[Date] ) = MONTH ( TODAY () )
        )
    )
)

Previous Month = 
IF (
    DAY ( TODAY () ) <= 14,
    IF (
        MONTH ( TODAY () ) = 1,
        CALCULATE (
            SUM ( Sample1[Sales] ),
            FILTER (
                ALLSELECTED ( Sample1 ),
                YEAR ( Sample1[Date] )
                    = YEAR ( TODAY () ) - 1
                    && MONTH ( Sample1[Date] ) = 11
            )
        ),
        IF (
            MONTH ( TODAY () ) = 2,
            CALCULATE (
                SUM ( Sample1[Sales] ),
                FILTER (
                    ALLSELECTED ( Sample1 ),
                    YEAR ( Sample1[Date] )
                        = YEAR ( TODAY () ) - 1
                        && MONTH ( Sample1[Date] ) = 12
                )
            ),
            CALCULATE (
                SUM ( Sample1[Sales] ),
                FILTER (
                    ALLSELECTED ( Sample1 ),
                    YEAR ( Sample1[Date] ) = YEAR ( TODAY () )
                        && MONTH ( Sample1[Date] )
                            = MONTH ( TODAY () ) - 2
                )
            )
        )
    ),
    IF (
        MONTH ( TODAY () ) <> 1,
        CALCULATE (
            SUM ( Sample1[Sales] ),
            FILTER (
                ALLSELECTED ( Sample1 ),
                YEAR ( Sample1[Date] ) = YEAR ( TODAY () )
                    && MONTH ( Sample1[Date] )
                        = MONTH ( TODAY () ) - 1
            )
        ),
        CALCULATE (
            SUM ( Sample1[Sales] ),
            FILTER (
                ALLSELECTED ( Sample1 ),
                YEAR ( Sample1[Date] )
                    = YEAR ( TODAY () ) - 1
                    && MONTH ( Sample1[Date] ) = 12
            )
        )
    )
)

Curr VS Prev = [Current Month]-[Previous Month]

2.PNG

 

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-yulgu-msft
Employee
Employee

Hi @aellison ,

 

Please refer to below measures:

Current Month = 
IF (
    DAY ( TODAY () ) <= 14,
    IF (
        MONTH ( TODAY () ) <> 1,
        CALCULATE (
            SUM ( Sample1[Sales] ),
            FILTER (
                ALLSELECTED ( Sample1 ),
                YEAR ( Sample1[Date] ) = YEAR ( TODAY () )
                    && MONTH ( Sample1[Date] )
                        = MONTH ( TODAY () ) - 1
            )
        ),
        CALCULATE (
            SUM ( Sample1[Sales] ),
            FILTER (
                ALLSELECTED ( Sample1 ),
                YEAR ( Sample1[Date] )
                    = YEAR ( TODAY () ) - 1
                    && MONTH ( Sample1[Date] ) = 12
            )
        )
    ),
    CALCULATE (
        SUM ( Sample1[Sales] ),
        FILTER (
            ALLSELECTED ( Sample1 ),
            YEAR ( Sample1[Date] ) = YEAR ( TODAY () )
                && MONTH ( Sample1[Date] ) = MONTH ( TODAY () )
        )
    )
)

Previous Month = 
IF (
    DAY ( TODAY () ) <= 14,
    IF (
        MONTH ( TODAY () ) = 1,
        CALCULATE (
            SUM ( Sample1[Sales] ),
            FILTER (
                ALLSELECTED ( Sample1 ),
                YEAR ( Sample1[Date] )
                    = YEAR ( TODAY () ) - 1
                    && MONTH ( Sample1[Date] ) = 11
            )
        ),
        IF (
            MONTH ( TODAY () ) = 2,
            CALCULATE (
                SUM ( Sample1[Sales] ),
                FILTER (
                    ALLSELECTED ( Sample1 ),
                    YEAR ( Sample1[Date] )
                        = YEAR ( TODAY () ) - 1
                        && MONTH ( Sample1[Date] ) = 12
                )
            ),
            CALCULATE (
                SUM ( Sample1[Sales] ),
                FILTER (
                    ALLSELECTED ( Sample1 ),
                    YEAR ( Sample1[Date] ) = YEAR ( TODAY () )
                        && MONTH ( Sample1[Date] )
                            = MONTH ( TODAY () ) - 2
                )
            )
        )
    ),
    IF (
        MONTH ( TODAY () ) <> 1,
        CALCULATE (
            SUM ( Sample1[Sales] ),
            FILTER (
                ALLSELECTED ( Sample1 ),
                YEAR ( Sample1[Date] ) = YEAR ( TODAY () )
                    && MONTH ( Sample1[Date] )
                        = MONTH ( TODAY () ) - 1
            )
        ),
        CALCULATE (
            SUM ( Sample1[Sales] ),
            FILTER (
                ALLSELECTED ( Sample1 ),
                YEAR ( Sample1[Date] )
                    = YEAR ( TODAY () ) - 1
                    && MONTH ( Sample1[Date] ) = 12
            )
        )
    )
)

Curr VS Prev = [Current Month]-[Previous Month]

2.PNG

 

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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.