Starting December 3, join live sessions with database experts and the Microsoft product team to learn just how easy it is to get started
Learn moreGet certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now
Hi Power BI Community,
I have two doubts:
1. There is a table with date, amount column. The time period of the table spans around 6 years. I am using table visual with date hierarchy (only year and month in axis). My requirement is to calculate the 3 years total for the same month.
Let's assume april row:
2020 April => 2020 April + 2019 April + 2018 April.
I wrote a DAX formula:
CALCULATE(
SUM('Table'[Amount]),
FILTER(
DATESINPERIOD('Table'[Date],LASTDATE('Table'[Date]),-3,YEAR),
MONTH('Table'[Date]) = MONTH(LASTDATE('Table'[Date]))
)
)
But it brings the value for each month. I mean whatever the value for 2021 April, it is bringing that one. I think I am missing another filter condition somewhere, but couldn't figure out.
2. I was reading about Cumulative total and found this formula:
Running Total MEASURE = CALCULATE ( SUM ( 'All Web Site Data (2)'[UniquePageviews] ), FILTER ( ALL ( 'All Web Site Data (2)' ), 'All Web Site Data (2)'[Date] <= MAX ( 'All Web Site Data (2)'[Date] ) ) )
But I couldn't understand one part. FILTER works in row context, so it goes row by row. But how does MAX bring the value for the current filter context. And also how does the 'All Web Site Data (2)'[Date] <= work if the filter context is still in place?
Thanks!
Solved! Go to Solution.
Hi, @dax_newbie
According to your description and requirement, I think you can create a measure that just uses variables and the Calculate() function to achieve this, you can try this measure:
I’ve also created some test data based on your description:
Last 3 years Same Month Sum =
var _selectedyear=YEAR(MIN('Table'[Date]))
var _selectedmonth=MONTH(MIN('Table'[Date]))
return
CALCULATE(SUM('Table'[Amount]),
FILTER(
ALL('Table'),
YEAR([Date])>=_selectedyear-2&&
YEAR([Date])<=_selectedyear&&
MONTH([Date])=_selectedmonth))
Then create a slicer to place the year and month column, a table visual to place this measure:
And you can get what you want, like this:
You can download my test pbix file below
Thank you very much!
If this result is not what you want, you can post some sample data(without sensitive data) and your expected result.
How to Get Your Question Answered Quickly
Best Regards,
Community Support Team _Robert Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi, @dax_newbie
According to your description and requirement, I think you can create a measure that just uses variables and the Calculate() function to achieve this, you can try this measure:
I’ve also created some test data based on your description:
Last 3 years Same Month Sum =
var _selectedyear=YEAR(MIN('Table'[Date]))
var _selectedmonth=MONTH(MIN('Table'[Date]))
return
CALCULATE(SUM('Table'[Amount]),
FILTER(
ALL('Table'),
YEAR([Date])>=_selectedyear-2&&
YEAR([Date])<=_selectedyear&&
MONTH([Date])=_selectedmonth))
Then create a slicer to place the year and month column, a table visual to place this measure:
And you can get what you want, like this:
You can download my test pbix file below
Thank you very much!
If this result is not what you want, you can post some sample data(without sensitive data) and your expected result.
How to Get Your Question Answered Quickly
Best Regards,
Community Support Team _Robert Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
i have sales for 3 years like 2022 and 2023 and 2024 now if i use sameperiod last year i will get 2023 sales so should i get 2022 sales please help me resovle this
This really helped me to get a similar calculation. Thank you so much!!
Hi, @dax_newbie
Please try the below measure.
Same Month 3 Years Total =
CALCULATE (
[Amount],
FILTER ( ALL ( Dates ), Dates[Month] = MAX ( Dates[Month] ) ),
DATESINPERIOD ( Dates[Date], LASTDATE ( Dates[Date] ), -3, YEAR )
)
Hi, My name is Jihwan Kim.
If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.
If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.
@dax_newbie , with April selected you can try measure like example measure
Assume april 2021 is selected
MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-12,MONTH))) +
CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-24,MONTH))) +CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-36,MONTH)))
Hi @amitchandak Thanks for the reply. But I am trying to come up with a solution that can be easily changed. Let's assume if I wanted to make last 10 years total, I would need to add lot of DATESMTD function.
Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.
User | Count |
---|---|
94 | |
85 | |
84 | |
73 | |
49 |
User | Count |
---|---|
143 | |
132 | |
110 | |
66 | |
55 |