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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
withnoe
Frequent Visitor

DAX calculation for sum of values by catgory divided by previous value and two previous values

I am new to BI and am trying to figure out a DAX formula to compute a precentage change.  I have searched quite a bit for an a way to do this but all I come up with is ways to do it by dates or by static values.  The term field will change as time goes on.  The query will return the last 3 terms.  Below is an example table of the data.

 

termnameHours
202241 Joe3
202241 Sally5
202241 Fred4
202341 Suzie3
202341 John6
202341 George4
202441 Kile5
202441 Brenda8
202441 Kim5

 

 

 

What I would like get a measure that will give the percentage change from the sum of the hours per term compared from the first term to the second and then from the first to the third.  For example sum of the hours for term 202241 = 12 , 202341 = 13, 202441 = 18.  The terms will always be 100 away from the next one.  For example 202441 - 202341 = 100.  The results that I am looking for are:

 

 

1 YR Percentage change from 202241 to 202341 = 12/13     8% change

2 YR Percentage change from 202241 to 202441 = 12/18     34% change

 

 

1 ACCEPTED SOLUTION
PhilipTreacy
Super User
Super User

Hi @withnoe 

 

Download PBIX file with examples below

 

OK, that doesn't complicate things much actually, you can just use a slicer to get the results. 

 

I assume that is ok as you haven't provided exactly what you want as a result.  And if you have lots of colleges I'm assuming you don't want loads of card visuals for each college?  You can just select the college in the slicer:

 

PhilipTreacy_0-1728349531159.png

 

PhilipTreacy_1-1728349549148.png

 

 

PhilipTreacy_2-1728349572923.png

 

 

 

The measures for each change are

 

1 Year Change = ([Sum Hours Previous Term] - [Sum Hours 2 Terms Ago]) / [Sum Hours 2 Terms Ago]
2 Year Change = ([Sum Hours Latest Term] - [Sum Hours 2 Terms Ago]) / [Sum Hours 2 Terms Ago]

 

and to calculate the sum of hours per term, same as before

 

Sum Hours Latest Term = CALCULATE(SUM('DataTable'[Hours]), 'DataTable'[term] = MAX('DataTable'[term]))
Sum Hours Previous Term = CALCULATE(SUM('DataTable'[Hours]), 'DataTable'[term] = MAX('DataTable'[term]) - 100)
Sum Hours 2 Terms Ago = CALCULATE(SUM('DataTable'[Hours]), 'DataTable'[term] = MAX('DataTable'[term]) - 200)

 

Regards

 

Phil

 

 

 



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


View solution in original post

6 REPLIES 6
PhilipTreacy
Super User
Super User

Hi @withnoe 

 

Download PBIX file with examples below

 

OK, that doesn't complicate things much actually, you can just use a slicer to get the results. 

 

I assume that is ok as you haven't provided exactly what you want as a result.  And if you have lots of colleges I'm assuming you don't want loads of card visuals for each college?  You can just select the college in the slicer:

 

PhilipTreacy_0-1728349531159.png

 

PhilipTreacy_1-1728349549148.png

 

 

PhilipTreacy_2-1728349572923.png

 

 

 

The measures for each change are

 

1 Year Change = ([Sum Hours Previous Term] - [Sum Hours 2 Terms Ago]) / [Sum Hours 2 Terms Ago]
2 Year Change = ([Sum Hours Latest Term] - [Sum Hours 2 Terms Ago]) / [Sum Hours 2 Terms Ago]

 

and to calculate the sum of hours per term, same as before

 

Sum Hours Latest Term = CALCULATE(SUM('DataTable'[Hours]), 'DataTable'[term] = MAX('DataTable'[term]))
Sum Hours Previous Term = CALCULATE(SUM('DataTable'[Hours]), 'DataTable'[term] = MAX('DataTable'[term]) - 100)
Sum Hours 2 Terms Ago = CALCULATE(SUM('DataTable'[Hours]), 'DataTable'[term] = MAX('DataTable'[term]) - 200)

 

Regards

 

Phil

 

 

 



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Kedar_Pande
Super User
Super User

@withnoe 

Create a Sum of Hours Measure

TotalHours = SUM('Table'[Hours])

Create a Percentage Change Measure:

PercentageChange =
VAR CurrentTerm = MAX('Table'[Term])
VAR PreviousTerm1 = CurrentTerm - 100
VAR PreviousTerm2 = CurrentTerm - 200
VAR HoursCurrent = CALCULATE([TotalHours], 'Table'[Term] = CurrentTerm)
VAR HoursPrev1 = CALCULATE([TotalHours], 'Table'[Term] = PreviousTerm1)
VAR HoursPrev2 = CALCULATE([TotalHours], 'Table'[Term] = PreviousTerm2)

RETURN
SWITCH (
TRUE(),
HoursPrev1 <> 0, (HoursCurrent - HoursPrev1) / HoursPrev1,
HoursPrev2 <> 0, (HoursCurrent - HoursPrev2) / HoursPrev2,
BLANK()
)

You can format the result as a percentage in the measure settings by selecting the percentage format in Power BI.

PhilipTreacy
Super User
Super User

Hi @withnoe 

 

Download PBIX file with example below

 

Create these measures

 

 

Sum Hours Latest Term = CALCULATE(SUM('DataTable'[Hours]), 'DataTable'[term] = MAX('DataTable'[term]))
Sum Hours Previous Term = CALCULATE(SUM('DataTable'[Hours]), 'DataTable'[term] = MAX('DataTable'[term]) - 100)
Sum Hours 2 Terms Ago = CALCULATE(SUM('DataTable'[Hours]), 'DataTable'[term] = MAX('DataTable'[term]) - 200)

 

 

The results you want are then calculated with these measures (Format both of these as %)

 

 

Change 1 = 1 - ([Sum Hours 2 Terms Ago] / [Sum Hours Previous Term])
Change 2 = 1- ([Sum Hours 2 Terms Ago] / [Sum Hours Latest Term])

 

 

 

PhilipTreacy_0-1728000391380.png

 

 

Regards

 

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Hi Philip,

 

This answers my question.  However, I forgot an important part in my simplified model.  I also have a column with a college code in it.  So the sum of the hours needs to be subgrouped by college. So it would be the sum of hours by term then by college.   The only way I could see it working is to create  sum of the latest hours by each college which would be a lot of measures.  However, the issue is that the number of colleges may change so I cannot have static values for those in a measure.

@withnoe 

 

Please supply some sample data to illustrate what you are describing.  Can't write code for tables/data I can't see!

 

Thanks

 

Phil

 



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Here is the table revision with the added college column:

 

 

termnamecollegeHours
202241 Joe123
202241 Sally125
202241 Fred144
202341 Suzie143
202341 John126
202341 George144
202441 Kile145
202441 Brenda128
202441 Kim12

5

 

 

Here is what the result of combining these per term per college would be:

 

termcollegesum of hours
202241 128
202241 144
202341 126
202341 149
202441 1212
202441 145

 

Does this help?

Helpful resources

Announcements
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.

May 2025 Monthly Update

Fabric Community Update - May 2025

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