Forum Discussion
Extend Value to all Weeks going forward
Hi TD
A Power Query option is attached
// ExpandWeeklyValue - can be shortened
(table as table) as table =>
let
Source = table,
MinDate = Table.Min( Source, {"Week Ending"} )[Week Ending],
MaxDate = Table.Max( Source, {"Week Ending"} )[Week Ending],
Weeks = ( Duration.Days( MaxDate - MinDate ) / 7 ) +1,
DateTable = Table.FromColumns(
{ List.Dates( MinDate, Weeks, #duration(7,0,0,0) ) },
type table [Week Ending = Date.Type]
),
MergedQueries = Table.NestedJoin(
DateTable, {"Week Ending"},
Source, {"Week Ending"},
"Source", JoinKind.LeftOuter
),
Expanded = Table.ExpandTableColumn( MergedQueries, "Source",
{"Item", "Available Value"}
)
in
Table.FillDown( Expanded, {"Item", "Available Value"} )
// Query
let
Source = SourceTable,
GroupedItem = Table.Group( Source, {"Item"},
{"ItemTable", each _,
type table [Week Ending=nullable date, Item=nullable number, Available Value=nullable number]
}
),
RemovedItem = Table.RemoveColumns( GroupedItem, {"Item"} ),
AddedExpand = Table.AddColumn( RemovedItem, "ExpandedItem", each
ExpandWeeklyValue( [ItemTable] ),
type table
),
CombinedExpandedItem = Table.Combine( AddedExpand[ExpandedItem] )
in
CombinedExpandedItem
I have 960 different Items
If you don't have external connections I would suggest you disable Background Data and Ignore the Privacy Levels under CURRENT WORKBOOK in the Query options (if not clear let me know)
(Glad to re-create existing data)
- LorenzoNov 09, 2024Silver Contributor
Attached is a slightly different approach that might be a bit better in term of perf. (not sure - to be tested...)
- LorenzoNov 09, 2024Silver Contributor
Forgot one point. If it happens you have disordered [Week Ending] by [Item] add a Sort step at the end of the query, sorting by [Item] then by [Week Ending]. Something like:
SortedRows = Table.Sort( <previous_step_name>, {{"Item", Order.Ascending}, {"Week Ending", Order.Ascending}} )
Reason this could happen: ExpandWeeklyValue does a Merge operation. The latter cannot guarantee an ordered set of records. This is - to my knowledge - currently undocumented. Same thing can happen with Grouping operation
- TDDec 14, 2024Copper Contributor
Thanks Lorenzo. It worked. Apologies for delay in response. Thank you so much for your help.