Hi @pete962,
I hope this works for you.
First created a supporting query called: ListMonths for the dynamic date range.
let
Today = Date.From(DateTime.FixedLocalNow()),
FirstDate = Date.StartOfMonth( Date.AddMonths( Today, -12 )),
LastDate = Date.StartOfMonth( Date.AddMonths( Today, -1 )),
Source = List.Generate( ()=> FirstDate, each _ <= LastDate, each Date.AddMonths(_, 1))
in
Source
and next for your monthly demand table
let
Source = Excel.Workbook(File.Contents(FileLocation), null, true),
tData_Table = Source{[Item="tData",Kind="Table"]}[Data],
ChType = Table.TransformColumnTypes(tData_Table,{{"DATAAREAID", type text}, {"SITEID", type text}, {"WAREHOUSEID", type text}, {"DEMANDDATE", type date}, {"DEMANDQUANTITY", Int64.Type}, {"ITEMNUMBER", type text}, {"PARTITION", Int64.Type}, {"RECID", Int64.Type}}),
GroupItem = Table.Group(ChType, {"ITEMNUMBER"}, {{"AllRows", each _, type table [DATAAREAID=nullable text, SITEID=nullable text, WAREHOUSEID=nullable text, DEMANDDATE=nullable date, DEMANDQUANTITY=nullable number, ITEMNUMBER=nullable text, PARTITION=nullable number, RECID=nullable number]}}),
AddedCustom = Table.AddColumn(GroupItem, "Temp", each
if Table.RowCount([AllRows])=12 then [AllRows] else
let
myTable = [AllRows],
GroupDemand = Table.Group(myTable, {"DEMANDDATE"}, {{"AllRows2", each _, type table [DATAAREAID=nullable text, SITEID=nullable text, WAREHOUSEID=nullable text, DEMANDDATE=nullable date, DEMANDQUANTITY=nullable number, ITEMNUMBER=nullable text, PARTITION=nullable number, RECID=nullable number]}}),
AddRows = List.Difference(ListMonths, GroupDemand[DEMANDDATE]),
ToTable = Table.FromList(AddRows, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
AddTables = Table.AddColumn(ToTable, "Custom", each #table( Table.ColumnNames(myTable), {})),
MatchNames = Table.RenameColumns( AddTables, List.Zip( {Table.ColumnNames( AddTables ), Table.ColumnNames( GroupDemand)} )),
CombineData = Table.Combine({GroupDemand,MatchNames} ),
ExpandGroupDemand = Table.ExpandTableColumn(CombineData, "AllRows2", {"DATAAREAID", "SITEID", "WAREHOUSEID", "DEMANDQUANTITY", "ITEMNUMBER", "PARTITION", "RECID"}, {"DATAAREAID", "SITEID", "WAREHOUSEID", "DEMANDQUANTITY", "ITEMNUMBER", "PARTITION", "RECID"}),
FillDown = Table.FillDown(ExpandGroupDemand,{"DATAAREAID", "SITEID", "WAREHOUSEID", "ITEMNUMBER", "PARTITION", "RECID"})
in
FillDown, type table [DATAAREAID=nullable text, SITEID=nullable text, WAREHOUSEID=nullable text, DEMANDDATE=nullable date, DEMANDQUANTITY=nullable number, ITEMNUMBER=nullable text, PARTITION=nullable number, RECID=nullable number] )[[Temp]],
ExpandedTemp = Table.ExpandTableColumn(AddedCustom, "Temp", {"DATAAREAID", "SITEID", "WAREHOUSEID", "DEMANDDATE", "DEMANDQUANTITY", "ITEMNUMBER", "PARTITION", "RECID"}, {"DATAAREAID", "SITEID", "WAREHOUSEID", "DEMANDDATE", "DEMANDQUANTITY", "ITEMNUMBER", "PARTITION", "RECID"})
in
ExpandedTemp
With this result.
You can easily replace the null with 0 should that be required.
Here’s your sample file. eDNA - Generate Missing Months.pbix (89.0 KB)
I hope this is helpful.