Forum Discussion
Extend Value to all Weeks going forward
Hi, My first post to community. I need help to extend value for all week endings from now. My Week ends on Saturday. First table is my current output from Power query where I have closing stock for each item as of week ending. As you can see there is no value for all week ends going forward. I want to get expected output table below as if there is no value for future weeks from now(), The value should be the previous Closing stock value. I can add the suggested solution as a last step in power query or as formula in excel. I have 960 different Items with this type of data.
- LorenzoSilver Contributor
Thank You Riny_van_Eekelen
And next time TD raises an issue s/he'll wonder why nobody respond 🤔
- Riny_van_EekelenPlatinum Contributor
I prefer to keep things simple and although you have 960 items the solution demonstrated in the attached example should work for you. It askes for some preparation in Excel (create a table with all item numbers and a table with all the week-ending-dates).
See if you can apply the same steps to your actual data.
- LorenzoSilver Contributor
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)
- LorenzoSilver Contributor
Attached is a slightly different approach that might be a bit better in term of perf. (not sure - to be tested...)
- LorenzoSilver 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