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
kasife
Helper V
Helper V

last 3 months without counting the current month

Hi Guys,

 

I'm using a measure to know the accumulated result of the last 3 months, but I would like to get the result of the last 3 months without counting the current month. How could I do this?

My measure:

 

 

 CALCULATE(
        [_Total vendas],
        DATESINPERIOD(d_calendario[Date],MAX(d_calendario[Date]),-3,MONTH))

 

 

 The result I want to get:

kasife_0-1678643168116.png

 

 

2 ACCEPTED SOLUTIONS
MohammadLoran25
Super User
Super User

Hiii,

If you search and read the explanations for STARTOFMONTH and DATESINPERIOD Functions, you can get the result you are looking for by measure below:

 

CALCULATE (
    [_Total Vendas],
    DATESINPERIOD (
        d_calendario[Date],
        STARTOFMONTH ( DATEADD ( d_calendario[Date], -3MONTH ) ),
        +2,
        MONTH
    )
)

 

Notice that it is more general than the thing you wanted. Actually it calculates the previous 3months per month (for example if your month is Dec, then it calculates over Sept, Oct, Nov).

So If you only want to calculate previous 3months based on current month, you need to change it accordingly.

 

Regards,

Loran

View solution in original post

Lets do it completely in another way.

 

1-In your DateTable, Create a calculated column as below:

 

YearMonthIndex=YEAR(DateTable[Date])*12+MONTH(DateTable[Date])
 
It gives you the index for combinations of year and month.
 
2-Then Create your Previous 3 Months measure as this:
 
CALCULATE (
    [_Total Vendas],
    FILTER (
        ALL ( DateTable ),
        DateTable[YearMonthIndex]
            >= MAX ( DateTable[YearMonthIndex] ) - 3
            && DateTable[YearMonthIndex]
                <= MAX ( DateTable[YearMonthIndex] ) - 1
    )
)
 
If it solves your problem, then please consider Accepting it as the solution
Regards,
Loran

View solution in original post

9 REPLIES 9
MohammadLoran25
Super User
Super User

Hiii,

If you search and read the explanations for STARTOFMONTH and DATESINPERIOD Functions, you can get the result you are looking for by measure below:

 

CALCULATE (
    [_Total Vendas],
    DATESINPERIOD (
        d_calendario[Date],
        STARTOFMONTH ( DATEADD ( d_calendario[Date], -3MONTH ) ),
        +2,
        MONTH
    )
)

 

Notice that it is more general than the thing you wanted. Actually it calculates the previous 3months per month (for example if your month is Dec, then it calculates over Sept, Oct, Nov).

So If you only want to calculate previous 3months based on current month, you need to change it accordingly.

 

Regards,

Loran

@MohammadLoran25 How would this measurement look from a selected date? Can you help me?

@kasife 

What do you mean exactly?

As I said it is different per month. For example if the month in your table row is Dec, then the previous 3 months would be Sept, Oct, Nov in order.

 

In the above-mentioned example, the statement below:

STARTOFMONTH ( DATEADD ( d_calendario[Date], -3MONTH ) )

would be the first day of Sept

 

then through:

DATESINPERIOD(...., +2,Month)

It would include the Sept, Oct and November that finally filter d_calendario[Date]

@MohammadLoran25 

I tried to demonstrate in the image the result I would like:

kasife_1-1678992972944.png

 

I got what you need.

The measure that I wrote works in this case as well.

Is there anything wrong with that?

@MohammadLoran25 The measure is returning the last 3 months always starting from the current month.

Lets do it completely in another way.

 

1-In your DateTable, Create a calculated column as below:

 

YearMonthIndex=YEAR(DateTable[Date])*12+MONTH(DateTable[Date])
 
It gives you the index for combinations of year and month.
 
2-Then Create your Previous 3 Months measure as this:
 
CALCULATE (
    [_Total Vendas],
    FILTER (
        ALL ( DateTable ),
        DateTable[YearMonthIndex]
            >= MAX ( DateTable[YearMonthIndex] ) - 3
            && DateTable[YearMonthIndex]
                <= MAX ( DateTable[YearMonthIndex] ) - 1
    )
)
 
If it solves your problem, then please consider Accepting it as the solution
Regards,
Loran

@MohammadLoran25  It worked perfectly. Thank you very much 🙂

Thanks so much @MohammadLoran25 

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.