Forum Discussion

HopkiJ's avatar
HopkiJ
Frequent Visitor
3 years ago
Solved

Calculate Monthly Usage using multiple filters in Power Query

Hi Everyone, I have built a system to manage my energy in Power Bi but I'm unable to figure out how to manage the manual meter readings within it.  I receive monthly reads on different days across ...
  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi HopkiJ ,

     

    Please follow the steps below:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fY4xCoAwEAS/IqkFk03ucnmElaVYprAV/49ENDlE0u3CMMy6mmU/8+DMaOZ85mNwZYImSxMsynEOZhsfEBUsC1FxwmRDI5USX6VISl5ZfWXLAhQaowT+tb6hXEPhba+UVSlJt5R1aQi90obSXbpd", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Site Name" = _t, #"Meter Ref" = _t, Date = _t, Reading = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Site Name", type text}, {"Meter Ref", type text}, {"Date", type text}, {"Reading", Int64.Type}}),
        #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"Date", type date}}, "fr-DZ"),
        #"Sorted Rows" = Table.Sort(#"Changed Type with Locale",{{"Site Name", Order.Ascending}, {"Meter Ref", Order.Ascending}, {"Date", Order.Ascending}}),
        #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Site Name", "Meter Ref"}, {{"Data", each Table.AddIndexColumn(_,"Index",1,1)}}),
        Custom1 = Table.TransformColumns(#"Grouped Rows",{"Data",(x)=>Table.AddColumn(x,"Monthly Usage",each try (x[Reading]{[Index]}-x[Reading]{[Index]-1}) otherwise null )}),
        #"Expanded Data" = Table.ExpandTableColumn(Custom1, "Data", {"Date", "Monthly Usage"}, {"Date", "Monthly Usage"})
    in
        #"Expanded Data"

    Best Regards,
    Gao

    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

    How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

  • HopkiJ's avatar
    HopkiJ
    3 years ago

    Thank you Gao, the help is greatly appreciated.

    I followed the above and it all looks correct except my "Monthly Usage" values have come out at null

    Can you tell where I have gone wrong? 

     

    = Table.TransformColumnTypes(#"Changed Type2", {{"Date", type date}}, "en-GB")
     

    = Table.Sort(#"Changed Type with Locale",{{"Site Name", Order.Ascending}, {"Meter Ref", Order.Ascending}, {"Date", Order.Ascending}})

     

    = Table.Group(#"Sorted Rows", {"Site Name", "Meter Ref"}, {{"Data", each Table.AddIndexColumn(_,"Index",1,1)}})
     

    = Table.TransformColumns(#"Grouped Rows",{"Data",(x)=>Table.AddColumn(x,"Monthly Usage",each try (x[Reading]{[Index]}-x[Reading]{[Index]-1}) otherwise null )})
     

    = Table.ExpandTableColumn(Custom1, "Data", {"Date", "Monthly Usage"}, {"Date", "Monthly Usage"})