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
Murten
Frequent Visitor

Using existing data in one column to calculate a delta value in another column with DAX

Hi there

So I have a dataset with 2 columns: "Timestamp" and "NumericValue"

My objective is to create a new column which uses DAX to calculate the change in NumericValue. It should obviously start off with the earliest timestamp date and calculate based off previous value in NumericValue. As new numbers are added continously the logic should be generic and work automatically.
To give context i want to calculate the change of water consumption coming from an IoT water sensor only providing the meter (accumulated total consumption)


My table "Measurements" with examples:
| Timestamp    | NumericValue | Delta_Value |
|--------------- |------------------|---------------|
| 2024-01-01   | 0                        | 0                   |
| 2024-01-02   | 10                      | 10                 |
| 2024-01-03   | 15                      | 5                   |
| 2024-01-04   | 16                      | 1                   |
etc etc

I have tried a lot including chatgpt suggestions, but it doesn't really work. I made with work in Javascript, but i really want it to work using DAX
Thank you!

4 REPLIES 4
Murten
Frequent Visitor

 -

Murten
Frequent Visitor

-

Murten
Frequent Visitor

Hi there

Thank you for the suggestion. However i get this error (translated); "Even if MatchBy columns are specified, duplicate rows are detected in the Relation parameter of OFFSET. This is not allowed."
- There are obviously duplicates in the NumericValue column, but that happens when the sensor doesn't register any new consumption = meter is not moving/counting

OK i may know what the issue is, but now how to fix it... 

So i guess timestamp will have duplicates as I also have columns "deviceId" and "field" with 4 different but constant values, where each value has their own row of timestamp, value etc... So 4x same timestamp
- Yes, this is coming from a NoSQL db.... 
So maybe I need to also filter by only one of the four variables somehow?

This is my code based on your suggestion:

Delta_heat =
VAR _previousvalue =
    SUMX (
        OFFSET (
            -1,
            Measurements,
            ORDERBY ( Measurements[timestamp], ASC ),
            ,
            ,
            MATCHBY ( Measurements[timestamp] )
        ),
        Measurements[numericValue]
    )
RETURN
    IF ( NOT ISBLANK ( _previousvalue ), Measurements[numericValue] - _previousvalue, 0 )
Jihwan_Kim
Super User
Super User

Hi, 
I am not sure how your semantic model looks like, but I tried to create a sample pbix file like below.

Please check the below picture and the attached pbix file, that is for creating a calculated column.

 

Jihwan_Kim_0-1705940613449.png

 

Delta_value CC =
VAR _previousvalue =
    SUMX (
        OFFSET (
            -1,
            Data,
            ORDERBY ( Data[Timestamp], ASC ),
            ,
            ,
            MATCHBY ( Data[Timestamp] )
        ),
        Data[NumericValue]
    )
RETURN
    IF ( NOT ISBLANK ( _previousvalue ), Data[NumericValue] - _previousvalue, 0 )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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