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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

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
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.