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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

How to mimic SQL command "SELECT TOP 1 col FROM table WHERE condition ORDER BY othercol DESC" in DAX

Question is pretty much in the subject.

 

I want a measure that mimic this SQL command : 

SELECT TOP 1 col

FROM table

WHERE condition

ORDER BY othercol DESC

 

Example input and output :

MyTable

id tag value

1 colour blue

2 shape triangle

3 height 7

4 shape round

5 colour orange

 

if I apply this SQL query :

SELECT TOP 1 value

FROM MyTable

WHERE tag = 'colour'

ORDER BY id DESC

 

it will return "orange"

 

How to do exactly the same in DAX ?

1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

Measure = 
MAXX(
    TOPN( 1, FILTER( INFO, INFO[tag] = "colour" ), INFO[id] ),
    INFO[value]
)

Screenshot 2021-07-01 125918.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

2 REPLIES 2
CNENFRNL
Community Champion
Community Champion

Measure = 
MAXX(
    TOPN( 1, FILTER( INFO, INFO[tag] = "colour" ), INFO[id] ),
    INFO[value]
)

Screenshot 2021-07-01 125918.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Anonymous
Not applicable

Fantastic. Thanks.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors