Forum Discussion
Insert rows with future months with mean calculus
- 10 months ago
pls see if this is what you want
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc3JDcAgDETRXnxG8go2taD030YCDpKvT18za4EEMqOQGDRQHx2e9uksKjb8KBOyXGWzVFKk0/atNH8dRSVCcre26ppKjiRXzSzyrRdV2gvPCw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DATE = _t, VALUE = _t]),
#"Changed Type with Locale" = Table.TransformColumnTypes(Source, {{"DATE", type date}}, "en-AU"),
#"Changed Type" = Table.TransformColumnTypes(#"Changed Type with Locale",{{"VALUE", Int64.Type}}),
ThisYear = let
MaxYear = List.Max(List.Transform(#"Changed Type"[DATE], Date.Year)),
ThisYear = MaxYear
in
ThisYear,
ThisYearRows = let
ThisYearRows = Table.SelectRows(#"Changed Type", each Date.Year([DATE]) = ThisYear)
in
ThisYearRows,
MeanThisYear = let
MeanThisYear = List.Average(ThisYearRows[VALUE])
in
MeanThisYear,
FutureDates = let
LastDate = List.Max(#"Changed Type"[DATE]),
StartDate = Date.AddMonths(Date.EndOfMonth(LastDate), 1),
FutureDates = List.Transform({0..23}, each Date.EndOfMonth(Date.AddMonths(StartDate, _)))
in
FutureDates,
FutureRowsWithValue = let
FutureRows = Table.FromList(FutureDates, Splitter.SplitByNothing(), {"DATE"}),
FutureRowsWithValue = Table.AddColumn(FutureRows, "VALUE", each MeanThisYear)
in
FutureRowsWithValue,
Custom1 = let
AllRows = Table.Combine({#"Changed Type",FutureRowsWithValue})
in
AllRows
in
Custom1
Hi powerbricco
This can be achieved with the following steps in power query
Get last date of your table:
LastDate = List.Max(Source[DATE])
Get last year:
LastYear = Date.Year(LastDate)
Filter rows for last year only:
LastYearRows = Table.SelectRows(Source, each Date.Year([DATE]) = LastYear)
Calculate Mean:
MeanValue = List.Average(LastYearRows[VALUE])
Generate a list of future months:
StartMonth = Date.AddMonths(Date.StartOfMonth(LastDate), 1),
FutureMonths = List.Transform({1..24}, each Date.EndOfMonth(Date.AddMonths(StartMonth, _ - 1)))
Convert this list to a table:
FutureTable = Table.FromList(FutureMonths, Splitter.SplitByNothing(), {"DATE"}),
FutureTable = Table.AddColumn(FutureTable, "VALUE", each MeanValue)
Combine original table with future table:
FinalTable = Table.Combine({Source, FutureTable})
This should add 24 new rows with dates as the last day of each month and the calculated mean value
--------------------------------
I hope this helps, please give kudos and mark as solved if it does!
Connect with me on LinkedIn.
Subscribe to my YouTube channel for Fabric/Power Platform related content!