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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Kahrax
Frequent Visitor

How to perform integral. Difference between two rows.

How to perform in Power BI.

 

I have the following tables in excel

 

 ABC
1TimestampkW savedkWh saved
201.01.2020 00:00:00200
302.01.2020 01:00:0030=(A3-A2)*24*((B3+B2)/2)
402.01.2020 01:10:002=(A4-A3)*24*((B4+B3)/2)

 

This then continues for rov 5...N

 

Plotting colum A and B gives a line plot with column C being the integral (area under the line). In Power BI I have colum A and B (no index column), and I want column C to be a calculated column. 

 

I have searched for all types of "difference between two rows" etc, but cant really find something that works. The difference between the date rows needs to be in hours. E.g 90minutes would then be 1,5hours.  If there is a smarter and better way in Power BI to find the area below a graph, please do tell me. 

1 ACCEPTED SOLUTION

Forgot to add the screenshot of the table:

watkinnc_0-1605418341482.png

 

let
Source = KWHTable,
Custom1 = Table.FromRows(List.Zip({Source[Timestamp], List.Skip(Source[Timestamp], 1), Source[kW saved],List.Skip(Source[kW saved], 1) }), type table [Timestamp = datetime, Timestamp2 = datetime, kW saved = number, kW saved2 = number] ),
#"Added Custom" = Table.AddColumn(Custom1, "kWh saved", each Number.From([Timestamp2] - [Timestamp])*24*(([kW saved2] + [kW saved])/2), type number)
in
#"Added Custom"


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!

View solution in original post

5 REPLIES 5
watkinnc
Super User
Super User

This works:

 

let
Source = KWHTable,
Custom1 = Table.FromRows(List.Zip({Source[Timestamp], List.Skip(Source[Timestamp], 1), Source[kW saved],List.Skip(Source[kW saved], 1) }), type table [Timestamp = datetime, Timestamp2 = datetime, kW saved = number, kW saved2 = number] ),
#"Added Custom" = Table.AddColumn(Custom1, "kWh saved", each Number.From([Timestamp2] - [Timestamp])*24*(([kW saved2] + [kW saved])/2), type number)
in
#"Added Custom"

 

Just paste those steps in, and replace "source" with the name of your last step.

 

---Nate


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!

Forgot to add the screenshot of the table:

watkinnc_0-1605418341482.png

 

let
Source = KWHTable,
Custom1 = Table.FromRows(List.Zip({Source[Timestamp], List.Skip(Source[Timestamp], 1), Source[kW saved],List.Skip(Source[kW saved], 1) }), type table [Timestamp = datetime, Timestamp2 = datetime, kW saved = number, kW saved2 = number] ),
#"Added Custom" = Table.AddColumn(Custom1, "kWh saved", each Number.From([Timestamp2] - [Timestamp])*24*(([kW saved2] + [kW saved])/2), type number)
in
#"Added Custom"


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!
wdx223_Daniel
Super User
Super User

ColumnC =
VAR _CurrentTime =
    MAX ( 'Table'[Timestamp] )
VAR _CurrentKw =
    MAX ( 'Table'[kW saved] )
VAR _LastTime =
    CALCULATE ( MAX ( 'Table'[Timestamp] ), 'Table'[Timestamp] < _CurrentTime )
VAR _LastKw =
    CALCULATE ( SUM ( 'Table'[kW saved] ), 'Table'[Timestamp] = _LastTime )
RETURN
    IF (
        _LastTime,
         ( _CurrentTime - _LastTime ) * 24 * ( _CurrentKw + _LastKw ) / 2
    )

@Kahraxcheck this code

Thank you @wdx223_Daniel , this is getting me much closer. Atleast I get an output with no syntax errors 😀

There are however still some snags. Did some debugging. 

 

Outputting only the kW calculation

 

 

IF (
        _LastTime,
    //   ( _CurrentTime - _LastTime ) * 24 * 
         ( _CurrentKw + _LastKw ) / 2
    )

 

 

 image.png
First row should be 0 as it has no previous row to calculate agaist. Second row should be (26,02+13,27)/2 = 19,65. All rows after that is correct.  Edit: Rest of the rows are not correct. Turns out it is the same issue as with the dates. _CurrentkW is fixed at the largest value in the dataset instead of just taking the actual current value. 

After running the complete code (nothing commented out) I get the following results. 

image.png

After some checking it turns out that the variable _currentTime remains fixed at the largest date in the dataset which is '31.10.2020 15:12:56'. So for the second row (first row should be 0) it is calculating the difference between 31.10.2020 15:12:56 and 01.11.2019 08:47:02. This turnst out to be 8781 hours. While infact it should calcualte two adjacent values, and the answer should be 8,78 hours (difference between 01.11.2019 00:00:00 and 01.11.2019 08:47:02). 

 

So, I'm allmost there 🙂 Any ideas for the last issues?

@Kahrax guess you are creating a calculated column, but my code is a measure formula. This might work in calculated column

ColumnC =
VAR _CurrentTime =
    'Table'[Timestamp] 
VAR _CurrentKw =
     'Table'[kW saved] 
VAR _LastTime =
     MAXX (FILTER(Table, 'Table'[Timestamp] < _CurrentTime ), 'Table'[Timestamp] )
VAR _LastKw =
    SUMX (FILTER(Table, 'Table'[Timestamp] = _LastTime ), 'Table'[kW saved] )
RETURN
    IF (
        _LastTime,
         ( _CurrentTime - _LastTime ) * 24 * ( _CurrentKw + _LastKw ) / 2
    )

 

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

Check out the September 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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

Top Solution Authors