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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

TopN based on Measure instead of Column

Hi everyone, I am struggling with the TopN function and I hope you can help me out.

Data: event registration data, with calculated columns for CY (current year) and LY (Last year). I also created a measure to show the count difference between them, “CY-LY Reg Diff”

Goal: create a card that displays the region and store type with the largest deficits year-on-year (smallest value for “CY-LY Diff” measure)

Challenge: I have tried this formula, but it gives me “blank” as a region.

Largest Deficit Region = CALCULATE(SELECTEDVALUE('Sample Data'[Region]),TOPN(1,'Sample Data',[CY - LY Reg Diff],ASC))

 

YearStore TypeRegionRegisteredLast yearCurrent year
Year 1Store Type AWest110
Year 1Store Type CSouth110
Year 1Store Type CMidwest110
Year 1Store Type BWest110
Year 1Store Type BWest110
Year 1Store Type BWest110
Year 2Store Type CWest101
Year 2Store Type BMidwest101
Year 2Store Type BSouth101
Year 2Store Type CMidwest101

 

 

Any help or tips would be greatly appreciated.

 

Thank you.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

This piece of code:

 

Largest Deficit Region = 
CALCULATE(
    SELECTEDVALUE('Sample Data'[Region]),
    TOPN(1,
         'Sample Data',
         [CY - LY Reg Diff],
         ASC
    )
)

 

returns BLANK because SELECTEDVALUE returns BLANK when there is more than 1 region returned.

 

Change the expression under CALCULATE to show the number of regions - distinctcount( 'sample data'[region] ) - and you'll see it's not 1.

 

Please read this: https://dax.guide/topn/

 

Best

D

View solution in original post

4 REPLIES 4
Greg_Deckler
Super User
Super User

Could you use ADDCOLUMNS to add your TOPN column and then take the MIN or MAX of it?



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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Hi, I must be doing something wrong here. I added ADDCOLUMNS to the expression, but now it looks like it's using it as multiple columns- I thought that I only added one. 😞 

= CALCULATE( SELECTEDVALUE('Sample Data'[Region]), ADDCOLUMNS('Sample Data',"Top N",TOPN(1,'Sample Data',[CY - LY Reg Diff],ASC)))
Error message: "The expression referrs to multiple columns. Multiple columns cannot be converted to a scalar value."
 
I must be missing a step or three. 
Anonymous
Not applicable

This piece of code:

 

Largest Deficit Region = 
CALCULATE(
    SELECTEDVALUE('Sample Data'[Region]),
    TOPN(1,
         'Sample Data',
         [CY - LY Reg Diff],
         ASC
    )
)

 

returns BLANK because SELECTEDVALUE returns BLANK when there is more than 1 region returned.

 

Change the expression under CALCULATE to show the number of regions - distinctcount( 'sample data'[region] ) - and you'll see it's not 1.

 

Please read this: https://dax.guide/topn/

 

Best

D

Anonymous
Not applicable

Thank you- I was actually able to get it to work! 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.