Forum Discussion
Calaculate the difference between one column if the other column contains a certain text
Hi There.
I was hoping you could help me with this.
I have a table where I would like the difference to be calculated based on the item number
So for example Item Number 22 I'd like the difference of landed unit cost between the current item number 22 and the previous one sold to return into another column
I have created a calculated column with this formula
CalculatedDifference =
VAR PreviousItemNumber_ =
CALCULATE (
MAX ( Sheet1[Item Number] ),
Sheet1[Item Number] < EARLIER (Sheet1[Item Number]),
ALLEXCEPT ( Sheet1, Sheet1[Item Number] )
)
VAR PreviousLandedCost_ =
CALCULATE (
DISTINCT ( Sheet1[Landed Unit Cost] ),
Sheet1[Item Number] = PreviousItemNumber_,
ALLEXCEPT ( Sheet1, Sheet1[Item Number] )
)
VAR CurrentLandedCost_ = Sheet1[Landed Unit Cost]
RETURN
IF (
NOT ISBLANK ( CurrentLandedCost_ ) && NOT ISBLANK ( PreviousLandedCost_ ),
CurrentLandedCost_ - PreviousLandedCost_
But it is returning an error ;A table of multiple values was supplied where a single value was expected
Here is a copy of the table
DatePO NumberItem NumberLanded Unit Cost
| Wednesday 1 January 2020 | 1 | 22 | 10 |
| Thursday 2 January 2020 | 2 | 22 | 12 |
| Friday 3 January 2020 | 3 | 11 | 2.86 |
| Saturday 4 January 2020 | 4 | 11 | 2.85 |
| Sunday 5 January 2020 | 5 | 11 | 2.84 |
| Monday 6 January 2020 | 6 | 77 | 3.84 |
| Tuesday 7 January 2020 | 7 | 77 | 3.89 |
| Wednesday 8 January 2020 | 8 | 77 | 3.89 |
| Thursday 9 January 2020 | 9 | 77 | 3.82 |
| Friday 10 January 2020 | 10 | 77 | 3.75 |
| Saturday 11 January 2020 | 11 | 77 | 3.75 |
| Sunday 12 January 2020 | 12 | 77 | 3.75 |
| Monday 13 January 2020 | 13 | 22 | 1.2 |
| Tuesday 14 January 2020 | 14 | 22 | 1.2 |
| Wednesday 15 January 2020 | 15 | 33 | 5 |
| Thursday 16 January 2020 | 16 | 33 | 5.14 |
| Friday 17 January 2020 | 17 | 33 | 5.12 |
| Saturday 18 January 2020 | 18 | 33 | 5.12 |
Thank you !
- Anonymous6 years ago
HI MaighreadL ,
Create a Calculated Column
Difference = VAR a = CALCULATE ( MAX ( 'Table'[Date] ), FILTER ( 'Table', 'Table'[Item Number] = EARLIER ( 'Table'[Item Number] ) && 'Table'[Date] < EARLIER ( 'Table'[Date] ) ) ) VAR b = CALCULATE ( MAX ( 'Table'[Unit Cost] ), FILTER ( 'Table', 'Table'[Item Number] = EARLIER ( 'Table'[Item Number] ) && 'Table'[Date] = a ) ) RETURN IF ( b <> BLANK (), 'Table'[Unit Cost] - b )Regards,
Harsh Nathani
Appreciate with a Kudos!! (Click the Thumbs Up Button)
Did I answer your question? Mark my post as a solution!
3 Replies
- AnonymousNot applicable
HI MaighreadL ,
Create a Calculated Column
Difference = VAR a = CALCULATE ( MAX ( 'Table'[Date] ), FILTER ( 'Table', 'Table'[Item Number] = EARLIER ( 'Table'[Item Number] ) && 'Table'[Date] < EARLIER ( 'Table'[Date] ) ) ) VAR b = CALCULATE ( MAX ( 'Table'[Unit Cost] ), FILTER ( 'Table', 'Table'[Item Number] = EARLIER ( 'Table'[Item Number] ) && 'Table'[Date] = a ) ) RETURN IF ( b <> BLANK (), 'Table'[Unit Cost] - b )Regards,
Harsh Nathani
Appreciate with a Kudos!! (Click the Thumbs Up Button)
Did I answer your question? Mark my post as a solution!
- parry2k
Super User
MaighreadL I'm sure Anonymous solution will work, I didn't look at it but it can have performance implications on a large dataset. I would add a measure using the following DAX expression:
Measure = VAR __tbl = ALL ( 'Table'[Id], 'Table'[DatePO] ) VAR __prevDate = CALCULATE ( MAX ( 'Table'[DatePO] ), FILTER ( __tbl, 'Table'[DatePO] < MAX ( 'Table'[DatePO] ) ) ) RETURN CALCULATE ( SUM ( 'Table'[Amount] ), __tbl, 'Table'[DatePO] = __prevDate)I would ❤ Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!
⚡Visit us at https://perytus.com, your one-stop shop for Power BI related projects/training/consultancy.⚡
- MaighreadLNew Member
Anonymous This is perfect !!! Thank you so much !!!