Forum Discussion

gigaenvy's avatar
gigaenvy
Frequent Visitor
2 years ago
Solved

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 ...
  • Anonymous's avatar
    Anonymous
    2 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 Team

     

    If 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!