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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
kvnbn
Frequent Visitor

Calculate median for each category based on its most recent time period

I have the following data in Power BI:

DateItemClientGroup%
2/28/2023FoodALarge35%
2/22/2023FoodALarge100%
2/15/2023FoodALarge35%
2/8/2023FoodALarge35%
2/1/2023FoodALarge35%
1/23/2023FoodALarge35%
1/18/2023FoodALarge35%
1/12/2023FoodALarge35%
1/4/2023FoodALarge35%
6/5/2022DrinkBLarge25%
6/3/2022DrinkBLarge25%
6/2/2022FoodALarge35%
5/30/2022DrinkBLarge25%
5/20/2022FoodALarge35%
4/29/2022FoodALarge35%
4/7/2022FoodALarge35%
3/24/2022FoodALarge35%
3/11/2022FoodALarge35%
2/25/2022FoodALarge36%
2/10/2022FoodALarge36%
1/25/2022FoodALarge36%
1/14/2022FoodALarge36%
12/30/2021FoodALarge36%
12/3/2021FoodALarge36%
11/15/2021FoodALarge36%
11/3/2021FoodALarge36%
10/27/2021FoodALarge36%
10/13/2021FoodALarge36%
9/29/2021FoodALarge36%
9/22/2021FoodALarge36%
9/15/2021FoodALarge36%
9/8/2021FoodALarge36%
9/1/2021FoodALarge36%
8/26/2021FoodALarge36%
8/18/2021FoodALarge36%
8/11/2021FoodALarge36%
8/2/2021FoodALarge36%
7/21/2021FoodALarge36%
7/14/2021FoodALarge36%
7/7/2021FoodALarge36%
6/30/2021FoodALarge0%
6/23/2021FoodALarge36%
6/15/2021FoodALarge36%
6/8/2021FoodALarge36%
6/2/2021FoodALarge36%
5/25/2021FoodALarge36%
5/18/2021FoodALarge36%
11/5/2020DrinkALarge23%
11/3/2020DrinkALarge23%
11/1/2020DrinkALarge23%
9/23/2020FoodALarge34%
9/16/2020FoodALarge34%

 

I am trying to calculate the median % for each Item, but only calculating from the lines in the most recent year for each Item. For example, Food has a most recent year of 2023, and Drink has a most recent year of 2022, so I only want to calculate the median % for Food from 2023, and % median for Drink from 2022.

 

Here are my current measures: 

 

Median = 
CALCULATE(
    MEDIANX(
        FILTER(
            'Table',
            'Table'[Date].[Year] = MAX('Table'[Date].[Year])
        ),
        IF(
            'Table'[%] > 0,
            'Table'[%],
            BLANK()
        )
    )
)

 

 

 

RecentYr = 
MAXX(
    SUMMARIZE(
        'Table',
        'Table'[Item],
        "MaxYear", MAX('Table'[Date].[Year])
    ),
    [MaxYear]
)

 

 

 

Median2 = 
CALCULATE(
    MEDIANX(
        FILTER(
            'Table',
            'Table'[Date].[Year] = [RecentYr]
        ),
        IF(
            'Table'[%] > 0, 
            'Table'[%],
            BLANK()
        )
    )
)

 


I am trying to summarize this info in a table visual. This is what it currently shows: 

kvnbn_1-1697483425713.png
It seems that using the MAX function in Median does not yield an answer for Drink because Max year is 2023 and there is no Drink in 2023. Meanwhile, Median2 does not seem to filter to the most recent year for each item, instead calculating the Median from all years. 


Desired output:

ItemMedianMostRecentYr
Drink25%2022
Food35%2023

 

Currently I can only get these numbers by having a slicer that selects the year, but my goal is to have a table visual that summarizes the most recent data for each Item. My raw data also has columns for Client and Group because I need to be able to filter by those two columns in slicers as well. 

 

Any help would be much appreciated, thank you!

1 ACCEPTED SOLUTION
Greg_Deckler
Community Champion
Community Champion

@kvnbn Try this. PBIX is attached below signature:

Most Recent Year Median = 
    VAR __MaxYear = YEAR(MAX('Table'[Date]))
    VAR __Table = FILTER('Table', YEAR([Date]) = __MaxYear && [%] > 0)
    VAR __Return = MEDIANX(__Table, [%])
RETURN
    __Return

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

3 REPLIES 3
Greg_Deckler
Community Champion
Community Champion

@kvnbn Try this. PBIX is attached below signature:

Most Recent Year Median = 
    VAR __MaxYear = YEAR(MAX('Table'[Date]))
    VAR __Table = FILTER('Table', YEAR([Date]) = __MaxYear && [%] > 0)
    VAR __Return = MEDIANX(__Table, [%])
RETURN
    __Return

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

This is such an elegant solution, thank you!

@kvnbn Thanks! It's the No CALCULATE approach. Every measure pretty much looks that way. Create a few VAR's, create a FILTERED or SUMMARIZED table, use an X Aggregator. Just works.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.