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

Current month vs Previous month

Hi! Could you guys give some help? I'm struggling here.

I need to do a calculation between the current x last month, but I'll not use a date dimension to show each month in a table/graph. Because of that, the PrevMonth below is not working. What should I do to bring the last month data, so I can divide with the MTD and found the % between them?

ID Alert = COUNT('Alerts'[ID])
PrevMonth = CALCULATE([ID Alert],PARALLELPERIOD('Calendar'[Date],-1;MONTH))

Tks in advance.

1 ACCEPTED SOLUTION
v-ljerr-msft
Employee
Employee

Hi @Larry130,

 

If I understand you correctly, the formula below should work in your scenario. Smiley Happy

PrevMonth =
VAR currentMonth =
    MONTH ( TODAY () )
VAR currentYear =
    YEAR ( TODAY () )
RETURN
    IF (
        currentMonth = 1,
        CALCULATE (
            [ID Alert],
            FILTER (
                ALL ( 'Calendar'[Date] ),
                YEAR ( 'Calendar'[Date] )
                    = currentYear - 1
                    && MONTH ( 'Calendar'[Date] ) = 12
            )
        ),
        CALCULATE (
            [ID Alert],
            FILTER (
                ALL ( 'Calendar'[Date] ),
                YEAR ( 'Calendar'[Date] ) = currentYear
                    && MONTH ( 'Calendar'[Date] )
                        = currentMonth - 1
            )
        )
    )

 

Regards

View solution in original post

3 REPLIES 3
v-ljerr-msft
Employee
Employee

Hi @Larry130,

 

If I understand you correctly, the formula below should work in your scenario. Smiley Happy

PrevMonth =
VAR currentMonth =
    MONTH ( TODAY () )
VAR currentYear =
    YEAR ( TODAY () )
RETURN
    IF (
        currentMonth = 1,
        CALCULATE (
            [ID Alert],
            FILTER (
                ALL ( 'Calendar'[Date] ),
                YEAR ( 'Calendar'[Date] )
                    = currentYear - 1
                    && MONTH ( 'Calendar'[Date] ) = 12
            )
        ),
        CALCULATE (
            [ID Alert],
            FILTER (
                ALL ( 'Calendar'[Date] ),
                YEAR ( 'Calendar'[Date] ) = currentYear
                    && MONTH ( 'Calendar'[Date] )
                        = currentMonth - 1
            )
        )
    )

 

Regards

Dear @v-ljerr-msft ,

 

I have a similar problem but the following code shows blank. My scenario is to calculate max difference between two time duration (hours) which is already a calculated column (DiffDeliveryTime). Slight difference is that I don't want to use calendar table to filter out my values instead I want to use my own table in which dates are stored. The dates are repeating as many deliveries are scheduled on the same day as well.

 

PrevMonth =
VAR currentMonth =
    MONTH ( invoice[DeliveryDate] )
VAR currentYear =
    YEAR ( invoice[DeliveryDate] )
RETURN
    IF (
        currentMonth = 1,
        CALCULATE (
            MAX(DiffDeliveryTime),
            FILTER (
                ALL ( invoice[DeliveryDate]),
                YEAR ( invoice[DeliveryDate] )
                    = currentYear - 1
                    && MONTH ( invoice[DeliveryDate] ) = 12
            )
        ),
        CALCULATE (
            MAX(DiffDeliveryTime),
            FILTER (
                ALL ( invoice[DeliveryDate] ),
                YEAR ( invoice[DeliveryDate] ) = currentYear
                    && MONTH ( invoice[DeliveryDate] )
                        = currentMonth - 1
            )
        )
    )

 

PERFECT! Thanks @v-ljerr-msft

I've tried to use the SQLBI DAX Pattern for previous month, but was really hard to understand. Your way of doing it using VAR was way easier to understand. I'll study VAR!

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.