Power Query: How to fill in missing dates in inventory with multiple columns?

Willem2904

New Member
Joined
Apr 4, 2018
Messages
6
I'm working on combining a number of tables with inventory numbers and dates into a single table using power query.

If this is my table, the code below works:
DateCountItem
01.jan​
100​
A
02.jan​
200​
A
02.jan​
250​
B
03.jan​
300​
A
03.jan​
350​
B
06.jan​
600​
A

let
SourceTable = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], //assumes you have a table in your workbook named "Table1", which is your "SOURCE" table
Types = Table.TransformColumnTypes(SourceTable,{{"Date", type date}, {"Count", Int64.Type}, {"Item", type text}}),
Pivot = Table.Pivot(Types, List.Distinct(Types[Item]), "Item", "Count"), //pivot our item field so we have a unique field for each item

//these steps generate a continuous list of dates from the first date to the last date in your source table
FirstDate = List.First(SourceTable[Date]),
LastDate = List.Last(SourceTable[Date]),
NumberList = {Number.From(FirstDate)..Number.From(LastDate)},
ListToTable = Table.FromList(NumberList, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
BackToDate = Table.TransformColumnTypes(ListToTable,{{"Column1", type date}}),
Rename = Table.RenameColumns(BackToDate,{{"Column1", "FullDateList"}}),

//using the list of dates as the "source", left join our pivoted source table
JoinTable1 = Table.NestedJoin(Rename, {"FullDateList"}, Pivot, {"Date"}, "Pivot", JoinKind.LeftOuter),
ExpandTable1 = Table.ExpandTableColumn(JoinTable1,
"Pivot",
Table.ColumnNames(Pivot),
Table.ColumnNames(Pivot)
), //note we don't refer to any column names here or in the next step other then the ones that won't change
FillDownItems = Table.FillDown(ExpandTable1,
List.Difference(Table.ColumnNames(ExpandTable1), {"FullDateList", "Date"})
),
RemoveOldDateField = Table.RemoveColumns(FillDownItems,{"Date"}),
UnpivotBack = Table.UnpivotOtherColumns(RemoveOldDateField, {"FullDateList"}, "Item", "Count")
in
UnpivotBack

1625660296685.png

As you can see, the data is calculated correctly.

However, what do I need to do if I add another column, e.g. Location to the table?
DateCountItemLocation
01.jan​
100​
AX
02.jan​
200​
AX
02.jan​
250​
BY
03.jan​
300​
AY
03.jan​
350​
BZ
06.jan​
600​
AZ

I want the result to look like this:
DateItemLocationCount
01.janAX
100​
02.janAX
200​
03.janAX
200​
04.janAX
200​
05.janAX
200​
06.janAX
200​
01.janAY
0​
02.janAY
0​
03.janAY
300​
04.janAY
300​
05.janAY
300​
06.janAY
300​
01.janAZ
0​
02.janAZ
0​
03.janAZ
0​
04.janAZ
0​
05.janAZ
0​
06.janAZ
600​
01.janBY
0​
02.janBY
250​
03.janBY
250​
04.janBY
250​
05.janBY
250​
06.janBY
250​
01.janBZ
0​
02.janBZ
0​
03.janBZ
0​
04.janBZ
0​
05.janBZ
0​
06.janBZ
350​

Thanks!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Dear Willam, I am struggling with a similar problem right now, however I can't fully reconcile your solution above. Especially the step where you expand the merge I get lost somehow. Can you please send the file you used before or explain the steps from "ExpandTable1" in more details? Your support is sooo much appreciated, really struggle here!
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top