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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
UBComma
Helper III
Helper III

Calculating the SUM of a subset from a running total

@AlexisOlson Any thoughts on this one?

I am trying to write a measure that can be updated by parameter values. I had this working as a calculated table but I'm having trouble getting one of the measures to work. Essentially what I am trying to do is calculate the sum of a specific set of values in a running total. The set is defined by years (I need to go from the present year back 15 years in this case). Once I have that SUM I'm entering it into the PMT function.

 

This is the measure I'm trying to fix........

Sum of Annual Values for past 15 years =

VAR Rate = .0275

VAR Periods = 15

VAR PeriodSum =

    SUMX (

        CALCULATETABLE (

            ADDCOLUMNS (

                Years,

                "Running Total", [RT]

            ),

            Years[Year] <= SELECTEDVALUE ( Years[Year] ) &&

            Years[Year] > SELECTEDVALUE ( Years[Year] ) - Periods

        ),

        [Running Total]

    )

RETURN

    PMT ( Rate, Periods, - PeriodSum )

 

The above measure is placed in a visual, say a table, the first column is Years[Year], and then the measure would follow.

My Years table is this: Years = GENERATESERIES (2022 , 2022 + 50, 1 )

And the [RT] measure is a value that increments up each year.

 

When I run the measure I get the correct values for the first year, 2022, through 2037, and then (after 15years) it goes flat and returns the same value for the rest of the years. I'm guessing there's something wrong with my filter context of Years[Year] but I can't figure it out. If anyone can offer a suggestion, I will greatly appreciate it.

 

Graphically, this is what I'm trying t do:

UBComma_0-1651844584099.png

 

1 ACCEPTED SOLUTION

6 REPLIES 6
UBComma
Helper III
Helper III

Absolutely GENIUS 😊. thank you so much for helping me with this one. It really had me stumped.

AlexisOlson
Super User
Super User

The SELECTEVALUE inside the CALCULATETABLE seems strange to me.

 

I'd try refactoring a bit:

Sum of Annual Values for past 15 years =
VAR Rate = .0275
VAR Periods = 15
VAR CurrYear = SELECTEDVALUE ( Years[Year] )
VAR YearRange =
    FILTER (
        ALL ( Years ),
        Years[Year] <= CurrYear &&
        Years[Year] > CurrYear - Periods
    )
VAR PeriodSum =
    SUMX (
        ADDCOLUMNS ( YearRange, "Running Total", [RT] ),
        [Running Total]
    )
RETURN
    PMT ( Rate, Periods, - PeriodSum )

I'm not sure this will do exactly what you want, but IMO, it's easier to debug if it doesn't.

Thank you, I'll rewrite it in the fashion you suggested and try some additional debugging.

tamerj1
Super User
Super User

Hi @UBComma 

can you share a screenshot of your table visual including both measures [RT] and [Sum of Annual Values for past 15 years]. What is the code for [RT]?

The code above was a sample. I sent the actual code in a private message to you

@UBComma 

701FBD06-DB7E-4507-AB91-9458DFD1BA00.png

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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