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
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
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.

Top Solution Authors