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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
vinceluc22
Regular Visitor

Add the difference of the Current row value and Previous row value and restart formula if new State?

StateDateCasesDifference
Georgia4/1/202022
Georgia4/2/202020
Georgia4/3/202031
Florida4/1/202055
Florida4/2/20203-2
Florida4/3/202041
Texas4/1/202066
Texas4/2/202082
Texas4/3/2020113

 

Above is the result I'm trying to achieve. I'm taking the current row value, subtracting it from the previous row value, and adding the difference in a new Column.

 

I have a formula that works for a single State below. How can I enable it to restart the formula if it detects a new State? I'm using Power Query editor in Power BI and I sorted all of my States alphabetically and Dates in ascending order.

 

Differential =
VAR dates = MyTable[Date]
VAR PrevRow =
CALCULATE(
MAX(MyTable[Cases]),
FILTER(MyTable,
MyTable[Date] < dates))
return
MyTable[Cases] - PrevRow
 
Current, incorrect result:
 
StateDateCasesDifference
Georgia4/1/202022
Georgia4/2/202020
Georgia4/3/202031
Florida4/1/202052
Florida4/2/20203-2
Florida4/3/202041
Texas4/1/202062
Texas4/2/202082
Texas4/3/2020113
1 ACCEPTED SOLUTION
amaniramahi
Helper V
Helper V

Hi,

 

I am not an expert but what I did gave me the needed result

 

I created two columns

1. DateKey = CONCATENATE(Sheet1[State],Sheet1[Date].[Date])

2. PreviousKey = CONCATENATE(Sheet1[State],PREVIOUSDAY(Sheet1[Date].[Date]))

this to make sure that we calculate the difference between the currecnt date and the previous date for the same state

 

Then a third column for the difference

 

 

Difference = 
var Check = CONTAINS(Sheet1,Sheet1[DateKey],Sheet1[PreviousKey])
return
IF(Check=FALSE(),Sheet1[Cases],Sheet1[Cases]-LOOKUPVALUE(Sheet1[Cases],Sheet1[DateKey],Sheet1[PreviousKey]))

 

 

 

and the results as  you need

 

Untitled.png

View solution in original post

3 REPLIES 3
camargos88
Community Champion
Community Champion

Hi @vinceluc22 ,

 

Try this calculated column:

 

Diff =
VAR _date = 'Table'[Date]
VAR _maxDate = CALCULATE(MAX('Table'[Date]); FILTER(ALL('Table'); 'Table'[State] = EARLIER('Table'[State]) && 'Table'[Date] < _date))
VAR _previousValue = CALCULATE(SUM('Table'[Cases]); FILTER(ALL('Table'); 'Table'[State] = EARLIER('Table'[State]) && 'Table'[Date] = _maxDate))
return 'Table'[Cases] - _previousValue
 
Ricardo


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

Proud to be a Super User!



amaniramahi
Helper V
Helper V

Hi,

 

I am not an expert but what I did gave me the needed result

 

I created two columns

1. DateKey = CONCATENATE(Sheet1[State],Sheet1[Date].[Date])

2. PreviousKey = CONCATENATE(Sheet1[State],PREVIOUSDAY(Sheet1[Date].[Date]))

this to make sure that we calculate the difference between the currecnt date and the previous date for the same state

 

Then a third column for the difference

 

 

Difference = 
var Check = CONTAINS(Sheet1,Sheet1[DateKey],Sheet1[PreviousKey])
return
IF(Check=FALSE(),Sheet1[Cases],Sheet1[Cases]-LOOKUPVALUE(Sheet1[Cases],Sheet1[DateKey],Sheet1[PreviousKey]))

 

 

 

and the results as  you need

 

Untitled.png

This worked without any issues, thanks!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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