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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Delta values

Hi,

 

How would I calculate Delta-values derived from a Date in combination with ID:

DateIDValuesDelta-values
DD-MM-YYYY  12:00188 
DD-MM-YYYY  12:00223 
DD-MM-YYYY  12:151891
DD-MM-YYYY  12:152252
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi  @Anonymous ,

Here are the steps you can follow:

1. Increase the index through power query

v-yangliu-msft_0-1605673882153.png

2. Create calculcated column.

Delta-values =
var _lastindex=
CALCULATE(MAX('Table'[Index]),FILTER(ALL('Table'),[Metern]=EARLIER([Metern])&&[Index]<EARLIER([Index])))

var _lastdata=
CALCULATE(SUM('Table'[Data]),FILTER('Table',[Index]=_lastindex))

return IF(_lastindex=BLANK(),BLANK(),[Data]-_lastdata)

3. Result.

v-yangliu-msft_1-1605673882155.png

You can downloaded PBIX file from here.

 

Best Regards,

Liu Yang

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

View solution in original post

9 REPLIES 9
Anonymous
Not applicable

Hi  @Anonymous ,

Here are the steps you can follow:

1. Increase the index through power query

v-yangliu-msft_0-1605673882153.png

2. Create calculcated column.

Delta-values =
var _lastindex=
CALCULATE(MAX('Table'[Index]),FILTER(ALL('Table'),[Metern]=EARLIER([Metern])&&[Index]<EARLIER([Index])))

var _lastdata=
CALCULATE(SUM('Table'[Data]),FILTER('Table',[Index]=_lastindex))

return IF(_lastindex=BLANK(),BLANK(),[Data]-_lastdata)

3. Result.

v-yangliu-msft_1-1605673882155.png

You can downloaded PBIX file from here.

 

Best Regards,

Liu Yang

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

Anonymous
Not applicable

To fact that you can do something does not mean you should. Such data transformations are best done (and easy!) in Power Query. DAX should be used to create calculated columns only as the very, very, very last resort. There are many reasons behind this, two of them being poor compression and model bloat if done through DAX. Please use Power Query - the right tool for the job.

Anonymous
Not applicable

Daxer,

That all makes sense,

However, I would still need the DAX code for it,

 

Any input for the DAX code highly appreciated!

Anonymous
Not applicable

No, you don't need DAX. You need M - the language of Power Query. That's the whole point. DAX is for data analysis. M is for data munging.

Anonymous
Not applicable

Sorry - so Power Query it is.. 

Still need the a working code though!

Anonymous
Not applicable

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hY9BCsAgDAS/UnK2mGytFL8i/v8btdCi1gUhOY3ZdXIWKHRXq7MZkmodcWJ1IcX9+dnxY+IhoeM28TjkB5L/cdD71g/6v9YPmh+H/GvkWPhj4Q/ur/Bq/nnF/XvO/Ec++/ec+fP+17/c", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Datetime = _t, ID = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Datetime", type datetime}, {"ID", Int64.Type}, {"Value", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"ID", Order.Ascending}, {"Datetime", Order.Ascending}}),
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 1, 1, Int64.Type),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Index",{"Index", "Datetime", "ID", "Value"}),
    #"Added Custom" = Table.AddColumn(#"Reordered Columns", 
        "ValueBefore", 
        each Table.SelectRows(
                #"Reordered Columns", 
                (r) => r[ID] = [ID] and [Index] = r[Index] + 1)
    ),
    #"Expanded ValueBefore" = Table.ExpandTableColumn(#"Added Custom", "ValueBefore", {"Value"}, {"Value.1"}),
    #"Renamed Columns" = Table.RenameColumns(#"Expanded ValueBefore",{{"Value.1", "ValueBefore"}}),
    #"Added Custom1" = Table.AddColumn(#"Renamed Columns", "Delta", each [Value] - [ValueBefore]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Value", "ValueBefore", "Index"})
in
    #"Removed Columns"

 

Here's the M code. Paste it into the Advanced Editor in Power Query and you'll see step-by-step how this is done. Then use it accordingly.

Anonymous
Not applicable

Hi  @Anonymous ,

 

Here are the steps you can follow:

1. Create calculcated column.

Delta-values =
var _max=MAX('Table'[Date])
var _min=MIN('Table'[Date])
var _1=
CALCULATE(SUM('Table'[Values]),FILTER('Table','Table'[Date]=_max&&'Table'[ID]=EARLIER('Table'[ID])))
-
CALCULATE(SUM('Table'[Values]),FILTER('Table','Table'[Date]=_min&&'Table'[ID]=EARLIER('Table'[ID])))

return IF('Table'[Date]=_min,BLANK(),_1)

2. Result.

v-yangliu-msft_0-1605519209396.jpeg

You can downloaded PBIX file from here.

 

Best Regards,

Liu Yang

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

Anonymous
Not applicable

Thanks Liu,

 

I followed your steps, and also ended up with a column - however the values seems wrong and does not reflect the expected delta-value as in your example,

 

Where did I go wrong?

 

2020-11-16_13-23-16.jpg

 

 

 

 

amitchandak
Super User
Super User

@Anonymous , Try formula

 

Column = var _1 = MAXX(FILTER('Table',[ID] =EARLIER('Table'[ID]) && [Date] <EARLIER('Table'[Date])), LASTNONBLANKVALUE('Table'[Date],'Table'[Values]))
return if( ISBLANK(_1), BLANK(),[Values]-_1)
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
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.

Top Solution Authors