Forum Discussion

Kahrax's avatar
Kahrax
Frequent Visitor
5 years ago
Solved

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. 

  • Anonymous's avatar
    Anonymous
    5 years ago

    Forgot to add the screenshot of the table:

     

    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"

5 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    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

    • Anonymous's avatar
      Anonymous
      Not applicable

      Forgot to add the screenshot of the table:

       

      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"

  • wdx223_Daniel's avatar
    wdx223_Daniel
    Community Champion
    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

    • Kahrax's avatar
      Kahrax
      Frequent Visitor

      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
          )

       

       

       
      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. 

      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?

      • wdx223_Daniel's avatar
        wdx223_Daniel
        Community Champion

        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
            )