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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
stanwang
Frequent Visitor

Matrix to calculate difference from previous record both gap and percentage

I am new to BI. How to get different from previous record both value and percentage.

For example my table as below

ProductRevenueRptDate
A1001/1/2024
A2003/7/2024
A1507/8/2024
B3001/1/2024
B4203/7/2024
B2507/8/2024
C3001/1/2024
C2503/7/2024
C6007/8/2024

The output visual is as below,

sample output.jpg

I can do it in excel, but how to achieve it in BI. Thanks.

3 ACCEPTED SOLUTIONS
Samarth_18
Community Champion
Community Champion

Hi @stanwang ,

 

You can create a custom column as below:-

Delta =
VAR _prev =
    CALCULATE (
        SUM ( 'Table (4)'[Revenue] ),
        FILTER (
            ( 'Table (4)' ),
            'Table (4)'[RptDate] < EARLIER ( [RptDate] )
                && 'Table (4)'[Product] = EARLIER ( 'Table (4)'[Product] )
        )
    )
RETURN
    IF ( _prev <> BLANK (), [Revenue] - _prev )

and use this column in Matrix as below:-

Samarth_18_0-1721461987733.png

 

 

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

View solution in original post

Hi @stanwang 

1. Dynamic Solution:

My solution stands out as the only dynamic approach among those presented here. Adding a column renders other solutions static, leading to unexpected and inaccurate results when filters or slicers are applied. My solution utilizes a measure, ensuring its dynamic nature and seamless operation even when interactions are added to the report. Please note that you have marked an incorrect solution as the correct one. It displays an erroneous result. (I have highlighted the incorrect solution you marked in red in the attached image.)

Ritaf1983_0-1721475665148.png

More information about the differences between the measure and calculated column is here :

https://www.sqlbi.com/articles/calculated-columns-and-measures-in-dax/

 

 

2. Total Row Error:

I acknowledge my oversight regarding the total row. It's essential to remember that unlike Excel, the total row in Power BI doesn't simply sum the values above it within the visualization. Instead, it performs the same calculation applied to each category but without their context. Therefore, errors can arise, as happened in my case, if proper analysis is not conducted.
I corrected the measure of the previous value :

PreviousVisibleRevenue =
VAR CurrentProduct = MAX('Table'[Product])
VAR CurrentDate = MAX('Table'[RptDate])
VAR PreviousDate =
    CALCULATE(
        MAX('Table'[RptDate]),
        FILTER(
            ALLSELECTED('Table'),
            'Table'[Product] = CurrentProduct && 'Table'[RptDate] < CurrentDate
        )
    )
RETURN
  if(HASONEVALUE('Table'[Product]),
    CALCULATE(
      [Revenue_],
        FILTER(
           ALLSELECTED('Table'),
            'Table'[Product] = CurrentProduct && 'Table'[RptDate] = PreviousDate
        )
    ),
    CALCULATE(
      [Revenue_],
        FILTER(
           ALLSELECTED('Table'),
            'Table'[RptDate] = PreviousDate
        )))
So now the results are correct including the total  (sorry for the mistake 🙂 )
Ritaf1983_1-1721475947420.png

 

 More information about fixing totals is here:
The updated PBIX is attached

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

Regards,
Rita Fainshtein | Microsoft MVP
https://www.linkedin.com/in/rita-fainshtein/
Blog : https://www.madeiradata.com/profile/ritaf/profile

View solution in original post

Ashish_Mathur
Super User
Super User

Hi,

PBI file attached.

Hope this helps.

Ashish_Mathur_0-1721518958629.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

11 REPLIES 11
Ashish_Mathur
Super User
Super User

Hi,

PBI file attached.

Hope this helps.

Ashish_Mathur_0-1721518958629.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

@Ashish_Mathur It is awesome. This is the best solution, easy to understand and implement. Thank you.

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Ritaf1983
Super User
Super User

Hi @stanwang 

In the first step, I recommend adding an index column by-product, using linked method

https://radacad.com/create-row-number-for-each-group-in-power-bi-using-power-query

The result will look like:

Ritaf1983_0-1721464377927.png

Then you can create these measures:

Revenue_ = sum('Table'[Revenue])
 
PreviousVisibleRevenue =
VAR CurrentProduct = MAX('Table'[Product])
VAR CurrentDate = MAX('Table'[RptDate])
VAR PreviousDate =
    CALCULATE(
        MAX('Table'[RptDate]),
        FILTER(
            ALLSELECTED('Table'),
            'Table'[Product] = CurrentProduct && 'Table'[RptDate] < CurrentDate
        )
    )
RETURN
    CALCULATE(
      [Revenue_],
        FILTER(
            ALL('Table'),
            'Table'[Product] = CurrentProduct && 'Table'[RptDate] = PreviousDate
        )
    )
 
delta_ = if(ISBLANK([PreviousVisibleRevenue]),"", [Revenue_]-[PreviousVisibleRevenue])
Result :
Ritaf1983_1-1721464501326.png

 

pbix is attached

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

 

Regards,
Rita Fainshtein | Microsoft MVP
https://www.linkedin.com/in/rita-fainshtein/
Blog : https://www.madeiradata.com/profile/ritaf/profile

@Ritaf1983 Your method is complicated, but it works fine for me. Just curious about highlighted in red. Why it is not 170?

stanwang_0-1721473428015.png

 

Hi @stanwang 

1. Dynamic Solution:

My solution stands out as the only dynamic approach among those presented here. Adding a column renders other solutions static, leading to unexpected and inaccurate results when filters or slicers are applied. My solution utilizes a measure, ensuring its dynamic nature and seamless operation even when interactions are added to the report. Please note that you have marked an incorrect solution as the correct one. It displays an erroneous result. (I have highlighted the incorrect solution you marked in red in the attached image.)

Ritaf1983_0-1721475665148.png

More information about the differences between the measure and calculated column is here :

https://www.sqlbi.com/articles/calculated-columns-and-measures-in-dax/

 

 

2. Total Row Error:

I acknowledge my oversight regarding the total row. It's essential to remember that unlike Excel, the total row in Power BI doesn't simply sum the values above it within the visualization. Instead, it performs the same calculation applied to each category but without their context. Therefore, errors can arise, as happened in my case, if proper analysis is not conducted.
I corrected the measure of the previous value :

PreviousVisibleRevenue =
VAR CurrentProduct = MAX('Table'[Product])
VAR CurrentDate = MAX('Table'[RptDate])
VAR PreviousDate =
    CALCULATE(
        MAX('Table'[RptDate]),
        FILTER(
            ALLSELECTED('Table'),
            'Table'[Product] = CurrentProduct && 'Table'[RptDate] < CurrentDate
        )
    )
RETURN
  if(HASONEVALUE('Table'[Product]),
    CALCULATE(
      [Revenue_],
        FILTER(
           ALLSELECTED('Table'),
            'Table'[Product] = CurrentProduct && 'Table'[RptDate] = PreviousDate
        )
    ),
    CALCULATE(
      [Revenue_],
        FILTER(
           ALLSELECTED('Table'),
            'Table'[RptDate] = PreviousDate
        )))
So now the results are correct including the total  (sorry for the mistake 🙂 )
Ritaf1983_1-1721475947420.png

 

 More information about fixing totals is here:
The updated PBIX is attached

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

Regards,
Rita Fainshtein | Microsoft MVP
https://www.linkedin.com/in/rita-fainshtein/
Blog : https://www.madeiradata.com/profile/ritaf/profile

@Ritaf1983 Thank you so much. It is perfect.

Happy to help 🙂

Regards,
Rita Fainshtein | Microsoft MVP
https://www.linkedin.com/in/rita-fainshtein/
Blog : https://www.madeiradata.com/profile/ritaf/profile
AllisonKennedy
Super User
Super User

@stanwang  you could also do this in Power Query using this method: 

 

Referencing the Next Row in Power Query • My Online Training Hub


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Samarth_18
Community Champion
Community Champion

Hi @stanwang ,

 

You can create a custom column as below:-

Delta =
VAR _prev =
    CALCULATE (
        SUM ( 'Table (4)'[Revenue] ),
        FILTER (
            ( 'Table (4)' ),
            'Table (4)'[RptDate] < EARLIER ( [RptDate] )
                && 'Table (4)'[Product] = EARLIER ( 'Table (4)'[Product] )
        )
    )
RETURN
    IF ( _prev <> BLANK (), [Revenue] - _prev )

and use this column in Matrix as below:-

Samarth_18_0-1721461987733.png

 

 

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

@Samarth_18Delta from 2nd to 1st is correct, but detal from 3rd to 2nd is not what I want.

Below is excel mockup.

stanwang_1-1721473559974.png

 

Helpful resources

Announcements
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.

June 2025 community update carousel

Fabric Community Update - June 2025

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