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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
ChrisFromOhio
Helper I
Helper I

Calculating Prior Sales at Monthly Level - Leap Year

Hi all - Having some trouble figuring out a calculated measure. My report allows users to select time ranges down to the monthly level. The below measure calculates last year's sales, accounting for incomplete months (i.e. sales of 1/1/2023 - 1/26/2023 will be compared to 1/1/2022 - 1/26/2022, even though the user has selected all of January 2023).

Sales PY = 
IF(
    [Sales] <> BLANK(),
    IF(
        HASONEVALUE('Calendar'[Date]),
        CALCULATE(
            [Sales],
            SAMEPERIODLASTYEAR('Calendar'[Date])
        ),
        IF(
            HASONEVALUE('Calendar'[Year]),
            CALCULATE(
                [Sales],
                DATESBETWEEN(
                    'Calendar'[Date],
                    EDATE(MIN('Calendar'[Date]),-12),
                    EDATE(MAX('Calendar'[Date]),-12)
                )
            )
        )
    )
)

However, I'm running into a problem when charting this data out and accounting for a leap year. My dataset has data starting 1/1/20, so in the chart below the lack of prior year sales for 2020 makes sense. But in 2021, February is missing, which I believe is because there was a 2/29/2020 and there is no 29th of February in 2021.

Capture.PNG

 

Can anyone help me as to why my measure won't calculate prior year sales for February 2021? Many thanks!

1 ACCEPTED SOLUTION
ChrisFromOhio
Helper I
Helper I

I have solved this issue. I rewrote my formula to account for partial months based off of today's date.

Sales PY = 
IF(
    MONTH(MAX('Calendar'[Date])) = MONTH(TODAY()),
    CALCULATE(
        [Sales],
        DATESBETWEEN(
            'Calendar'[Date],
            EDATE(MIN('Calendar'[Date]),-12),
            EDATE(MAX('Calendar'[Date]),-12)
        )
    ),
    IF(
        HASONEVALUE('Calendar'[Year]),
        CALCULATE(
            [Sales],
            SAMEPERIODLASTYEAR('Calendar'[Date])
        )
    )
)

View solution in original post

1 REPLY 1
ChrisFromOhio
Helper I
Helper I

I have solved this issue. I rewrote my formula to account for partial months based off of today's date.

Sales PY = 
IF(
    MONTH(MAX('Calendar'[Date])) = MONTH(TODAY()),
    CALCULATE(
        [Sales],
        DATESBETWEEN(
            'Calendar'[Date],
            EDATE(MIN('Calendar'[Date]),-12),
            EDATE(MAX('Calendar'[Date]),-12)
        )
    ),
    IF(
        HASONEVALUE('Calendar'[Year]),
        CALCULATE(
            [Sales],
            SAMEPERIODLASTYEAR('Calendar'[Date])
        )
    )
)

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors