Forum Discussion

Alvin2013's avatar
Alvin2013
Frequent Visitor
3 years ago
Solved

How to do a running total sorted by values

How to create a running total that follow the order of my chart?

This was done in Tableau. How to replicate it in PowerBI?

  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi Alvin2013 ,

     

    I suggest you to try code as below to create a measure.

    Running Total Sort by Values =
    VAR _SUMMARIZE =
        SUMMARIZE ( ALL ( 'Table' ), 'Table'[Category], "Sum", SUM ( 'Table'[Value] ) )
    VAR _ADDSORT =
        ADDCOLUMNS ( _SUMMARIZE, "Sort", RANKX ( _SUMMARIZE, [Sum],, DESC, SKIP ) )
    VAR _RUNNINGTOTAL =
        ADDCOLUMNS (
            _ADDSORT,
            "Running Total", SUMX ( FILTER ( _ADDSORT, [Sort] <= EARLIER ( [Sort] ) ), [Sum] )
        )
    RETURN
        SUMX (
            FILTER ( _RUNNINGTOTAL, [Category] = MAX ( 'Table'[Category] ) ),
            [Running Total]
        )
    

    Result is as below.

     

    Best Regards,
    Rico Zhou

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

2 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Alvin2013 ,

     

    I suggest you to try code as below to create a measure.

    Running Total Sort by Values =
    VAR _SUMMARIZE =
        SUMMARIZE ( ALL ( 'Table' ), 'Table'[Category], "Sum", SUM ( 'Table'[Value] ) )
    VAR _ADDSORT =
        ADDCOLUMNS ( _SUMMARIZE, "Sort", RANKX ( _SUMMARIZE, [Sum],, DESC, SKIP ) )
    VAR _RUNNINGTOTAL =
        ADDCOLUMNS (
            _ADDSORT,
            "Running Total", SUMX ( FILTER ( _ADDSORT, [Sort] <= EARLIER ( [Sort] ) ), [Sum] )
        )
    RETURN
        SUMX (
            FILTER ( _RUNNINGTOTAL, [Category] = MAX ( 'Table'[Category] ) ),
            [Running Total]
        )
    

    Result is as below.

     

    Best Regards,
    Rico Zhou

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.