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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Difference between rows by grouping with multiple columns

Hi All,

I am quite new to Power BI.

I have a table where I have to calculate the difference between previous yr and current yr value by grouping data by different attributes. 

YearUnivSchoolClassValueDifference
2020ABCsAcX10-30
2019ABCsAcX4010
2018ABCsAcX30null
2018XYZsBcY40-40
2017XYZsBcY80null
2016XYZsCcZ10null
2017XYZsCcZ30null
2017ABCsDcZ70null

 

The difference column should be calculated for each yr by grouping the value by Univ, School and Class. 

I basically want to replicate the sql query in DAX :

 

SELECT Year,Univ, School,Class, Value,
Value - LAG(Value) OVER (PARTITION BY Univ, School,Class ORDER BY Year )
AS Difference FROM TableA

 

Note : I am using DirectQuery to the Database.

I want to calculate the difference column.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous 

First , you need create a measure to return the previous value .

 

PreviousValue = CALCULATE(SELECTEDVALUE('Table'[Value]),FILTER(ALLEXCEPT('Table','Table'[Univ],'Table'[School]),'Table'[Year]=SELECTEDVALUE('Table'[Year])-1))

 

Then create a measure to count the diff between previous year and current year .

 

Diff = IF([PreviousValue] <> BLANK(),SELECTEDVALUE('Table'[Value])-[PreviousValue],BLANK())

 

Finally, put these two measures in your table visual , you will get a result like below .

Ailsamsft_0-1649054693162.png

Best Regards,
Community Support Team _ Ailsa Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Hi @Anonymous 

First , you need create a measure to return the previous value .

 

PreviousValue = CALCULATE(SELECTEDVALUE('Table'[Value]),FILTER(ALLEXCEPT('Table','Table'[Univ],'Table'[School]),'Table'[Year]=SELECTEDVALUE('Table'[Year])-1))

 

Then create a measure to count the diff between previous year and current year .

 

Diff = IF([PreviousValue] <> BLANK(),SELECTEDVALUE('Table'[Value])-[PreviousValue],BLANK())

 

Finally, put these two measures in your table visual , you will get a result like below .

Ailsamsft_0-1649054693162.png

Best Regards,
Community Support Team _ Ailsa Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

tamerj1
Super User
Super User

Hi @Anonymous 

when using power bi visuals, aggregation happens automatically. The only thing which is required in your case is to move the year column filter one year back. 

Value =
SUM ( TableA[Value] )
Value Previous Year =
VAR CurrentYear =
    MAX ( TableA[Year] )
RETURN
    CALCULATE ( [Value], TableA[Year] = CurrentYear - 1 )
Difference =
[Value] - [Value Previous Year]



 

Anonymous
Not applicable

Hi @tamerj1 ,

Thanks for the response.

I tried the solution you proposed but it isn't working with the direct query model.

 

Function 'CALCULATE' is not allowed as part of calculated column DAX expressions on DirectQuery models.

Hi @Anonymous 

Please create as new measures not calculated columns. 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.