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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
al4x
Frequent Visitor

[DAX] - finding pairs of rows and subtracting from each other based on a key.

Tearing my hair out,  just cannot seem to understand DAX!

 

First my data is in long format in form

 

Catagory1, Catagory2, Catagory3,....Catagory#, YEAR, SCORE

 

Where all the 'Category' columns are character based, YEAR is INT column for example '2016' and SCORE is an INT column from 0-100

 

i want to create a measure that for any filter set for the catefory columns the SCORE year=2018 is subtracted from the year=2016

and further average the result

a unique set of 'Category' columns forms the primary key to define this pairing

 

NOTE, not all 2016 results have a 2018 result. also, there will be 2018 rows that do not have a 2016 result,  In this case, i would want to exclude these completely.

 

Effctively a subset looking like

 

AAA,BBB,2016,100

AAA,BBB,2018,50

AAA,CCC,2016,75

AAA,CCC,2018,100

BBB,CCC,2016,98

 

would produce a calc of

 

((50-100)+(100-75))/2

 

 

Because i come from an SQL backgroud, i can do this easily in SQL and just load a new dataset in, but

1..to help my understanding of DAX

and

2..to stop my pbi file becomeing large

i want to do it in DAX

 

 

What i want to do is subtract

1 ACCEPTED SOLUTION
Stachu
Community Champion
Community Champion

this will work

Measure =
VAR SummaryPerYear =
    ADDCOLUMNS (
        SUMMARIZE ( 'Table', 'Table'[Category1], 'Table'[Category2] ),
        "2016", CALCULATE ( SUM ( 'Table'[Score] ), 'Table'[Year] = 2016 ),
        "2018", CALCULATE ( SUM ( 'Table'[Score] ), 'Table'[Year] = 2018 )
    )
VAR ExcludeNull =
    FILTER ( SummaryPerYear, [2016] <> BLANK() && [2018] <> BLANK() )
RETURN
    AVERAGEX ( ExcludeNull, [2018] - [2016] )

and this is a good starting point for learning DAX
https://www.powerpivotpro.com/wp-content/uploads/2015/10/PowerPivotPro-Reference-Card.pdf

 

 



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

View solution in original post

3 REPLIES 3
v-chuncz-msft
Community Support
Community Support

@al4x,

 

By the way, you may also select Pivot Column in Query Editor first.

Community Support Team _ Sam Zha
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Stachu
Community Champion
Community Champion

this will work

Measure =
VAR SummaryPerYear =
    ADDCOLUMNS (
        SUMMARIZE ( 'Table', 'Table'[Category1], 'Table'[Category2] ),
        "2016", CALCULATE ( SUM ( 'Table'[Score] ), 'Table'[Year] = 2016 ),
        "2018", CALCULATE ( SUM ( 'Table'[Score] ), 'Table'[Year] = 2018 )
    )
VAR ExcludeNull =
    FILTER ( SummaryPerYear, [2016] <> BLANK() && [2018] <> BLANK() )
RETURN
    AVERAGEX ( ExcludeNull, [2018] - [2016] )

and this is a good starting point for learning DAX
https://www.powerpivotpro.com/wp-content/uploads/2015/10/PowerPivotPro-Reference-Card.pdf

 

 



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

al4x
Frequent Visitor

thanks for this.

 

It wasnt a simple answer for a beginner at least!

i will try to get my head round it and test it now. thank you!

 

if i understand this correctly it does look very similar to what i would do in SQL.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.

Top Solution Authors