Forum Discussion
Difference between two rows
Hi,
I need help in order to solve a problem trying to calculate the differences between two rows in Power BI.
My tables look like:
Date Orders Index
01/01/2017 8501 1
02/01/2017 8345 2
03/01/2017 7985 3
04/01/2017 8134 4
I would need to performt the difference betweent the rows of Order in order to get something like:
Difference
156 (from 8501-8345)
360 (from 8345-7985)
-149 (from 7985-8134)
I've tried to add a column with:
=Orders{Index}-Orders{Index+1}
but it does not work.
I will appreciate any help. Thanks
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 ) )
32 Replies
- MarcelBeugCommunity Champion
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_MuhammadCommunity 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 ) )- david7fFrequent Visitor
thanks for the solution, it works perfectly
- TorbenKirkWolfAdvocate 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!
- pxg08680Resolver III
- AnonymousNot applicable
I have a similar case and will use same example. Let's say there's another column named "Store" with values 'Store1', 'Store2' and
'Store3'. Is it possible to still use DAX and have 1 table insetad of 3? As current example at some points gives me order diffrence between 'Store1'
and 'Store2'.
- Ashish_MathurSuper User
Hi SSS,
If you have a calendar table and there is a relatioship from the date column of your base data table to the date column of the calendar table, then you can use this calculated field formula
=CALCULATE(SUM(Data[Orders]),PREVIOUSDAY(Calendar{Date]))-SUM(Data[Orders])
- Zubair_MuhammadCommunity Champion
Anonymous
You can use one of these...I believe
Days from Next Date = VAR Next_Date = MINX ( TOPN ( 1, FILTER ( Table1, [Key] = EARLIER ( [Key] ) && [Date] > EARLIER ( [Date] ) ), [Date], ASC ), [Date] ) RETURN DATEDIFF ( [Date], Next_Date, DAY )OR
Days from Previous Date = VAR Previous_Date = MINX ( TOPN ( 1, FILTER ( Table1, [Key] = EARLIER ( [Key] ) && [Date] < EARLIER ( [Date] ) ), [Date], DESC ), [Date] ) RETURN DATEDIFF ( Previous_Date, [Date], DAY )- AnonymousNot applicable
Hi Zubair_Muhammad,
Thanks a lot!
The first one result a strange column, but the second works when I compared with Excel result.
Now, I'll proceed to tests accessing real data from the database.
Dax has some mysteries to me, yet. I didn't know the VAR concept or the possibility to call previous or next records in an instruction.
Thanks a lot again.
- SSSHelper I
Thanks Guys!! It really helped!
- tomislav_miHelper II
Hey guys,
I encountered one problem I simply can't solve. If anyone can help me out I would be extra grateful!
I want to calculate the difference between two rows, but after I import data to the data model, the sort order is mixed up (see below: Report Date and Account Name) so one of my formulas that could work if the sort order is correct doesn't work.
The table looks like this and the Change column like this is my goal.Account Name MRR Report Date Change a 100 1/31/2017 0:00 100 a 100 2/28/2017 0:00 0 a 150 4/30/2017 0:00 -30 a 100 5/31/2017 0:00 -50 b 13 7/31/2019 0:00 13 b 13 8/31/2019 0:00 0 c 5 7/31/2019 0:00 5 a 100 6/30/2017 0:00 0 a 100 7/31/2017 0:00 0 a 100 8/31/2017 0:00 0 a 100 9/30/2017 0:00 0 a 100 10/31/2017 0:00 0 a 180 3/31/2017 0:00 80
You can notice that the account doesn't have the correct order either by the report date.
If there is any way how to solve it (maybe as a measure? or maybe is there a way how to correctly import data?)
I would be so so grateful.
All the best,
Tomislav- Ashish_MathurSuper User
- tomislav_miHelper II
Thank you Ashish_Mathur !
Works amazing!
Thank you so much.
All the best from Croatia!
Tomislav
- RathanFrequent Visitor
Hello Guys,
I have the same question on how to find the difference between two rows from the dated column. Below is how my table looks.
Date Quantity Diff 06/01/2020 1 06/02/2020 3 2 06/03/2020 6 3 I just want the difference between the quantity column. I have tried multiple queries but it works fine in Import Mode but mine is Direct Query mode. Looks like there are many limitations in Direct Query. Could someone help me in giving some guidance regarding this for DIrect Query?
Thanks in advance.
- Ashish_MathurSuper User
Hi,
You should have a Calendar Table with a relationship from the Date column of your Data Table to the Date column of your Calendar Table. To your visual, drag the Date column from the Calendar Table. Write these measures:
Quantity = sum(Data[Number])
Quantity on previous day = calculate([quantity],previousday(calendar[date]))
Diff in quantity = [quantity]-[Quantity on previous day]
Hope this helps.
- RathanFrequent Visitor
Thank you. It worked
- pijotr93New Member
I have another case. I would like to calculate the difference between rows. For example for 10-22
77-73=4
73-67=6
67-59=8etc...
- oldandnewFrequent Visitor
I have a similar problem.
I have 2 different datasets (for example sake, let's say volume table and ranking table). In the volume dataset, I have a measure that calculates the sum of the volume for each material based on an indicator column if it was received or issued. In the ranking table, the plant, material, plant volume limit, and the plant volume limit change date. I created a column in the ranking table called ranking that gives a numerical rank of the oldest plant volume limit change date from earliest (rank 1) to the latest for each material at each plant. I would like to accomplish 2 things:
- Calculate the change in plant volume limit between 2 dates for each material and each plant (i.e. the plant volume limit change between rank 1 & 2 as well as 2 & 3 for each material at each plant).
- Show the volume measure in the ranking table for each material at each plant. When I currently pull it, it shows the sum of all the volumes for every row. How do I fix this?
Here is the ranking table
- Ashish_MathurSuper User
Hi,
Your question is not at all clear. Start a new thread and post your question there.