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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Anonymous
Not applicable

Calculate Difference between two rows

Sample DataSample Data     Desired OutputDesired Output      

Hi,

I am trying to a add calculated column  that will return the difference of two rows in column: Value. by Date, Retailer and Attribute.

Sample data attached is for one Retailer and Attribute, In this case the output shouls look like the extreme right column(Desired Output)

I tried everything available on web without any success

 

1 ACCEPTED SOLUTION

Hi @Anonymous ,

If you want to calculate for each Retailer and Attribute, just add their fields as variables into the previous formula:

Anomoly =
VAR _date = [Date]
VAR _index = [Index]
VAR _re = [Retailer]
VAR _att = [Attribute]
VAR lastrow =
    CALCULATE (
        MAX ( 'Table'[Value] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Date] = _date
                && 'Table'[Retailer] = _re
                && 'Table'[Attribute] = _att
                && 'Table'[Index]
                    = CALCULATE (
                        MAX ( 'Table'[Index] ),
                        FILTER (
                            ALL ( 'Table' ),
                            'Table'[Date] = _date
                                && 'Table'[Attribute] = _att
                                && 'Table'[Retailer] = [Retailer]
                                && 'Table'[Index] < _index
                        )
                    )
        )
    )
RETURN
    [Value] - lastrow

att.png

Attached the modified sample file in the below.

 

Best Regards,
Yingjie Li

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

7 REPLIES 7
v-yingjl
Community Support
Community Support

Hi @Anonymous ,

To get the previous row value, you need to create an index column in power query editor first:

index column.png

Create this calculated column:

Anomoly =
VAR _date = [Date]
VAR _index = [Index]
VAR lastrow =
    CALCULATE (
        MAX ( 'Table'[Value] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Date] = _date
                && 'Table'[Index]
                    = CALCULATE (
                        MAX ( 'Table'[Index] ),
                        FILTER ( ALL ( 'Table' ), 'Table'[Date] = _date && 'Table'[Index] < _index )
                    )
        )
    )
RETURN
    [Value] - lastrow

re.png

Attached a sample file in the below, hopes to help you.

 

Best Regards,
Yingjie Li

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

Anonymous
Not applicable

Hi @v-yingjl , 

 

Thanks for so much for your solution, but this will not work if i had different Retailers and Attributes.

 

 

Hi @Anonymous ,

If you want to calculate for each Retailer and Attribute, just add their fields as variables into the previous formula:

Anomoly =
VAR _date = [Date]
VAR _index = [Index]
VAR _re = [Retailer]
VAR _att = [Attribute]
VAR lastrow =
    CALCULATE (
        MAX ( 'Table'[Value] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Date] = _date
                && 'Table'[Retailer] = _re
                && 'Table'[Attribute] = _att
                && 'Table'[Index]
                    = CALCULATE (
                        MAX ( 'Table'[Index] ),
                        FILTER (
                            ALL ( 'Table' ),
                            'Table'[Date] = _date
                                && 'Table'[Attribute] = _att
                                && 'Table'[Retailer] = [Retailer]
                                && 'Table'[Index] < _index
                        )
                    )
        )
    )
RETURN
    [Value] - lastrow

att.png

Attached the modified sample file in the below.

 

Best Regards,
Yingjie Li

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

@Anonymous 

If you need this to be dynamic (you are going to be using slicers for example) you should be writing measures instead of calculated columns. RANKX is your friend.
If you get stuck, please provide sample data.





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






richbenmintz
Solution Sage
Solution Sage

Hi @Anonymous,

 

Couple Questions:

1. Can you provide the data in a table rather than an image?

2. What is the tie breaker when ordering the rows?

 

Thanks,

Hope this Helps,
Richard
Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!



I hope this helps,
Richard

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

Proud to be a Super User!


Anonymous
Not applicable

@richbenmintz Retailer and Attrbute are tie breaker

I am unable to add Table, give me HTML error ans moves the reply to spam

Hi @Anonymous,

 

- you should just be able to paste the data into the body of the message

- within the retailer and attribute how are the rows supposed to be ordered, in your sample data the retailer and attribute are duplicated how would the system know which row comes first? the natural order of the data or is there another sort by field?



I hope this helps,
Richard

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

Proud to be a Super User!


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! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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