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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

How to get difference between two columns in Matrix based on slicer selection of Year in Power Bi

Difference.PNG

1 ACCEPTED SOLUTION

Hi @Anonymous 
Here you go https://www.dropbox.com/t/sMyzwonHckNHvdKX
Original Measure code remains the same

Measure = COUNTROWS ( DISTINCT ('Table'[Value] ) )

New Measure code is

Measure1 = 
VAR MaxYear = MAX ( 'Table'[Year] )
VAR MinYear = MIN ( 'Table'[Year] )
VAR MaxYearValue =
    CALCULATE ( 
        [Measure],
        'Table'[Year] = MaxYear
    )
VAR MinYearValue =
    CALCULATE ( 
        [Measure],
        'Table'[Year] = MinYear
    )
VAR Difference = MaxYearValue - MinYearValue
VAR Result =
    SWITCH (
        TRUE,
        NOT HASONEVALUE ( 'Table'[Year] ), Difference,
        COUNTROWS ( ALLSELECTED ('Table'[Year] ) ) = 1, BLANK(),
        [Measure]
    )
RETURN
    Result 

 

View solution in original post

14 REPLIES 14
AAspa
Frequent Visitor

is there a way to add an additional column with the difference in percentage?

 

Many thanks

tamerj1
Super User
Super User

Hi @Anonymous 

Check out this sample file https://www.dropbox.com/t/34kVadAA9Yb40CUO
Actually, you cannot add a new measure to the matrix visual. If you do so you will see two values under each year. Therefore, the only choice is to create a new measure that is built on top of the original one, playing with the code in order to view the difference at the Total column like this 
Untitled.png

This is the code:     Note: [Measure] is your original measure

Measure1 = 
VAR MaxYear = MAX ( 'Table'[Year] )
VAR MinYear = MIN ( 'Table'[Year] )
VAR MaxYearValue =
    CALCULATE ( 
        [Measure],
        'Table'[Year] = MaxYear
    )
VAR MinYearValue =
    CALCULATE ( 
        [Measure],
        'Table'[Year] = MinYear
    )
VAR Difference = MaxYearValue - MinYearValue
VAR Result =
    SWITCH (
        TRUE,
        NOT HASONEVALUE ( 'Table'[Year] ), Difference,
        COUNTROWS ( ALLSELECTED ('Table'[Year] ) ) = 1, BLANK(),
        [Measure]
    )
RETURN
    Result 

 

Anonymous
Not applicable

Thanks For Your Solution .
One thing is the values column in your Pbix is numbers like 10 , 20  26 ,but  my requirement is,  values  column is codes like b010 , c350, d6f , ng6 . I categorized them as Distinct count . So they look like numbers in my attached file . I think the DAX is almost correct .  Can you modify dax by using my scenario .
Thanks For such great Help .

You can just refer your measure name instead of [Measure] and it should work. Otherwise, you can send your file with a sample data and I'll do it for you. 
If you are satisfied please mark my reply as accepted solution. Kudoes are also appreciated. 

Anonymous
Not applicable

community.PNG
 I replaced values with codes . My requirement is look like this . I categorized them as distinct count ( from column tools ) .What I want is exactly like your output  . Measure 1 is exactly corect . I think we need to modify the Measure , with codes (Distinct Count ) .Please modify the dax .,It would work fine. Am unable tho send the file .
Kudos !
Thanks for such quick response 

Hi @Anonymous 
Here you go https://www.dropbox.com/t/sMyzwonHckNHvdKX
Original Measure code remains the same

Measure = COUNTROWS ( DISTINCT ('Table'[Value] ) )

New Measure code is

Measure1 = 
VAR MaxYear = MAX ( 'Table'[Year] )
VAR MinYear = MIN ( 'Table'[Year] )
VAR MaxYearValue =
    CALCULATE ( 
        [Measure],
        'Table'[Year] = MaxYear
    )
VAR MinYearValue =
    CALCULATE ( 
        [Measure],
        'Table'[Year] = MinYear
    )
VAR Difference = MaxYearValue - MinYearValue
VAR Result =
    SWITCH (
        TRUE,
        NOT HASONEVALUE ( 'Table'[Year] ), Difference,
        COUNTROWS ( ALLSELECTED ('Table'[Year] ) ) = 1, BLANK(),
        [Measure]
    )
RETURN
    Result 

 

Anonymous
Not applicable

Thank You So Much Tamerj1 .
You deserve to be  a super user .
Kudos ! Kudos !

Anonymous
Not applicable

Sample solution as per your requirement.

Aditya_Meshram_1-1644422414529.pngAditya_Meshram_2-1644422436093.png

Aditya_Meshram_4-1644422507125.pngAditya_Meshram_5-1644422528153.png

 

Attached .pbix file.

https://nicesoftwaresolutions1-my.sharepoint.com/:u:/g/personal/ameshram_nicesoftwaresolutions_com/E...

 

 

Regards,

Aditya

 

Anonymous
Not applicable

Sample solution as per your requirement.

Aditya_Meshram_1-1644422414529.pngAditya_Meshram_2-1644422436093.png

Aditya_Meshram_4-1644422507125.pngAditya_Meshram_5-1644422528153.png

 

Attached .pbix file.

https://nicesoftwaresolutions1-my.sharepoint.com/:u:/g/personal/ameshram_nicesoftwaresolutions_com/E...

 

 

Regards,

Aditya

 

nickyvv
Most Valuable Professional
Most Valuable Professional

@Anonymous  @ibarrau thread moved to DAX forum



Did I answer your question? Mark my post as a solution!

Blog: nickyvv.com | @NickyvV


ibarrau
Super User
Super User

Hi. I think this is a question for the DAX forum. Anyway. In this case, you have years. That might help you to create a math subsctracting current selection and last year. You can do a measure like this:

 

Measure = 
_current = CALCULATE ( SUM(Table[Value]) )
_last_year = CALCULATE ( SUM(Table[Value]) , SAMEPERIODLASTYEAR(Calendar[Date]) )
RETURN
_current - _last_year

 

In order to make that calculation you need a calendar or date table to help you get time intelligence calculations. That would be the best practice, but if you don't like the idea you can use SELECTEDVALUE to get the current year and just do -1 in the _last_year filter context instead of SAMEPERIODLASTYEAR. That way when you filter a year you can get current - last difference. If you don't filter it will try to render differences with last year for each year in the matrix. The measure won't make sense if you add it in a visual without a period of time.

I hope that helps,


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Happy to help!

LaDataWeb Blog

Anonymous
Not applicable

Thanks for your response Ibbarau ,
 My values are actually codes ( like m124 , b016, cd56 ) . I categorized them as Distinct count . so when I use  SUM  the DAX is not working.
If you need  any info. Please ask .
Thanks

 

Ok. If the numbers in the fields are distinct count of categories you can replace the SUM of the measure I have sent with DISTINCTCOUNT function in DAX.

I hope that work


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Happy to help!

LaDataWeb Blog

Anonymous
Not applicable

Thanks for your solution 
I also wrote one dax it worked out

Measure =
VAR X = CALCULATE(DISTINCTCOUNT(table[ Code]),FILTER(table,table[Year]=MIN(table[Year])))
var y =CALCULATE(DISTINCTCOUNT(table[Code]),FILTER(table , table[Year]=MAX(table[Year])))
Return
y-X
but the same dax is not working for calculated column. Why . Can you explain
 can you write suitable Dax for calculated column .

Thank you so much .

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.