Join us for an expert-led overview of the tools and concepts you'll need to pass exam PL-300. The first session starts on June 11th. See you there!
Get registeredPower BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.
Hi All,
I have a question about how to calculate the last 3 months rolling value.
Let's say I have a below table:
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) :
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?
Solved! Go to Solution.
Hi,
Try this approach
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.
Hi,
for 2024/5/1, why should the rolling cost be 30? Shouldn't it be 30+80+210 = 320?
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
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.
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.
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.
never mind, I solved the problem. Thanks!
@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
)
Proud to be a Super User! |
|
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!
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
76 | |
76 | |
55 | |
37 | |
34 |
User | Count |
---|---|
99 | |
56 | |
53 | |
44 | |
40 |