Forum Discussion
Top N for Many Columns in Table
Hi,
I am very new to DAX. Actually, 1st timer and have realized I need to do something with DAX code to get Top N (10) results based on a multi column table with different choices being imported from SharePoint.
What I want is to create a Top N (10) Table Visual for 'FAILED' values. The Patient Name can be a filter as they may have visited many times prior, but so can other slicers such as City, State, and other personal characteristics if I want to see a larger group of people in a demographic.
It should just be a simple Top N displaying in visual Highest to Lowest and no other information other than the column names as shown below.
I asked Copilot to help, but I did't try as it seems there could be a more streamlined way. Is the below correct before I rinse and repeat for 21 different columns in the table?
1. Repeat this for each column you want to include in the visual.
Failed_LCD1 = CALCULATE(COUNTROWS(PatientCheck_InspAnswers), PatientCheck_InspAnswers[HeartO] = "Failed")
2. Create a Measure for Top 10 Failed.
Top_10_Failed =
VAR TopNValue = 10
VAR FailedTable = UNION(
SELECTCOLUMNS(FILTER(PatientCheck_InspAnswers, PatientCheck_InspAnswers[HeartO] = "Failed"), "Item", "HeartO", "Count", [Failed_HeartO),
SELECTCOLUMNS(FILTER(PatientCheck_InspAnswers, Patientheck_InspAnswers[HeartL] = "Failed"), "Item", "HeartL", "Count", [Failed_HeartL]),
// ... repeat for each column ...
)
VAR RankedTable = ADDCOLUMNS(FailedTable, "Rank", RANKX(FailedTable, [Count],, DESC, Dense))
RETURN FILTER(RankedTable, [Rank] <= TopNValue)
Thanks,
- Anonymous2 years ago
Hi gigaenvy ,
Your first syntax is logically correct, which is great!
Your second syntax is a little wrong, here is the revised one:
Top_10_Failed = VAR TopNValue = 10 VAR FailedTable = UNION( SELECTCOLUMNS(FILTER(PatientCheck_InspAnswers, PatientCheck_InspAnswers[HeartO] = "Failed"), "Item", "HeartO", "Count", [Failed_LCD1]), SELECTCOLUMNS(FILTER(PatientCheck_InspAnswers, PatientCheck_InspAnswers[HeartL] = "Failed"), "Item", "HeartL", "Count", [Failed_LCD2]) // Repeat for each measure/column... ) VAR RankedTable = ADDCOLUMNS(FailedTable, "Rank", RANKX(ALL(FailedTable), [Count], , DESC, Dense)) RETURN FILTER(RankedTable, [Rank] <= TopNValue)Please change the underlined [Failed_HeartO) to [Failed_LCD1], and so on, change [Failed_HeartL] to [Failed_LCD2]...
Please add the ALL function to the underlined FailedTable to prevent it from possible context filtering effects.
Please try the modified syntax. If it does not work, I would be grateful if you could provide me with the pbix file or sample data.
Remember to remove sensitive data and do not log in to your account in Power BI Desktop when uploading the pbix file.
If you have any other questions please feel free to contact me.
Best Regards,
Yang
Community Support TeamIf there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!
2 Replies
- AnonymousNot applicable
Hi gigaenvy ,
Your first syntax is logically correct, which is great!
Your second syntax is a little wrong, here is the revised one:
Top_10_Failed = VAR TopNValue = 10 VAR FailedTable = UNION( SELECTCOLUMNS(FILTER(PatientCheck_InspAnswers, PatientCheck_InspAnswers[HeartO] = "Failed"), "Item", "HeartO", "Count", [Failed_LCD1]), SELECTCOLUMNS(FILTER(PatientCheck_InspAnswers, PatientCheck_InspAnswers[HeartL] = "Failed"), "Item", "HeartL", "Count", [Failed_LCD2]) // Repeat for each measure/column... ) VAR RankedTable = ADDCOLUMNS(FailedTable, "Rank", RANKX(ALL(FailedTable), [Count], , DESC, Dense)) RETURN FILTER(RankedTable, [Rank] <= TopNValue)Please change the underlined [Failed_HeartO) to [Failed_LCD1], and so on, change [Failed_HeartL] to [Failed_LCD2]...
Please add the ALL function to the underlined FailedTable to prevent it from possible context filtering effects.
Please try the modified syntax. If it does not work, I would be grateful if you could provide me with the pbix file or sample data.
Remember to remove sensitive data and do not log in to your account in Power BI Desktop when uploading the pbix file.
If you have any other questions please feel free to contact me.
Best Regards,
Yang
Community Support TeamIf there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!- gigaenvyFrequent Visitor
Thank you for the reply and follow up. Give me some time to play with before replying or Accepting the solution. Nag me next week if I forget 🙂