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
abhishek_2593
Helper II
Helper II

Need help with the descending sort for this DAX values

Hi, 
I have the following DAX to dynamically select the TOP N values for % of followers from each city but unable to define the sort by descending in this DAX.
What changes can i make to define this descending sort?


Here's the DAX

 

Top Cities KPI =
VAR N = SELECTEDVALUE('Top'[top])
VAR CityTable =
    SUMMARIZE(
        'Table (2)',
        'Table (2)'[City],
        "TotalFAN"COUNT('Table (2)'[FAN ID])
    )

 

VAR TotalFAN = CALCULATE(COUNT('Table (2)'[FAN ID]))

 

VAR TopCitiesTable =
    TOPN(NCityTable[TotalFAN]DESC)

 

VAR ResultTable =
    ADDCOLUMNS(
        TopCitiesTable,
        "Percentage"FORMAT(DIVIDE([TotalFAN]TotalFAN), "0.0%")
    )

 

VAR ResultString =
    CONCATENATEX(
        ResultTable,
        [Percentage] & " in " & [City],
        ", "
    )

 

RETURN
    ResultString
 
 
 
3. use as a silcer the table you created for select top N values.


abhishek_2593_0-1747198524079.png

The sorting should be in descending order.

Thank you.

2 ACCEPTED SOLUTIONS
OwenAuger
Super User
Super User

Hi @abhishek_2593 

To fix the sorting issue, add two arguments to CONCATENATEX:

VAR ResultString =
    CONCATENATEX(
        ResultTable,
        [Percentage] & " in " & [City],
        ", ",
        [Percentage], DESC
    )

A possible alternative version of the measure consolidating some steps:

Top Cities KPI =
VAR N = SELECTEDVALUE ( 'Top'[top] )
VAR TotalFAN =
    COUNT ( 'Table (2)'[FAN ID] )
VAR CityPercentage =
    SUMMARIZECOLUMNS (
        'Table (2)'[City],
        "Percentage", DIVIDE ( COUNT ( 'Table (2)'[FAN ID] ), TotalFan )
    )
VAR TopCityPercentage =
    TOPN ( N, CityPercentage, [Percentage], DESC )
VAR ResultString =
    CONCATENATEX (
        TopCityPercentage,
        FORMAT ( [Percentage], "0.0%" ) & " in " & 'Table (2)'[City],
        ", ",
        [Percentage], DESC
    )
RETURN
    ResultString

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

View solution in original post

mdaatifraza5556
Super User
Super User

Hi @abhishek_2593 

Top Cities KPI =
VAR N = SELECTEDVALUE('Top'[top])
VAR CityTable =
SUMMARIZE(
'Table (2)',
'Table (2)'[City],
"TotalFAN", COUNT('Table (2)'[FAN ID])
)

VAR TotalFAN =
CALCULATE(COUNT('Table (2)'[FAN ID]))

VAR TopCitiesTable =
TOPN(N, CityTable, [TotalFAN], DESC)

-- Add both numeric and formatted percentage
VAR ResultTable =
ADDCOLUMNS(
TopCitiesTable,
"NumericPercentage", DIVIDE([TotalFAN], TotalFAN),
"FormattedPercentage", FORMAT(DIVIDE([TotalFAN], TotalFAN), "0.0%")
)

-- Use line break with UNICHAR(10)
VAR ResultString =
CONCATENATEX(
ResultTable,
[FormattedPercentage] & " in " & [City],
UNICHAR(10),
[NumericPercentage], DESC
)

RETURN
ResultString



If this answers your questions, kindly accept it as a solution and give kudos.

View solution in original post

2 REPLIES 2
mdaatifraza5556
Super User
Super User

Hi @abhishek_2593 

Top Cities KPI =
VAR N = SELECTEDVALUE('Top'[top])
VAR CityTable =
SUMMARIZE(
'Table (2)',
'Table (2)'[City],
"TotalFAN", COUNT('Table (2)'[FAN ID])
)

VAR TotalFAN =
CALCULATE(COUNT('Table (2)'[FAN ID]))

VAR TopCitiesTable =
TOPN(N, CityTable, [TotalFAN], DESC)

-- Add both numeric and formatted percentage
VAR ResultTable =
ADDCOLUMNS(
TopCitiesTable,
"NumericPercentage", DIVIDE([TotalFAN], TotalFAN),
"FormattedPercentage", FORMAT(DIVIDE([TotalFAN], TotalFAN), "0.0%")
)

-- Use line break with UNICHAR(10)
VAR ResultString =
CONCATENATEX(
ResultTable,
[FormattedPercentage] & " in " & [City],
UNICHAR(10),
[NumericPercentage], DESC
)

RETURN
ResultString



If this answers your questions, kindly accept it as a solution and give kudos.

OwenAuger
Super User
Super User

Hi @abhishek_2593 

To fix the sorting issue, add two arguments to CONCATENATEX:

VAR ResultString =
    CONCATENATEX(
        ResultTable,
        [Percentage] & " in " & [City],
        ", ",
        [Percentage], DESC
    )

A possible alternative version of the measure consolidating some steps:

Top Cities KPI =
VAR N = SELECTEDVALUE ( 'Top'[top] )
VAR TotalFAN =
    COUNT ( 'Table (2)'[FAN ID] )
VAR CityPercentage =
    SUMMARIZECOLUMNS (
        'Table (2)'[City],
        "Percentage", DIVIDE ( COUNT ( 'Table (2)'[FAN ID] ), TotalFan )
    )
VAR TopCityPercentage =
    TOPN ( N, CityPercentage, [Percentage], DESC )
VAR ResultString =
    CONCATENATEX (
        TopCityPercentage,
        FORMAT ( [Percentage], "0.0%" ) & " in " & 'Table (2)'[City],
        ", ",
        [Percentage], DESC
    )
RETURN
    ResultString

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

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.