Forum Discussion
Need help with DAX for Top N values
Hi,
I have written a DAX to display the Topmost % of certain categories from the data table
For ex - 20% of my followers live in London, 85% identify themselves as Male etc.
This is my DAX :
and here is how it is displayed in visual(CARD) -
However, I would like to change the DAX to display not just the Topmost but Top 5 or 10 instead. I'm finding it difficult to display the same.
Can you please help ?
Thanks in advance.
Hi Anonymous
Can you please try the below steps.
1. First, create a table to select the top values
By entering manually ( enter data option and enter a value for how much you want in top)
2. Use below DAX to find the top N values
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)VAR ResultTable =ADDCOLUMNS(TopCitiesTable,"Percentage", FORMAT(DIVIDE([TotalFAN], TotalFAN), "0.0%"))VAR ResultString =CONCATENATEX(ResultTable,[Percentage] & " in " & [City],", ")RETURNResultString3. use as a silcer the table you created for select top N values.
If this answers your questions, kindly accept it as a solution and give kudos.Hi Anonymous
Can you please try the below DAX?I have modified only in (VAR ResultTable and VAR ResultString)
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)
VAR ResultTable =
ADDCOLUMNS(
TopCitiesTable,
"NumericPercentage", DIVIDE([TotalFAN], TotalFAN),
"FormattedPercentage", FORMAT(DIVIDE([TotalFAN], TotalFAN), "0.0%")
)
VAR ResultString =
CONCATENATEX(
ResultTable,
[FormattedPercentage] & " in " & [City],
", ",
[NumericPercentage], DESC
)RETURN
ResultString
If this answers your questions, kindly accept it as a solution and give kudos.
6 Replies
- mdaatifraza5556
Super User
Hi Anonymous
Can you please try the below steps.
1. First, create a table to select the top values
By entering manually ( enter data option and enter a value for how much you want in top)
2. Use below DAX to find the top N values
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)VAR ResultTable =ADDCOLUMNS(TopCitiesTable,"Percentage", FORMAT(DIVIDE([TotalFAN], TotalFAN), "0.0%"))VAR ResultString =CONCATENATEX(ResultTable,[Percentage] & " in " & [City],", ")RETURNResultString3. use as a silcer the table you created for select top N values.
If this answers your questions, kindly accept it as a solution and give kudos.- AnonymousNot applicable
This is very helpful! thank you.
How do I order the answers in descending order of the %s and in separate lines instead of commas in the result set? - AnonymousNot applicable
This is very helpful! thank you.
How do I order the answers in descending order of the %s and in separate lines instead of commas in the result set? mdaatifraza5556- mdaatifraza5556
Super User
Hi Anonymous
Can you please try the below DAX?I have modified only in (VAR ResultTable and VAR ResultString)
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)
VAR ResultTable =
ADDCOLUMNS(
TopCitiesTable,
"NumericPercentage", DIVIDE([TotalFAN], TotalFAN),
"FormattedPercentage", FORMAT(DIVIDE([TotalFAN], TotalFAN), "0.0%")
)
VAR ResultString =
CONCATENATEX(
ResultTable,
[FormattedPercentage] & " in " & [City],
", ",
[NumericPercentage], DESC
)RETURN
ResultString
If this answers your questions, kindly accept it as a solution and give kudos.