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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
AnthonyXelya
Helper II
Helper II

Calculate the median of previous year

Hello,
 
I'm trying to show in a report the median of a filtered year and the median of the previous year of this one.
 
For the filtered year, it's ok thanks to :
 
 Median Current Year = MEDIANX(
                               SUMMARIZE(MyTable,
                MyTable[Id_Entity],
                                "Sum", CALCULATE(SUM(MyTable[Value]),
                                       FILTER(MyTable,
                                       MyTable[Date] >= MIN(Calendar[Date])
                                       && MyTable[Date] <= MAX(Calendar[Date]))))
                                      , [Sum]) 
 
But for the previous year, since I have a filter on the date in another year, my "Table" doesn't return any data... So this doesn't return anything :
 
Median Last Year =
VAR minDate = DATE(YEAR(MIN(Calendar[Date]))-1, MONTH(MIN(Calendar[Date])), DAY(MIN(Calendar[Date])))
VAR maxDate = DATE(YEAR(MAX(Calendar[Date]))-1, MONTH(MAX(Calendar[Date])), DAY(MAX(Calendar[Date])))
 
VAR Result = MEDIANX(
                            SUMMARIZE(MyTable,
                                      MyTable[Id_Entity],
                                      "Sum", CALCULATE(SUM(MyTable[Value]),
                                                  FILTER(ALL(Calendar[Date]),
                                                  Calendar[Date] >= minDate
                                                  && Calendar[Date] <= maxDate)) )
                        , [Sum])
RETURN Result 
 
 
Any idea to help me to get rid of the year filter in my 2nd formula?
 
1 ACCEPTED SOLUTION

Hi @AnthonyXelya - Can you share the sample data for reference 

I have removed both all and allselected,CALCULATETABLE to create a filtered version of MyTable that only includes rows where the Date column falls within the specified date range of the previous year.

 

Try this

Median Last Year =
VAR minDate = DATE(YEAR(MIN(Calendar[Date])) - 1, MONTH(MIN(Calendar[Date])), DAY(MIN(Calendar[Date])))
VAR maxDate = DATE(YEAR(MAX(Calendar[Date])) - 1, MONTH(MAX(Calendar[Date])), DAY(MAX(Calendar[Date])))

VAR FilteredTable =
CALCULATETABLE(
MyTable,
Calendar[Date] >= minDate && Calendar[Date] <= maxDate
)

VAR Result =
MEDIANX(
SUMMARIZE(
FilteredTable,
MyTable[Id_Entity],
"Sum", SUM(MyTable[Value])
),
[Sum]
)
RETURN Result





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





View solution in original post

7 REPLIES 7
AnthonyXelya
Helper II
Helper II

Still nobody to help me? 😞

rajendraongole1
Super User
Super User

Hi @AnthonyXelya  Modified 2nd forumale for previous year,  It will correctly calculate the median for the previous year check below measure:

 

Median Last Year =
VAR minDateCurrentYear = MIN(Calendar[Date])
VAR maxDateCurrentYear = MAX(Calendar[Date])
VAR minDatePreviousYear = DATE(YEAR(minDateCurrentYear) - 1, MONTH(minDateCurrentYear), DAY(minDateCurrentYear))
VAR maxDatePreviousYear = DATE(YEAR(maxDateCurrentYear) - 1, MONTH(maxDateCurrentYear), DAY(maxDateCurrentYear))

VAR Result =
MEDIANX(
SUMMARIZE(
MyTable,
MyTable[Id_Entity],
"Sum", CALCULATE(
SUM(MyTable[Value]),
FILTER(
ALL(Calendar[Date]),
Calendar[Date] >= minDatePreviousYear &&
Calendar[Date] <= maxDatePreviousYear
)
)
),
[Sum]
)
RETURN
Result

 

Hope it works. pls check





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Thanks for your answer... sadly it doesn't change anything 😞

Hi @AnthonyXelya -To ensure the context of other filters is maintained, we can use ALLSELECTED instead of ALL

 

Median Last Year =
VAR minDate = DATE(YEAR(MIN(Calendar[Date]))-1, MONTH(MIN(Calendar[Date])), DAY(MIN(Calendar[Date])))
VAR maxDate = DATE(YEAR(MAX(Calendar[Date]))-1, MONTH(MAX(Calendar[Date])), DAY(MAX(Calendar[Date])))

VAR Result =
MEDIANX(
SUMMARIZE(
MyTable,
MyTable[Id_Entity],
"Sum",
CALCULATE(
SUM(MyTable[Value]),
FILTER(
ALLSELECTED(Calendar),
Calendar[Date] >= minDate && Calendar[Date] <= maxDate
)
)
),
[Sum]
)
RETURN Result

 

If this approach still doesn’t work, it might be helpful to inspect the underlying data or test the calculation in a simple scenario to isolate the issue.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Sadly, still the same : 

AnthonyXelya_0-1724685452396.png

 

Hi @AnthonyXelya - Can you share the sample data for reference 

I have removed both all and allselected,CALCULATETABLE to create a filtered version of MyTable that only includes rows where the Date column falls within the specified date range of the previous year.

 

Try this

Median Last Year =
VAR minDate = DATE(YEAR(MIN(Calendar[Date])) - 1, MONTH(MIN(Calendar[Date])), DAY(MIN(Calendar[Date])))
VAR maxDate = DATE(YEAR(MAX(Calendar[Date])) - 1, MONTH(MAX(Calendar[Date])), DAY(MAX(Calendar[Date])))

VAR FilteredTable =
CALCULATETABLE(
MyTable,
Calendar[Date] >= minDate && Calendar[Date] <= maxDate
)

VAR Result =
MEDIANX(
SUMMARIZE(
FilteredTable,
MyTable[Id_Entity],
"Sum", SUM(MyTable[Value])
),
[Sum]
)
RETURN Result





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





It's working! Thank you very much dude!!

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.