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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
ssab_wenx
Frequent Visitor

Rolling Value For Last 3 Months

Hi All,

 

I have a question about how to calculate the last 3 months rolling value. 

Let's say I have a below table:

ssab_wenx_0-1741943017609.png

 

and I want to create a measure to get the rolling 3 before month values(it should calculate by any filter), the result should looks like(if user select the country filter) :

ssab_wenx_1-1741943076924.png

 

this is a measure I created:

Rolling3monthCostPerTon = 
CALCULATE(
     DIVIDE(SUM([cost]), SUM([ton]), 0),
     DATESBETWEEN(
        'table'[createdate],
        EDATE('table'[createdate], -3),
        'table'[createdate]
    )
)

The result is not what I wanted.

So is there anything wrong about my measure? 

 

1 ACCEPTED SOLUTION

Hi,

Try this approach

  1. Create a Calendar Table with calculated column formulas for Year, Month name and Month number.  Sort the Month name by the Month number column
  2. Create a relationship (Many to One and Single) from the Date column of the Fact table to the Date column of the Calendar table
  3. To your visual, drag Year and Month name from the Calendar table
  4. Write these measures

C = sum(Data[Cost])

T = sum(Data[Tonne])

CPT = divide([C],[T])

R3M C = calculate([C],datesbetween(calendar[date],edate(min(calendar[date]),-2),max(calendar[date])))

R3M T = calculate([T],datesbetween(calendar[date],edate(min(calendar[date]),-2),max(calendar[date])))

R3M CPT = divide([R3M C],[R3M T])

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

10 REPLIES 10
Ashish_Mathur
Super User
Super User

Hi,

for 2024/5/1, why should the rolling cost be 30?  Shouldn't it be 30+80+210 = 320?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

If the user don't filter the record by conuntry, yes it should be 30+80+210 = 320. sorry for the misunderstandings, the screenshot shows the value filter by country.

Hi,

Try this approach

  1. Create a Calendar Table with calculated column formulas for Year, Month name and Month number.  Sort the Month name by the Month number column
  2. Create a relationship (Many to One and Single) from the Date column of the Fact table to the Date column of the Calendar table
  3. To your visual, drag Year and Month name from the Calendar table
  4. Write these measures

C = sum(Data[Cost])

T = sum(Data[Tonne])

CPT = divide([C],[T])

R3M C = calculate([C],datesbetween(calendar[date],edate(min(calendar[date]),-2),max(calendar[date])))

R3M T = calculate([T],datesbetween(calendar[date],edate(min(calendar[date]),-2),max(calendar[date])))

R3M CPT = divide([R3M C],[R3M T])

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thanks for the reply. It works on local if I don't filter the records. The result becomes wrong if I  set a "Country" slicer. That means it only works on "createdate". Attached the pbix if you are avaliable to take a look. Thanks!

Share the download link of the PBI file.  Show the problem there very clearly.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi Ashish, I cannot upload the PBI file here since I am not a super user. Could you tell me your mail ? Really thanks!

Share the download link of the PBI file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

never mind, I solved the problem. Thanks!

bhanu_gautam
Super User
Super User

@ssab_wenx Try using

DAX
Rolling3MonthCost =
CALCULATE(
SUM('table'[cost]),
DATESINPERIOD(
'table'[createdate],
MAX('table'[createdate]),
-3,
MONTH
)
)

 

DAX
Rolling3MonthTon =
CALCULATE(
SUM('table'[ton]),
DATESINPERIOD(
'table'[createdate],
MAX('table'[createdate]),
-3,
MONTH
)
)

 

DAX
Rolling3MonthCostPerTon =
DIVIDE(
[Rolling3MonthCost],
[Rolling3MonthTon],
0
)




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Hi, thanks for your reply but it doesn't works. As for the cost, it keeps the current row value and not calculate the total cost of last 3 months. For the "MAX('table'[createdate])", what does it mean?  I think it should be the current row data value. Could you help take a look? Thanks!

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.