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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
markostalnacke
Frequent Visitor

How to calculate change comparing to previous value (not date)

Hi, I searched the forum without solution.

I have a table with values being filled generally twice a week. I need to be able to count the change in amount with these last two filled dates. ie.

DateCityAmount
18.4.2020Tokio500
21.4.2020Tokio

550

21.4.2020Helsinki250
24.4.2020Tokio300
24.4.2020Helsinki200

 

What I would like to get:

City18.4.2020Change21.4.Change24.4.Change
Helsinki  250 200-50
Tokio500055050300-200
       

 

Any ideas? Thanks.

1 ACCEPTED SOLUTION
Robert-RK
Regular Visitor

You can try following solution:

1. create measure 'Previous order'

 

Previous order =
var PriorDate = CALCULATE(MAX(city[Date]),
FILTER(ALL(city[Date]), city[Date] < MAX(city[Date])))
RETURN
CALCULATE(SUM(city[Amount]),
FILTER(ALL(city[Date]), city[Date] = PriorDate))
 
2. and then measure that you need:
 
Change = IF(ISBLANK([Previous order]),BLANK(),SUM(city[Amount]) - [Previous order])

View solution in original post

4 REPLIES 4
Robert-RK
Regular Visitor

You can try following solution:

1. create measure 'Previous order'

 

Previous order =
var PriorDate = CALCULATE(MAX(city[Date]),
FILTER(ALL(city[Date]), city[Date] < MAX(city[Date])))
RETURN
CALCULATE(SUM(city[Amount]),
FILTER(ALL(city[Date]), city[Date] = PriorDate))
 
2. and then measure that you need:
 
Change = IF(ISBLANK([Previous order]),BLANK(),SUM(city[Amount]) - [Previous order])

Great Robert! Works!

harshnathani
Community Champion
Community Champion

Hi @markostalnacke ,

 

You can use this in the Query Editor.

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrTQN9E3MjAyUNJRCsnPzswH0qYGBkqxOtFKRoZY5Ewx5TxSc4oz87IzgUwjmLQJplZjA0w5ZK0g6VgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ate = _t, City = _t, Amount = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ate", type text}, {"City", type text}, {"Amount", Int64.Type}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"ate", "Date"}}),
#"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date", type text}}),
#"Pivoted Column" = Table.Pivot(#"Changed Type1", List.Distinct(#"Changed Type1"[Date]), "Date", "Amount", List.Sum),
#"Added Custom" = Table.AddColumn(#"Pivoted Column", "Change1", each [#"21/4/2020"]-[#"18/4/2020"]),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Change2", each [#"24/4/2020"]-[#"21/4/2020"]),
#"Reordered Columns" = Table.ReorderColumns(#"Added Custom1",{"City", "18/4/2020", "21/4/2020", "Change1", "24/4/2020", "Change2"})
in
#"Reordered Columns"

 

 

 

12.JPG

 

Regards,

Harsh Nathani

Hi Harsh, 

Your solution is assuming the dates are always those. I can't use it that way, because we will have dates (and values) for months and dates unfortunately can differ. ie. some unit misses a date etc. 

 

This would do the trick, but would need a create function PREVIOUSVALUE 🙂
Change=CALCULATE(Sum(Table[Amount]);PREVIOUSDAY(Table[Amount]))

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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