Forum Discussion

smpa01's avatar
smpa01
Community Champion
4 years ago
Solved

DAX Table expression to return TOPN by partition

AlexisOlson  I might have brought this one up in a different post with you but never really had a chance to properly ask the question. This post might get long with follow-up questions My goal is t...
  • AlexisOlson's avatar
    4 years ago

    How about this?

    GENERATE (
        VALUES ( tbl[CAT] ),
        CALCULATETABLE (
            TOPN (
                3,
                ADDCOLUMNS (
                    SUMMARIZE ( tbl, tbl[subCAT] ),
                    "@Sum", CALCULATE ( SUM ( tbl[Value] ) )
                ),
                [@Sum]
            )
        )
    )

    Or not best practice but shorter,

    GENERATE (
        VALUES ( tbl[CAT] ),
        CALCULATETABLE (
            TOPN (
                3,
                SUMMARIZE ( tbl, tbl[subCAT], "@Sum", SUM ( tbl[Value] ) ),
                [@Sum]
             )
        )
    )