Merging rows based on dates (multiple rows have same date but different data)

isafu

New Member
Joined
Sep 13, 2022
Messages
4
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
POWER QUERY HELP ENGLISH.jpg

Hi, I'm very new at using Power Query, I would appreciate any pointers / guide to solve this case

Data source are two different table, purchase and production

I need to make a report table based on date, as shown on the bottom right table


What I have tried:

Using unpivot

Result:

Expression.Error: There weren't enough elements in the enumeration to complete the operation

Thank you!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
In your 'Results that I need' table you show Biscuits paired with Target and Bread paired with WalMart. Could you explain why it is that way around and not Bread paired with Target and Biscuits paired with WalMart?

There is nothing in either table to clearly identify which rows should be paired in that way, which is why your attempt has split the results over 2 rows each time.
 
Upvote 0
In your 'Results that I need' table you show Biscuits paired with Target and Bread paired with WalMart. Could you explain why it is that way around and not Bread paired with Target and Biscuits paired with WalMart?

There is nothing in either table to clearly identify which rows should be paired in that way, which is why your attempt has split the results over 2 rows each time.
Hi, thank you for responding!

The location of purchase is not important, I just need it for a label for this report.

August 2nd has BREAD and COOKIE
But only 1 purchase of flour at TARGET

The flour purchase could be merged to either of the August 2nd entry, second entry's purchase and location column would be simply blank

The opposite is true, as seen in August 3rd

Please let me know if the description is unclear, as English is not my first language

Originally the report was typed manually, I am trying to automate the process, but I was not allowed to change the report format ("Result that I need" table)

Do you have any suggestion to simplify the process?
 
Upvote 0
I certainly don't see any way of doing it in power query, for that you would need a unique common key to match both tables.

I'll set up a test sheet and try a few things but I suspect that you will have to rethink your method of getting your final result.
 
Upvote 0
I certainly don't see any way of doing it in power query, for that you would need a unique common key to match both tables.

I'll set up a test sheet and try a few things but I suspect that you will have to rethink your method of getting your final result.

I have been trying to use a help column of some sort, is it possible to assign a number to the first occurring date on both the PURCHASE and PRODUCTION table?
So it can be the unique common key to merge, as the order is not important, only the date matters
 
Upvote 0
I was trying something similar after my last reply but had no success, I don't think that PQ is capable of merging the data in the way that you want to.

I was going to ask another member that I know is extremely good at doing impossible things with PQ to have a look at your question, but they have unfortunately been banned from this forum and don't appear to be active on any of the other forums where I have seen them posting previously.

Does it specifically have to be PQ, or are you open to alternative methods (either vba, or formulas if the real tables are not too large)?
 
Upvote 0
I was trying something similar after my last reply but had no success, I don't think that PQ is capable of merging the data in the way that you want to.

I was going to ask another member that I know is extremely good at doing impossible things with PQ to have a look at your question, but they have unfortunately been banned from this forum and don't appear to be active on any of the other forums where I have seen them posting previously.

Does it specifically have to be PQ, or are you open to alternative methods (either vba, or formulas if the real tables are not too large)?
Thank you for going so far to help

I am open to trying other solution!

PQ is just the first thing to mind because I have been using it to work with most reports.

The real tables wouldn't be too large, as it will be a monthly report, already filtered into each category without messing with my main database
 
Upvote 0
I'm not giving up on PQ just yet, it's getting closer but not quite there.
PQ test.xlsx
ABCDEFGH
1CodeDateProductQty UsedTable2.CodeTable2.DateTable2.Purchase QtyTable2.Location
2101/08/2022Bread30101/08/2022100WalMart
3101/08/2022Biscuits10101/08/202250Target
4102/08/2022Cookies15102/08/202250Target
5103/08/2022Bread30103/08/202220WalMart
6103/08/202220Target
7102/08/2022Brownies10
Merge1
 
Upvote 0
I'm still getting no closer to resolving this in PQ (don't like being defeated but it's getting close). I've asked another member who has better knowledge of PQ if they would be kind enough to take a look at this for you.

Mini sheet version of Tables A and B for testing purposes.
PQ test.xlsx
ABCDEFGHIJK
1KeyCodeDatePurchase QtyLocationKeyCodeDateProductQty Used
21-44774-1101/08/2022100WalMart1-44774-1101/08/2022Bread30
31-44774-2101/08/202250Target1-44774-2101/08/2022Biscuits10
41-44775-1102/08/202250Target1-44775-1102/08/2022Cookies15
51-44776-1103/08/202220WalMart1-44775-2102/08/2022Brownies10
61-44776-2103/08/202220Target1-44776-1103/08/2022Bread30
Sheet10
Cell Formulas
RangeFormula
A2:A6,G2:G6A2=[@Code]&"-"&[@Date]&"-"&COUNTIF(INDEX([Date],1):[@Date],[@Date])
 
Upvote 0
Power Query:
let
    Purch = Excel.CurrentWorkbook(){[Name="Purchase"]}[Content],
    Prod = Excel.CurrentWorkbook(){[Name="Product"]}[Content],
    tbl1 = Prod & Purch,
    Group = Table.Group(tbl1, {"Date"}, {{"All", each _}}),
    tbl2 = Table.AddColumn(Group, "Custom", each let lst = List.RemoveNulls([All][Purchase Qty]) in lst & List.Repeat({null}, Table.RowCount([All]) - List.Count(lst))),
    tbl3 = Table.AddColumn(tbl2, "Custom1", each let lst = List.RemoveNulls([All][Location]) in lst & List.Repeat({null}, Table.RowCount([All]) - List.Count(lst))),
    tbl4 = Table.AddColumn(tbl3, "Composite", each 
            Table.SelectRows(Table.FromColumns(List.FirstN(Table.ToColumns([All]), 4) & {[Custom]} &{[Custom1]}, Table.ColumnNames([All])), each List.Count(List.RemoveNulls(Record.ToList(_))) <>2)),
    tbl5 = Table.RemoveColumns(tbl4,{"All", "Custom", "Custom1"}),
    tbl6 = Table.ExpandTableColumn(tbl5, "Composite", {"Code", "Product", "Qty Used", "Purchase Qty", "Location"} ),
    tbl7 = Table.TransformColumnTypes(tbl6,{{"Date", type date}}),
    Result = Table.ReorderColumns(tbl7,{"Code", "Date", "Product", "Qty Used", "Purchase Qty", "Location"})
in
    Result

Book1
ABCDEFGHIJKLMNOPQ
1CodeDatePurchase QtyLocationCodeDateProductQty UsedCodeDateProductQty UsedPurchase QtyLocation
218/1/2022100WalMart18/1/2022Bread3018/1/2022Bread30100WalMart
318/1/202250Target18/1/2022Biscuits1018/1/2022Biscuits1050Target
418/2/202250Target18/2/2022Cookies1518/2/2022Cookies1550Target
518/3/202220WalMart18/2/2022Brownies1018/2/2022Brownies10
618/3/202220Target18/3/2022Bread3018/3/2022Bread3020WalMart
718/3/202220Target
8
Sheet6
 
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,451
Members
452,514
Latest member
cjkelly15

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