Forum Discussion
Difference between two rows
- 8 years ago
Hi SSS
Using DAX you can add this calculated column to get desired results
= VAR NextIndex = Table1[Index] + 1 RETURN Table1[Orders] - CALCULATE ( VALUES ( Table1[Orders] ), FILTER ( ALL ( Table1 ), Table1[Index] = NextIndex ) )
It looks lik you are a looking for a solution in Power Query?
In general, if you want to compare values from different rows, you can add 2 index columns, one starting with 0 and the other with 1.
Then you merge the query with itself, using the index columns as merge columns.
If you want the data from the previous row on the current row, then you should merge on Index 0 and Index 1 (in this sequence);
if you want the data from the next row on the current row, then you should mergen on Index 1 and Index 0.
After merging, adjust the generated code and name your column "Previous" or "Next".
Exoand, using the original column name as prefix,
After expanding sort on one of the original index columns.
Below the code for your case.
let
Source = Table1,
#"Added Index" = Table.AddIndexColumn(Source, "Index.0", 0, 1),
#"Merged Queries" = Table.NestedJoin(#"Added Index",{"Index"},#"Added Index",{"Index.0"},"Next",JoinKind.LeftOuter),
#"Expanded Next" = Table.ExpandTableColumn(#"Merged Queries", "Next", {"Orders"}, {"Next.Orders"}),
#"Sorted Rows" = Table.Sort(#"Expanded Next",{{"Index", Order.Ascending}}),
#"Added Custom" = Table.AddColumn(#"Sorted Rows", "Difference", each [Orders] - [Next.Orders]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index.0", "Next.Orders"})
in
#"Removed Columns"
- Zubair_Muhammad8 years ago
Community Champion
Hi SSS
Using DAX you can add this calculated column to get desired results
= VAR NextIndex = Table1[Index] + 1 RETURN Table1[Orders] - CALCULATE ( VALUES ( Table1[Orders] ), FILTER ( ALL ( Table1 ), Table1[Index] = NextIndex ) )- david7f8 years agoFrequent Visitor
thanks for the solution, it works perfectly
- fxz98 years agoFrequent Visitor
Can we make the difference as new row and named Difference?
- Anonymous7 years agoNot applicable
Hi Zubair_Muhammad,
Congrats for the solution.
Can I take ask for a complementary demand on the same idea?
As the original demand, I need to execute the difference between two dates located in different rows, but I need to consider a key. It is something like this:
Key Date
1 01/08/2018
1 12/09/2018
1 05/10/2018
2 02/07/2018
2 16/08/2018
Thanks a lot in advance.
Regards.
- htalat63 years agoFrequent Visitor
i am actually looking to find the solution to the same usecase.
Please share if you find out anything
- TorbenKirkWolf5 years ago
Advocate I
Well, that's a neat trick! This thread showed up on a google search, and it's exactly what I needed. So I just want to let you know that your reply still lives on and helps people 🙂
I've never thought of merging a table with itself on different indexes, but now you've mentioned it, it's such an obvious and elegant solution. I would ever have thought of this myself, so a big thanks from me!