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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

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
Memorable Member
Memorable Member

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
Memorable Member
Memorable Member

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.