cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
smpa01
Super User
Super User

Running total Issue

I am trying to create a running total using WINDOW function and I am not sure if I am overcomplicating but this is the only I can get it to work.

//my base data is this

| Year | Val |
|------|-----|
| 2020 | 100 |
| 2021 | 200 |
| 2022 | 300 |
| 2023 | 400 |

//I need a calculated table with running total
| Year | Val | rnt  |
|------|-----|------|
| 2020 | 100 | 100  |
| 2021 | 200 | 300  |
| 2022 | 300 | 600  |
| 2023 | 400 | 1000 |

 

I am writing a table expression like this which is doing the job. The rank is calculated to determine the correct TO parameter of WINDOW. Is there an easier way?

 

Table = 
ADDCOLUMNS (
    tbl,
    "rnt",
        VAR toVal =
            RANKX (
                WINDOW ( 1, ABS, -1, ABS, SUMMARIZE ( tbl, tbl[Year] ), ORDERBY ( tbl[Year] ) ),
                tbl[Year],
                ,
                DESC
            )
        VAR rnt =
            CALCULATE (
                SUM ( tbl[Val] ),
                ALL ( tbl ),
                WINDOW ( 1, ABS, toVal * -1, ABS, , ORDERBY ( tbl[Year], ASC ) )
            )
        RETURN
            rnt
)

 

@AlexisOlson @CNENFRNL @jeffrey_wang 

 

Thank you in advance





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!






New Animated Dashboard: Sales Calendar


1 ACCEPTED SOLUTION
jeffrey_wang
Power BI Team
Power BI Team

Here is a simpler solution

 

Table2 = ADDCOLUMNS(tbl, "rnt", CALCULATE(SUM('tbl'[Val]), WINDOW(1, ABS, 0, REL, ALL(tbl[Year])), ALL(tbl)))
 
The key is to use ALL(tbl) to remove the filter on the [Val] column that's converted from row context to filter context by CALCULATE.

View solution in original post

1 REPLY 1
jeffrey_wang
Power BI Team
Power BI Team

Here is a simpler solution

 

Table2 = ADDCOLUMNS(tbl, "rnt", CALCULATE(SUM('tbl'[Val]), WINDOW(1, ABS, 0, REL, ALL(tbl[Year])), ALL(tbl)))
 
The key is to use ALL(tbl) to remove the filter on the [Val] column that's converted from row context to filter context by CALCULATE.

Helpful resources

Announcements
Carousel_PBI_Wave1

2023 Release Wave 1 Plans

Power BI release plans for 2023 release wave 1 describes all new features releasing from April 2023 through September 2023.

Power BI Summit Carousel 2

Global Power BI Training

Make sure you register today for the Power BI Summit 2023. Don't miss all of the great sessions and speakers!

BizApps LATAM 2023

Business Application LATAM Summit 2023

Join the biggest FREE Business Applications Event in LATAM this February.

Power Platform Bootcamp

Global Power Platform Bootcamp

In this bootcamp we will deep-dive into Microsoft’s Power Platform stack with hands-on sessions and labs, delivered to you by experts and community leaders.