Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Calculating kWh based on datetime and kW

Hi 

 

I want to calculate kWh.

 

I have a table with time stamps and corresponding kW, and I want to convert it to kWh.

 

 

I can imagine I have to calculate some time duration in hours, but I don't know how to go about doing this so that I'm still able to filter on an store ID and a time period.

 

Do anyone have an idea how to do this?

Thank you very much.

  • This turned out to be much more challenging than expected.  Power BI kept running out of memory even though it is running on 32 GB.  I ended up presorting the data in SQL server (and adding the index row there)

     

     

     

     

     

    SELECT 
     ROW_NUMBER() OVER(ORDER BY [Telemetry AssetID ] 
     ,[Telemetry PointName ] 
     ,[Telemetry Timetag ] asc) Row,
    
           [Telemetry SiteID ] [SiteID]
          ,[Telemetry AssetID ] [AssetID]
          ,[Telemetry PointName ] [PointName]
          ,[Telemetry Timetag ] [Timetag]
          ,[Telemetry Units ] [Units]
          ,[Telemetry ELECTRICITY_MAP ] [ELECTRICITY_MAP]
          ,[Telemetry StoreID ] [StoreID]
          ,[Telemetry date_key ] [date_key]
      FROM [Telemetry]

     

     

     

     

     

    Then the calculated column can be written as such: (shown in two variations)

     

     

     

     

    kWh = 
    var i = Telemetry[Row]
    var t = Telemetry[Timetag]
    var ap = Telemetry[AssetID] & Telemetry[PointName]
    var f = filter(Telemetry,Telemetry[Row]=i-1)
    var pt = SELECTCOLUMNS(f,"pt",Telemetry[Timetag])
    var pv = SELECTCOLUMNS(f,"pv",Telemetry[ELECTRICITY_MAP])
    var pap= SELECTCOLUMNS(f,"pap",Telemetry[AssetID] & Telemetry[PointName])
    //var pt = maxx(f,Telemetry[Timetag])
    //var pv = maxx(f,Telemetry[ELECTRICITY_MAP])
    //var pap = maxx(f,Telemetry[AssetID] & Telemetry[PointName])
    return if (ap=pap,divide(Telemetry[ELECTRICITY_MAP]+pv,120)*DATEDIFF(pt,t,MINUTE),0)

     

     

     

     

    and the calculation yields the expected result.

     

     

     

    Thank you for the challenge.  I'm still baffled that Power BI struggles with such a seemingly small dataset (7.8M rows) but I guess there are a few cartesian products happening that explode the memory needs. Hope someone else can find a faster solution.

     

18 Replies

  • Sounds like a typical integral calculus.  Multiply the average of two adjacent data points by the duration between them, that will give you the amount of energy consumed during that period. Then aggregate it up to hourly level.  It gets a little fuzzy because you may not be able to guarantee that a measurement is available exactly at the top of the hour but your data granularity seems good enough to fudge it.

     

    If you want help with the implementation please provide the sample data in usable format.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi lbendlin 

       

      I would very much like some help with the implementation. Seems like the skills needed are a bit over my head. 

       

      How would like the format of a data sample? CSV or a .pbix file?

      • lbendlin's avatar
        lbendlin
        Icon for Super User rankSuper User

        Either is fine but if you already have the pbix started then that would be nicer.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Thanks lbendlin , i used your formula for the calculated column and it works just fine. In my case i had also to calculate from kW to kWh where the readings (timetags) are not evenly distributed (1:1 as in the example above). The problem is now, when i would like to create an incremental refresh, it seems calculated columns doesn't work with incremental refresh. How could i change the calculated column to a measure? Let's assume my table is also called "Telemetry", and i have two columns which are "Timestamp" and "Electricity Map".

     

    I would appreciate your help!

     

    • lbendlin's avatar
      lbendlin
      Icon for Super User rankSuper User

      I don't see a reason why a calculated column would not work with incremental refresh - do you have anything to reproduce that?

      Please provide sanitized sample data that fully covers your issue. If you paste the data into a table in your post or use one of the file services it will be easier to work with. Please show the expected outcome.