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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
riffraff55
Regular Visitor

Calculate percentage difference in year on year values

Newbiw here to Power BI and DAX. I am looking to calculate the percentage difference in year on year values and then writing the derived value in a new column.Ive written the following DAX formula but it keeps saying theres a syntax error so not sure what I am doing wrong. I would be grateful if anyone can point me in the right direction:

 

PercentageChange =
VAR PrevYrGrowth =
    CALCULATE(
        MAX('PProj'[P Growth]),
            'PProj'[CName] = EARLIER('PProj'[CName]) &&
            'PProj'[Year] = EARLIER('PProj'[Year]) - 1
        )
RETURN
IF(
    NOT(ISBLANK(PrevYrGrowth)),
    (('PProj'[P Growth] - PrevYrGrowth) / PrevYrGrowth) * 100,
    BLANK()
)

 

 
The CName column is a text coloumn representing a country name. the Year column is a number column representing a Year in the format yyyy
The P Growth column is a decimal colum rounded to 2 decimal places
1 ACCEPTED SOLUTION
hnguy71
Super User
Super User

Hi @riffraff55 

I would recommend building it as a measure instead of a calculated column, but if you still insist it being a column this should work:

PercentageChange = 

VAR _Year = [Year]

VAR _CY = 
CALCULATE(
    SUM(PProj[P Growth]), 
    FILTER(ALL(PProj), [CName] = EARLIER([CName]) && [Year] = _Year)
)


VAR _PY = 
CALCULATE(
    SUM(PProj[P Growth]), 
    FILTER(ALL(PProj), [CName] = EARLIER([CName]) && [Year] = _Year - 1)
) 

RETURN

DIVIDE(_CY - _PY, _PY) * 100


Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

View solution in original post

3 REPLIES 3
riffraff55
Regular Visitor

just for my learning exprience, what would be the difference in creating it as a measure rather than as a column? is it just good practice or is the coding completely different?

Hi @riffraff55 ,

They're both DAX specific expressions and each of them have their advantages. Having it as a measure gives you more flexibility and can give you results based on visual level context at the aggregate level and does not take up memory, thus letting your model refresh faster.

 

But if you want to do specific row level calculations, then calculated column(s) may be better suited, but do note that on reresh, these columns are calculated which may slow down your model refresh.

 

 



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!
hnguy71
Super User
Super User

Hi @riffraff55 

I would recommend building it as a measure instead of a calculated column, but if you still insist it being a column this should work:

PercentageChange = 

VAR _Year = [Year]

VAR _CY = 
CALCULATE(
    SUM(PProj[P Growth]), 
    FILTER(ALL(PProj), [CName] = EARLIER([CName]) && [Year] = _Year)
)


VAR _PY = 
CALCULATE(
    SUM(PProj[P Growth]), 
    FILTER(ALL(PProj), [CName] = EARLIER([CName]) && [Year] = _Year - 1)
) 

RETURN

DIVIDE(_CY - _PY, _PY) * 100


Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 NL Carousel

Fabric Community Update - February 2025

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