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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
DmitryKo
Helper IV
Helper IV

Measure to show value change since previous value

The source table is a simple log-type table similar to:

KeyDateValueComment (text)
A2022-08-012 
A2022-08-033 
A2022-08-224.5 
B2022-08-052 
B2022-08-171 

 

Note that date (timestamp) values are not guaranteed to be sequential; an important assumption taken in many similar questions is not valid here. Dates can be random as presented in the sample above.

The goal is, to create a DAX measure to calculate:

  • average value (straightforward)
  • latest value (not so straightforward but doable)
  • previous value
  • value change between latest and previous value

The result over the sample data above would have to be this:

MetricLatest valuePrevious valueChange since previous
A4.531.5
B12-1

 

Any suggestion on the "previous value" DAX code?

8 REPLIES 8
v-jianboli-msft
Community Support
Community Support

Hi @DmitryKo ,

 

Based on your description, I have modified the sample data:

vjianbolimsft_0-1662088485221.png

Here is the measure:

 

average value = AVERAGE('Table'[Value])

latest value = CALCULATE(MAX('Table'[Value]),FILTER('Table',[Index]=MAX('Table'[Index])))

previous value = 
var _a = CALCULATE(MAX([Date]),FILTER('Table',[Index]=MAX([Index])))
var _b = CALCULATE(MAX([Date]),FILTER('Table',[Date]<_a))
return CALCULATE(MAX([Value]),FILTER('Table',[Date]=_b))

value change between latest and previous value = [latest value]-[previous value]

 

Final output:

vjianbolimsft_1-1662088586669.png

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

v-jianboli-msft
Community Support
Community Support

Hi @DmitryKo ,

 

The purpose of creating index is to help find the previous row's value, instead of the previous date. For example, even if the previous row's date is much more bigger than current date, it will still return previous row's value.

vjianbolimsft_0-1661827961977.png

vjianbolimsft_1-1661827982455.png

If I misunderstood, please give more details to help me clearify your problem.

 

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

It's not about using the index or not. It's about logic.

"Change since previous" is NOT the difference between current (as defined by date context filters) value and value at latest possible date available within the dataset. It's the difference between the value at current (as defined by date context filters) value and the value that was available previously - at the preceding date.

The solutions suggested calculate measures for (latest-current), essentially, while what is needed is (current-previous)

v-jianboli-msft
Community Support
Community Support

Hi @DmitryKo ,

 

Approve with @daXtreme , You can create an index column to help calculate. Please try:

First, create an index column in power query.

vjianbolimsft_0-1661505573441.png

Here is the M code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIyMDLSNbDQNTAEcYBYQSlWB03KGMgxxiplBNJhomcKl3RC1meKYiSylKE5kGMIkYoFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Key = _t, Date = _t, Value = _t, #"Comment (text)" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Key", type text}, {"Date", type date}, {"Value", type number}, {"Comment (text)", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type)
in
    #"Added Index"

Output:

vjianbolimsft_1-1661505655535.png

Then create these measure:

average value = AVERAGE('Table'[Value])

latest value = CALCULATE(MAX('Table'[Value]),FILTER('Table',[Index]=MAX('Table'[Index])))

previous value = 
var _a = MAX('Table'[Index])
var _b = MAXX(FILTER(ALL('Table'),[Index]<_a),[Index])
return CALCULATE(MAX('Table'[Value]),FILTER('Table',[Index]=_b))

value change between latest and previous value = [latest value]-[previous value]

Final output:

vjianbolimsft_2-1661506322667.png

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

daXtreme
Solution Sage
Solution Sage

@DmitryKo 

 

Why don't you index the rows with an integer that's sequential within each key and follows the Date order? That would then be dead easy to calculate everything you want, wouldn't it?

Index or not, solutions offered still calculate "latest possible" value (as opposed to "latest regarding to the curret date range) for "latest" and "delta between current and latest possble" instead of delta between current and previous values. Not the logic needed.

DmitryKo
Helper IV
Helper IV

This metric, specifically Previous value, provides incorrect results over that very sample data above. Specifically, it always returns value that is "previous" related to the LATEST date for the dataset, not that is previous to the CURRENT (specified by "Date") timestamp. As a result, change over the previous value is incorrect as well.

 

amitchandak
Super User
Super User

@DmitryKo , Try like

 

Latest value =
var _max = maxx(filter(allselected(Table), [Key] = max(Table[Key]) ), [Date])
return
calculate(Sum(Table[Value]), filter(Table, Table[Date] = _max))


Previous value =
var _max1 = maxx(filter(allselected(Table), [Key] = max(Table[Key]) ), [Date])
var _max = maxx(filter(allselected(Table), [Key] = max(Table[Key]) && [Date] <_max1 ), [Date])
return
calculate(Sum(Table[Value]), filter(Table, Table[Date] = _max))

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.