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
johnlloyd45
Regular Visitor

Calculating the Interval of a Cumulative Column

I have large datasets of approximately 1-minute step timeseries cumulative energy meter readings from multiple meters in multiple homes, and within Power BI I need to calculate the interval for each meter between each timestep so I can display energy consumption.

 

I'm relatively new to DAX and am really struggling to get this to work. I've tried following the example from this thread but am having problems with the EARLIER function. I also can't get this to work using the example data provided within that thread, so am wondering if this solution is no longer supported in the current version of Power BI?

 

Below is an example of the data once imported and cleaned. You'll see there are four columns of cumulative meter readings (highlighted yellow), the timestamps (highlighted orange) and the different home ID's (highlighted red).

 

Can anyone help me with this? It's driving me crazy!

 

johnlloyd45_1-1641482404972.png

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @johnlloyd45 ,

I updated your sample pbix file(see attachment), please check whether that is what you want.

1. Create a calculated column as below to get the previous DateTime(Hour)

Pre DateTime (Hour) = 
CALCULATE (
    MAX ( 'Trial Import'[DateTime (Hour)] ),
    FILTER (
        'Trial Import',
        'Trial Import'[Home ID] = EARLIER ( 'Trial Import'[Home ID] )
            && 'Trial Import'[DateTime (Hour)] < EARLIER ( 'Trial Import'[DateTime (Hour)] )
    )
)

yingyinr_0-1641981255478.png

2. Create the measures as below to get the interval of Hot water, Regulated and Renewable

Hot Water Interval =
VAR _prehour =
    SELECTEDVALUE ( 'Trial Import'[Pre DateTime (Hour)] )
VAR _htusage =
    SUM ( 'Trial Import'[Hot Water Usage] )
VAR _prehtusage =
    SUMX (
        FILTER (
            ALLSELECTED ( 'Trial Import' ),
            'Trial Import'[DateTime (Hour)] = _prehour
        ),
        'Trial Import'[Hot Water Usage]
    )
RETURN
    IF ( ISBLANK ( _prehtusage ), BLANK (), _htusage - _prehtusage )
Regulated Interval =
VAR _prehour =
    SELECTEDVALUE ( 'Trial Import'[Pre DateTime (Hour)] )
VAR _regusage =
    SUM ( 'Trial Import'[Regulated Energy Used] )
VAR _preregusage =
    SUMX (
        FILTER (
            ALLSELECTED ( 'Trial Import' ),
            'Trial Import'[DateTime (Hour)] = _prehour
        ),
        'Trial Import'[Regulated Energy Used]
    )
RETURN
    IF ( ISBLANK ( _preregusage ), BLANK (), _regusage - _preregusage )
Renewable Interval =
VAR _prehour =
    SELECTEDVALUE ( 'Trial Import'[Pre DateTime (Hour)] )
VAR _reusage =
    SUM ( 'Trial Import'[Unregulated Energy Used] )
VAR _prerenusage =
    SUMX (
        FILTER (
            ALLSELECTED ( 'Trial Import' ),
            'Trial Import'[DateTime (Hour)] = _prehour
        ),
        'Trial Import'[Unregulated Energy Used]
    )
RETURN
    IF ( ISBLANK ( _prerenusage ), BLANK (), _reusage - _prerenusage )

yingyinr_1-1641981594850.png

Best Regards

View solution in original post

12 REPLIES 12
parry2k
Super User
Super User

@johnlloyd45 there are many ways to do this and the only concern I will have that the performance. How large is your dataset? 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

The dataset wil eventually be for around 300 homes, with one meter reading being taken every 10 to 15 minutes, and it'll grow indefinitely. I can always limit the size of the dataset by archiving older readings and only keeping the last 12 months or so of data in the Power BI. Once I've got it working I'd also be looking to move away from Power BI Desktop and move the cloud hosted version which I presume would deal with large scale a lot better?

parry2k
Super User
Super User

@johnlloyd45 are you getting any errors? What expression did you use? It will be easier if you paste data in the table format and share the expected output.

 

 

Follow us on LinkedIn and YouTube.gif to our YouTube channel

 

Learn about conditional formatting at Microsoft Reactor

My latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Sure thing. You can download an example Power BI file with a selection of the data and tables here.

 

https://www.dropbox.com/s/jkoweuvuw3n6649/Cumulative%20Meter%20Reading%20Example.pbix?dl=0

 

I've tried numerous examples from posts on this forum but haven't managed to succeed.

 

In this file I've filtered by a single House ID, and aggregated the data in daily meter reading values in one table, and hourly meter reading values in another table. You can see from this that the readings are cumulative and therefore do not represent the energy consumption for each day or hourly period. An example of the hourly table is below:

 

Home IDDateTime (Hour)Hot Water UsageRegulated Energy UsedUnregulated Energy Used
18899409/05/2021 14:00339724024.5674377.91
18899409/05/2021 15:00339724024.8344378.033
18899409/05/2021 16:00339724025.0994378.158
18899409/05/2021 17:00339724025.3664378.281
18899409/05/2021 18:00339724025.6314378.407
18899409/05/2021 19:00339724025.8984378.531
18899409/05/2021 20:00339724026.6974378.651

 

 

I'm looking to add some calcualted columns for each of the four meters, which is the difference between the reading at each time, and the reading at the previous time - the red columns below.

 

Home IDDateTime (Hour)Hot Water UsageHot Water IntervalRegulated Energy UsedRegulated IntervalUnregulated Energy UsedRenewable Interval
18899409/05/2021 14:003397204024.56704377.910
18899409/05/2021 15:003397204024.8340.2674378.0330.123
18899409/05/2021 16:003397204025.0990.2654378.1580.125
18899409/05/2021 17:003397204025.3660.2674378.2810.123
18899409/05/2021 18:003397204025.6310.2654378.4070.126
18899409/05/2021 19:003397204025.8980.2674378.5310.124
18899409/05/2021 20:003397204026.6970.7994378.6510.12
18899409/05/2021 21:003397204026.9940.2974378.7760.125
18899409/05/2021 22:003397204027.2620.2684378.9030.127
18899409/05/2021 23:003397204027.5260.2644379.0210.118
18899410/05/2021 00:003397204027.7910.2654379.1440.123
18899410/05/2021 01:003397204028.5790.7884379.2680.124
18899410/05/2021 02:003397204031.8823.3034379.3880.12
18899410/05/2021 03:003397204032.550.6684379.5060.118

 

 

Anonymous
Not applicable

Hi @johnlloyd45 ,

I updated your sample pbix file(see attachment), please check whether that is what you want.

1. Create a calculated column as below to get the previous DateTime(Hour)

Pre DateTime (Hour) = 
CALCULATE (
    MAX ( 'Trial Import'[DateTime (Hour)] ),
    FILTER (
        'Trial Import',
        'Trial Import'[Home ID] = EARLIER ( 'Trial Import'[Home ID] )
            && 'Trial Import'[DateTime (Hour)] < EARLIER ( 'Trial Import'[DateTime (Hour)] )
    )
)

yingyinr_0-1641981255478.png

2. Create the measures as below to get the interval of Hot water, Regulated and Renewable

Hot Water Interval =
VAR _prehour =
    SELECTEDVALUE ( 'Trial Import'[Pre DateTime (Hour)] )
VAR _htusage =
    SUM ( 'Trial Import'[Hot Water Usage] )
VAR _prehtusage =
    SUMX (
        FILTER (
            ALLSELECTED ( 'Trial Import' ),
            'Trial Import'[DateTime (Hour)] = _prehour
        ),
        'Trial Import'[Hot Water Usage]
    )
RETURN
    IF ( ISBLANK ( _prehtusage ), BLANK (), _htusage - _prehtusage )
Regulated Interval =
VAR _prehour =
    SELECTEDVALUE ( 'Trial Import'[Pre DateTime (Hour)] )
VAR _regusage =
    SUM ( 'Trial Import'[Regulated Energy Used] )
VAR _preregusage =
    SUMX (
        FILTER (
            ALLSELECTED ( 'Trial Import' ),
            'Trial Import'[DateTime (Hour)] = _prehour
        ),
        'Trial Import'[Regulated Energy Used]
    )
RETURN
    IF ( ISBLANK ( _preregusage ), BLANK (), _regusage - _preregusage )
Renewable Interval =
VAR _prehour =
    SELECTEDVALUE ( 'Trial Import'[Pre DateTime (Hour)] )
VAR _reusage =
    SUM ( 'Trial Import'[Unregulated Energy Used] )
VAR _prerenusage =
    SUMX (
        FILTER (
            ALLSELECTED ( 'Trial Import' ),
            'Trial Import'[DateTime (Hour)] = _prehour
        ),
        'Trial Import'[Unregulated Energy Used]
    )
RETURN
    IF ( ISBLANK ( _prerenusage ), BLANK (), _reusage - _prerenusage )

yingyinr_1-1641981594850.png

Best Regards

Hi @Anonymous, rather than calculating the intervals as measures, would it be possible to do them as calculated columns so that I could use aggregation to view the data at different time intervals?

Anonymous
Not applicable

Hi @johnlloyd45 ,

You can create the calculated columns as below to get them, please find the details in the attachment.

Column_Hot Water Interval = 
VAR _prehour = 'Trial Import'[Pre DateTime (Hour)]
VAR _prehtgusage =
    CALCULATE (
        SUM ( 'Trial Import'[Hot Water Usage] ),
        FILTER (
            'Trial Import',
            'Trial Import'[Home ID] = EARLIER ( 'Trial Import'[Home ID] )
                && 'Trial Import'[DateTime (Hour)] = _prehour
        )
    )
VAR _curhtgusage =
    CALCULATE (
        SUM ( 'Trial Import'[Hot Water Usage] ),
        FILTER (
            'Trial Import',
            'Trial Import'[Home ID] = EARLIER ( 'Trial Import'[Home ID] )
                && 'Trial Import'[DateTime (Hour)] = EARLIER ( 'Trial Import'[DateTime (Hour)] )
        )
    )
RETURN
    IF ( ISBLANK ( _prehtgusage ), BLANK (), _curhtgusage - _prehtgusage )
Column_Regulated Interval = 
VAR _prehour = 'Trial Import'[Pre DateTime (Hour)]
VAR _preregusage =
    CALCULATE (
        SUM ( 'Trial Import'[Regulated Energy Used] ),
        FILTER (
            'Trial Import',
            'Trial Import'[Home ID] = EARLIER ( 'Trial Import'[Home ID] )
                && 'Trial Import'[DateTime (Hour)] = _prehour
        )
    )
VAR _curregusage =
    CALCULATE (
        SUM ( 'Trial Import'[Regulated Energy Used] ),
        FILTER (
            'Trial Import',
            'Trial Import'[Home ID] = EARLIER ( 'Trial Import'[Home ID] )
                && 'Trial Import'[DateTime (Hour)] = EARLIER ( 'Trial Import'[DateTime (Hour)] )
        )
    )
RETURN
    IF ( ISBLANK ( _preregusage ), BLANK (), _curregusage - _preregusage )
Column_Renewable Interval = 
VAR _prehour = 'Trial Import'[Pre DateTime (Hour)]
VAR _prerenusage =
    CALCULATE (
        SUM ( 'Trial Import'[Unregulated Energy Used] ),
        FILTER (
            'Trial Import',
            'Trial Import'[Home ID] = EARLIER ( 'Trial Import'[Home ID] )
                && 'Trial Import'[DateTime (Hour)] = _prehour
        )
    )
VAR _currenusage =
    CALCULATE (
        SUM ( 'Trial Import'[Unregulated Energy Used] ),
        FILTER (
            'Trial Import',
            'Trial Import'[Home ID] = EARLIER ( 'Trial Import'[Home ID] )
                && 'Trial Import'[DateTime (Hour)] = EARLIER ( 'Trial Import'[DateTime (Hour)] )
        )
    )
RETURN
    IF ( ISBLANK ( _prerenusage ), BLANK (), _currenusage - _prerenusage )

yingyinr_0-1642475461129.png

Best Regards

Thanks @Anonymous, that seems to have done exactly what I wanted, certainly for the hourly data. I'll spend some time understanding what you've done and seeing if I can also get it working for aggregations, such as daily, monthly, annual totals, etc.

 

You're a legend!

@johnlloyd45 if you want calculated column (opposed to measure) that means it is not affceted by filter context. If that is the case and if you are getting it from a RDBMS (e.g. MS SQL), it would be a lot performant doing it on the server side than with DAX (not that DAX can't take care of it, but performance will be concern).

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Unfortunately the incoming data is from daily CSV files dropped onto an FTP site so I have no option to do the calcualtion prior to importing into Power BI. I'm happy to do the calc either with a Measure or a Column...I just need to find a way of doing it. I can mitigate performance issues later if needed.

Hi @smpa01 & @parry2k 

 

Any chance you can give me some suggestions on how do to it, ignoring any potential performance issues for the time being?

This looks to be a similar issue, albeit a little simpler because it doesn't have the added complexity of mutliple homes in the same dataset?

 

https://community.powerbi.com/t5/Desktop/Calculating-cumulative-difference-between-two-timestamps-fo...

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