Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
aesalasaguero
Frequent Visitor

Running total in Dax Studio

Hi There, Could anybody held with this Dax Query. I Group Sales by month and created a raking, as following:

DEFINE
VAR Sales =
ADDCOLUMNS (
VALUES ( 'DIM PRODUCTO'[PRODUCTO]),
"@SalesMonth", CALCULATE([VENTAS EN UNIDADES], 'DIM TIEMPO'[AÑO-MES]="2024-03")
)
EVALUATE
ADDCOLUMNS (
Sales,
"Rank",
RANKX (
Sales,
[@SalesMonth]
)

)
ORDER BY [@SalesMonth] DESC
This is the result:

Result.png

Is there any way to do a running total based on this result?

Thanks

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @aesalasaguero 

I noticed an error in the original query which I have corrected in my earlier post (a reference to SalesTable that should have been Sales).

 

In order to break ties as was suggested, one method for :

 

DEFINE
    VAR Sales =
        ADDCOLUMNS (
            VALUES ( 'DIM PRODUCTO'[PRODUCTO] ),
            "@SalesMonth",
                CALCULATE (
                    [VENTAS EN UNIDADES],
                    'DIM TIEMPO'[AÑO-MES] = "2024-03"
                )
        )

EVALUATE
ADDCOLUMNS (
    Sales,
    "Rank",
        RANKX (
            Sales,
            [@SalesMonth]
        ),
    "RunningTotal",
        SUMX (
            WINDOW (
                1,
                ABS,
                0,
                REL,
                Sales,
                ORDERBY ( [@SalesMonth], DESC, 'DIM PRODUCTO'[PRODUCTO], ASC )
            ),
            [@SalesMonth]
        )
)
ORDER BY [@SalesMonth] DESC

 

 

If your DAX version doesn't have the WINDOW function, you could use:

 

DEFINE
    VAR Sales =
        ADDCOLUMNS (
            VALUES ( 'DIM PRODUCTO'[PRODUCTO] ),
            "@SalesMonth",
                CALCULATE (
                    [VENTAS EN UNIDADES],
                    'DIM TIEMPO'[AÑO-MES] = "2024-03"
                )
        )

EVALUATE
ADDCOLUMNS (
    Sales,
    "Rank",
        RANKX (
            Sales,
            [@SalesMonth]
        ),
    "RunningTotal",
    	VAR CurrentSalesMonth = [@SalesMonth]
    	VAR CurrentProduct = 'DIM PRODUCTO'[PRODUCTO]
    	RETURN
	        SUMX (
	            FILTER (
					Sales,
					[@SalesMonth] > CurrentSalesMonth
					|| [@SalesMonth] = CurrentSalesMonth && 'DIM PRODUCTO'[PRODUCTO] <= CurrentProduct
					),
	            	[@SalesMonth]
	        )
)
ORDER BY [@SalesMonth] DESC

 

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

View solution in original post

7 REPLIES 7
OwenAuger
Super User
Super User

Hi @aesalasaguero 

I noticed an error in the original query which I have corrected in my earlier post (a reference to SalesTable that should have been Sales).

 

In order to break ties as was suggested, one method for :

 

DEFINE
    VAR Sales =
        ADDCOLUMNS (
            VALUES ( 'DIM PRODUCTO'[PRODUCTO] ),
            "@SalesMonth",
                CALCULATE (
                    [VENTAS EN UNIDADES],
                    'DIM TIEMPO'[AÑO-MES] = "2024-03"
                )
        )

EVALUATE
ADDCOLUMNS (
    Sales,
    "Rank",
        RANKX (
            Sales,
            [@SalesMonth]
        ),
    "RunningTotal",
        SUMX (
            WINDOW (
                1,
                ABS,
                0,
                REL,
                Sales,
                ORDERBY ( [@SalesMonth], DESC, 'DIM PRODUCTO'[PRODUCTO], ASC )
            ),
            [@SalesMonth]
        )
)
ORDER BY [@SalesMonth] DESC

 

 

If your DAX version doesn't have the WINDOW function, you could use:

 

DEFINE
    VAR Sales =
        ADDCOLUMNS (
            VALUES ( 'DIM PRODUCTO'[PRODUCTO] ),
            "@SalesMonth",
                CALCULATE (
                    [VENTAS EN UNIDADES],
                    'DIM TIEMPO'[AÑO-MES] = "2024-03"
                )
        )

EVALUATE
ADDCOLUMNS (
    Sales,
    "Rank",
        RANKX (
            Sales,
            [@SalesMonth]
        ),
    "RunningTotal",
    	VAR CurrentSalesMonth = [@SalesMonth]
    	VAR CurrentProduct = 'DIM PRODUCTO'[PRODUCTO]
    	RETURN
	        SUMX (
	            FILTER (
					Sales,
					[@SalesMonth] > CurrentSalesMonth
					|| [@SalesMonth] = CurrentSalesMonth && 'DIM PRODUCTO'[PRODUCTO] <= CurrentProduct
					),
	            	[@SalesMonth]
	        )
)
ORDER BY [@SalesMonth] DESC

 

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Hi Owen, 

The second solution gets error timeout, but I deleted the line: 

|| [@SalesMonth] = CurrentSalesMonth && 'DIM PRODUCTO'[PRODUCTO] <= CurrentProduct

 and It worked perfectly.

 

Thank you very much.

 

Just for general knowledge. the WINDOWS function is not included in my DAX, is it a possible solution to update my SSAS version to a newer one?

Glad to hear it 🙂

I don't have specific experience on upgrading SSAS Tabular, but see here:

https://learn.microsoft.com/en-us/sql/database-engine/install-windows/upgrade-analysis-services?view...

 

It appears that you need a recent build of SSAS 2022 for WINDOW and related functions.

https://dax.guide/WINDOW/

 

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn
lbendlin
Super User
Super User

The standard pattern is to sum up all values that are greater than or equal to the current value (like a Pareto).  For ties you need to find a suitable tie breaker, for example the product name.

Hi, I tried to do it, but I'm kind of lost on this. I added an extra column in ADDCOLUMS like this:

 "Sum",
            CALCULATE(
                sumx(
                    sales,
                    [@SalesMonth]
                ),
                FILTER(
                    ALLSELECTED(sales),
                    [@SalesMonth] >= MAX([@SalesMonth])
                )
            )

but how do I put the suitable tie breaker here?

 

Thanks

 

OwenAuger
Super User
Super User

Hi @aesalasaguero 

If you want to add a running total to the table returned, you could use the WINDOW function. For example:

 

DEFINE
    VAR Sales =
        ADDCOLUMNS (
            VALUES ( 'DIM PRODUCTO'[PRODUCTO] ),
            "@SalesMonth",
                CALCULATE (
                    [VENTAS EN UNIDADES],
                    'DIM TIEMPO'[AÑO-MES] = "2024-03"
                )
        )

EVALUATE
ADDCOLUMNS (
    Sales,
    "Rank",
        RANKX (
            Sales,
            [@SalesMonth]
        ),
    "RunningTotal",
        SUMX (
            WINDOW (
                1,
                ABS,
                0,
                REL,
                Sales,
                ORDERBY ( [@SalesMonth], DESC, 'DIM PRODUCTO'[PRODUCTO], ASC )
            ),
            [@SalesMonth]
        )
)
ORDER BY [@SalesMonth] DESC

 

 

Is this the sort of thing you were looking for?

 

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Hi Owen, I tried your approach, but I get an error message:

 

Failed to resolve the name 'ABS'. It is not a valid table, variable or function name.

 

what do you think could be the issue?

 

Thanks

 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.