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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
jasemilly
Helper II
Helper II

How to create top 5 previous year, allowing a this year / last year comparison

Hi 
I have created Clustered Column Chart that shows top 5 by category for the whole date range.  Broken down by year/month.  This works great.  It handles if a category has a month were it would be outside the top 5.  It's the top 5 for the date range and breaks down what these top 5 did each month.

From Aport To Htl Top 5 Countries = 

VAR TopFive = 
    TOPN(
        5, 
        SUMMARIZE(
            ALLSELECTED(MergedToHtlFromAirport),
            Country[Country], 
            "TotalSales", [Total # Rows From Airport]
        ), 
        [TotalSales], 
        DESC
    )

RETURN
CALCULATE(
    [Total # Rows From Airport],
    KEEPFILTERS( TopFive )
)

 

I have taken this measure and altered to try and look at the previous year.

 

So I can compare year on year change. 


Screenshot 2025-02-27 160347.png






Feb 2024 shows 1092 and happy with this LY showa 1146, this should be 1092.

 

This is the measure I created for the LY measure it doesn't quite work correctly and returns the top 5 qty for each month. 

 

Rather than the top 5 for the date range and then showing the breakdown for each month.  

 

From Aport To Htl Journeys Top 5 Countries LY = 
  
 VAR TopFivePrevYear = 
    TOPN(
        5, 
        SUMMARIZE(
		CalculateTable(
	            ALLSELECTED(MergedToHtlFromAirport),
        		SamePeriodLastYear('Date'[date]  )
	            ),	
	    Country[Country], 
            "TotalSales", [Total # Rows From Airport]
        ), 
        [TotalSales], 
        DESC
    )

RETURN

CALCULATE(
    [Total # Rows From Airport],
    KEEPFILTERS( TopFivePrevYear ),
    SAMEPERIODLASTYEAR('Date'[Date])
)

 

2 REPLIES 2
johnt75
Super User
Super User

Try

From Aport To Htl Journeys Top 5 Countries LY =
VAR TopFivePrevYear =
    TOPN (
        5,
        ADDCOLUMNS (
            SUMMARIZE ( ALLSELECTED ( MergedToHtlFromAirport ), Country[Country] ),
            "TotalSales", CALCULATE ( [Total # Rows From Airport], SAMEPERIODLASTYEAR ( 'Date'[date] ) )
        ),
        [TotalSales], DESC
    )
RETURN
    CALCULATE (
        [Total # Rows From Airport],
        KEEPFILTERS ( TopFivePrevYear ),
        SAMEPERIODLASTYEAR ( 'Date'[Date] )
    )

I added the ADDCOLUMNS around SUMMARIZE as you shouldn't use SUMMARIZE to generate calculated columns, always use ADDCOLUMNS or SELECTCOLUMNS for that.

The main problem I think is that you had the SAMEPERIODLASTYEAR in the wrong place. You want it when you are calculating the total, not when you are doing the ALLSELECTED.

That doesn't work getting the same values as the query I posted.

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.