# 13.17 RANKX आणि TOPN

Source: https://ravindrabagale.com/mr/powerbi/ch13-dax-data-analysis-expressions/13-17-ranking-rankx-and-topn.html
Language: mr (Marathi with English technical terms)

RANKX(table, expression, value, order, ties) table मधल्या items ना expression नुसार rank देतो.

Product Rank =
IF(
    HASONEVALUE(Product[Product Name]),
    RANKX(ALL(Product[Product Name]), [Total Sales], , DESC, DENSE)
)

-- Rank dark stores by speed: fastest (lowest time) = rank 1
Store Speed Rank =
IF(
    HASONEVALUE(DarkStore[Store Name]),
    RANKX(ALL(DarkStore[Store Name]), [Avg Delivery Time (mins)], , ASC, DENSE)
)

Customer Rank in City =
RANKX(ALLSELECTED(Customer[Customer Name]), [Total Sales], , DESC)

Top 5 Products Sales =
CALCULATE(
    [Total Sales],
    TOPN(5, ALL(Product[Product Name]), [Total Sales], DESC)
)

Top 5 Share % = DIVIDE([Top 5 Products Sales], CALCULATE([Total Sales], ALL(Product)))

-- Calculated table of the 10 busiest dark stores
Top 10 Stores =
TOPN(
    10,
    ADDCOLUMNS(VALUES(DarkStore[Store Name]), "Orders", [Total Orders]),
    [Orders], DESC
)

ALL वापरून current item च्या बाहेरची comparison list मिळवा; selectionपुरता rank हवा तर ALLSELECTED विचारात घ्या.

HASONEVALUE total row वर अनावश्यक rank टाळू शकतो; hierarchy totalsसाठी ISINSCOPE उपयोगी असू शकतो.

DESC मध्ये मोठी value rank1; ASC मध्ये छोटी value rank1.

SKIP ties:1,2,2,4; DENSE:1,2,2,3.

Store speed rank मध्ये blank timesना fastest समजू नका; eligible stores filter करा. Product / Customer Name unique नसेल तर unique key वर ranking ठरवा.

TOPN

Top N rows ची table देतो. शेवटच्या rank ला tie असेल तर Nपेक्षा जास्त rows येऊ शकतात. Deterministic selection हवी तर secondary sort key द्या. Calculated Top 10 Stores table refresh ला बदलते; slicerला dynamic top10 हवं तर visual filter किंवा measure वापरा.

फक्त chart मध्ये top10 दाखवण्यासाठी Filters pane मधला Top N पुरू शकतो. Share सारखा calculation हवा तेव्हा DAX उपयोगी.

रवींद्र बागले यांची tip

Current filter असलेल्या Product table वर rank केल्यास सगळे rank1 दिसू शकतात. Comparison scope आणि ties स्पष्ट ठेवा.
