Power Query to extract rows based on criteria

ronakchoudhary

New Member
Joined
Jul 28, 2020
Messages
7
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
Hi there,

I have two data sets - one has total quantity and one has all invoice details (Invoice number, Quantity, Date). I need to extract all invoices which comprise of the quantity on hand. Data set as below:

Table 1 - Quantity on Hand:

Item NumberItem DescriptionGSV QTY
00000429a
1​
00000584b
4​
0006648-801c
6​
00076d
1​
00083-002e
331​
00107-072-001f
4​

Table 2 - Invoice Data:

Item NumberItem DescriptionGRN RefGRN DateGRN Qty
00000429a43
03-Jul-13​
1​
00000584b73
16-Dec-09​
3​
00000584b22
11-Dec-09​
13​
00000584b93
22-Jan-10​
13​
00000584b20
02-Mar-10​
26​
00000584b54
10-Jun-10​
13​
00000584b23
10-Jun-10​
13​
00000584b26
22-Jul-10​
10​
00000584b82
02-Sep-10​
1​
00000584b55
02-Sep-10​
1​
00000584b30
02-Sep-10​
1​
00000584b62
02-Sep-10​
1​
00000584b95
02-Sep-10​
1​
00000584b30
02-Sep-10​
1​
00000584b67
02-Sep-10​
1​
00000584b71
02-Sep-10​
1​
00000584b27
02-Sep-10​
1​
00000584b49
02-Sep-10​
1​
00000584b43
22-Sep-10​
1​
00000584b17
22-Sep-10​
1​
00000584b88
22-Sep-10​
1​
00000584b91
22-Sep-10​
1​
00000584b12
22-Sep-10​
1​
00000584b24
22-Sep-10​
1​
00000584b81
22-Sep-10​
1​
00000584b40
22-Sep-10​
1​
00000584b41
22-Sep-10​
1​
00000584b25
22-Sep-10​
1​
00000584b25
22-Sep-10​
1​
00000584b86
22-Sep-10​
1​
00000584b49
22-Sep-10​
1​
00000584b83
22-Sep-10​
1​
00000584b27
22-Sep-10​
1​
00000584b43
22-Sep-10​
1​
00000584b41
22-Sep-10​
1​
00000584b39
22-Sep-10​
1​
00000584b37
22-Sep-10​
1​
00000584b31
22-Sep-10​
1​
00000584b71
06-Nov-10​
1​
00000584b43
06-Nov-10​
1​
00000584b41
06-Nov-10​
1​
00000584b79
06-Nov-10​
1​
00000584b44
06-Nov-10​
1​
00000584b30
06-Nov-10​
1​
00000584b27
06-Nov-10​
1​
00000584b87
06-Nov-10​
1​
00000584b88
06-Nov-10​
1​
00000584b32
06-Nov-10​
1​
00000584b4
06-Nov-10​
1​
00000584b36
06-Nov-10​
1​
00000584b13
06-Nov-10​
1​
0006648-801c59
07-Dec-06​
1​
0006648-801c54
05-Feb-07​
1​
0006648-801c65
22-Sep-06​
1​
0006648-801c88
19-Aug-07​
1​
0006648-801c71
19-Aug-07​
1​
0006648-801c30
11-Feb-05​
1​
0006648-801c61
05-Aug-10​
1​
0006648-801c38
05-Aug-10​
1​
0006648-801c65
26-Dec-10​
1​
0006648-801c13
26-Dec-10​
1​
00076d68
22-Jun-14​
1​
00083-002e96
16-Jul-06​
30​
00083-002e63
23-Jun-06​
30​
00083-002e4
31-Jul-06​
30​
00083-002e84
12-Nov-05​
58​
00083-002e52
01-Nov-05​
62​
00083-002e89
03-Sep-06​
26​
00083-002e19
19-Apr-07​
50​
00083-002e34
06-Mar-07​
40​
00083-002e38
19-Sep-06​
174​
00083-002e25
11-Feb-07​
34​
00083-002e46
03-Dec-06​
34​
00083-002e26
29-Dec-06​
116​
00083-002e79
30-Apr-07​
26​
00083-002e0
13-May-06​
10​
00083-002e83
06-Sep-05​
20​
00083-002e43
27-Feb-05​
30​
00083-002e31
30-Apr-05​
30​
00083-002e59
13-Feb-08​
21​
00083-002e43
05-May-08​
40​
00083-002e41
26-May-08​
27​
00083-002e81
21-Nov-07​
52​
00083-002e64
25-Nov-07​
140​
00083-002e35
06-Jan-08​
87​
00083-002e13
02-Jun-08​
700​
00083-002e33
04-Jul-08​
28​
00083-002e31
21-Jul-08​
172​
00083-002e14
07-May-10​
50​
00083-002e7
14-Jun-10​
75​
00083-002e11
25-Jul-10​
100​
00083-002e77
10-Nov-10​
60​
00083-002e63
25-Nov-10​
40​
00083-002e9
02-Apr-11​
21​
00083-002e91
28-Apr-11​
79​
00083-002e67
28-Jun-11​
49​
00083-002e55
10-Jul-11​
51​
00083-002e22
09-Aug-11​
52​
00083-002e24
13-Aug-11​
48​
00083-002e36
06-Apr-12​
150​
00083-002e46
19-Jul-12​
100​
00083-002e10
10-Aug-12​
100​
00083-002e63
27-Jan-13​
200​
00083-002e79
11-May-13​
26​
00083-002e54
31-May-13​
100​
00083-002e45
10-Jun-13​
34​
00083-002e59
14-Sep-13​
36​
00083-002e85
24-Sep-13​
44​
00083-002e72
28-Oct-13​
80​
00083-002e22
22-Jun-14​
17​
00083-002e78
26-Jun-14​
83​
00083-002e84
24-Nov-14​
100​
00107-072-001f95
22-Feb-14​
4​
00107-072-001f92
31-Oct-16​
2​
00107-072-001f74
28-Jul-19​
2​
00107-072-001f20
07-Mar-20​
2​

Note that total invoice quantity can be higher than, equal to or lower than quantity on hand.

If total quantity in various invoices is equal to quantity on hand, data should pick up all invoices.
If total quantity in various invoices is lower than quantity on hand, data should pick up all invoices.
If total quantity in various invoices is higher than quantity on hand, data should pick up latest invoices (based on date) which make up the quantity on hand.

Can someone help me with how to do this in M Query?

Thanks in advance!!
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
is that what you want?

Item NumberItem DescriptionGSV-QtyGRN-QtyMaxDate
429a1103/07/2013
584b18413006/11/2010
0006648-801c601026/12/2010
76d1122/06/2014
00083-002e16550376224/11/2014
00107-072-001f161007/03/2020
 
Last edited:
Upvote 0
is that what you want?

Item NumberItem DescriptionGSV-QtyGRN-QtyMaxDate
429a1103/07/2013
584b18413006/11/2010
0006648-801c601026/12/2010
76d1122/06/2014
00083-002e16550376224/11/2014
00107-072-001f161007/03/2020

Hey Sandy,

Thanks a lot for your reply. I actually need the list of invoices with respective purchase dates and not just the summary.
 
Upvote 0
Item NumberItem DescriptionGSV QTYGRN RefGRN DateGRN Qty
429a14303/07/20131
584b47316/12/20093
584b42211/12/200913
584b49322/01/201013
584b42002/03/201026
584b45410/06/201013
584b42310/06/201013
584b42622/07/201010
584b48202/09/20101
584b45502/09/20101
584b43002/09/20101
584b46202/09/20101
584b49502/09/20101
584b46702/09/20101
584b47102/09/20101
584b42702/09/20101
584b44902/09/20101
584b44322/09/20101
584b41722/09/20101
584b48822/09/20101
584b49122/09/20101
584b41222/09/20101
584b42422/09/20101
584b48122/09/20101
584b44022/09/20101
584b44122/09/20101
584b42522/09/20101
584b48622/09/20101
584b44922/09/20101
584b48322/09/20101
584b42722/09/20101
584b43922/09/20101
584b43722/09/20101
584b43122/09/20101
584b47106/11/20101
584b44306/11/20101
584b44106/11/20101
584b47906/11/20101
584b44406/11/20101
584b43006/11/20101
584b42706/11/20101
584b48706/11/20101
584b48806/11/20101
584b43206/11/20101
584b4406/11/20101
584b43606/11/20101
584b41306/11/20101
0006648-801c65907/12/20061
0006648-801c65405/02/20071
0006648-801c66522/09/20061
0006648-801c68819/08/20071
0006648-801c67119/08/20071
0006648-801c63011/02/20051
0006648-801c66105/08/20101
0006648-801c63805/08/20101
0006648-801c66526/12/20101
0006648-801c61326/12/20101
76d16822/06/20141
00083-002e3319616/07/200630
00083-002e3316323/06/200630
00083-002e331431/07/200630
00083-002e3318412/11/200558
00083-002e3315201/11/200562
00083-002e3318903/09/200626
00083-002e3311919/04/200750
00083-002e3313406/03/200740
00083-002e3313819/09/2006174
00083-002e3312511/02/200734
00083-002e3314603/12/200634
00083-002e3312629/12/2006116
00083-002e3317930/04/200726
00083-002e331013/05/200610
00083-002e3318306/09/200520
00083-002e3314327/02/200530
00083-002e3313130/04/200530
00083-002e3315913/02/200821
00083-002e3314305/05/200840
00083-002e3314126/05/200827
00083-002e3318121/11/200752
00083-002e3316425/11/2007140
00083-002e3313506/01/200887
00083-002e3311302/06/2008700
00083-002e3313304/07/200828
00083-002e3313121/07/2008172
00083-002e3311407/05/201050
00083-002e331714/06/201075
00083-002e3311125/07/2010100
00083-002e3317710/11/201060
00083-002e3316325/11/201040
00083-002e331902/04/201121
00083-002e3319128/04/201179
00083-002e3316728/06/201149
00083-002e3315510/07/201151
00083-002e3312209/08/201152
00083-002e3312413/08/201148
00083-002e3313606/04/2012150
00083-002e3314619/07/2012100
00083-002e3311010/08/2012100
00083-002e3316327/01/2013200
00083-002e3317911/05/201326
00083-002e3315431/05/2013100
00083-002e3314510/06/201334
00083-002e3315914/09/201336
00083-002e3318524/09/201344
00083-002e3317228/10/201380
00083-002e3312222/06/201417
00083-002e3317826/06/201483
00083-002e3318424/11/2014100
00107-072-001f49522/02/20144
00107-072-001f49231/10/20162
00107-072-001f47428/07/20192
00107-072-001f42007/03/20202
 
Upvote 0
Item NumberItem DescriptionGSV QTYGRN RefGRN DateGRN Qty
429a14303/07/20131
584b47316/12/20093
584b42211/12/200913
584b49322/01/201013
584b42002/03/201026
584b45410/06/201013
584b42310/06/201013
584b42622/07/201010
584b48202/09/20101
584b45502/09/20101
584b43002/09/20101
584b46202/09/20101
584b49502/09/20101
584b46702/09/20101
584b47102/09/20101
584b42702/09/20101
584b44902/09/20101
584b44322/09/20101
584b41722/09/20101
584b48822/09/20101
584b49122/09/20101
584b41222/09/20101
584b42422/09/20101
584b48122/09/20101
584b44022/09/20101
584b44122/09/20101
584b42522/09/20101
584b48622/09/20101
584b44922/09/20101
584b48322/09/20101
584b42722/09/20101
584b43922/09/20101
584b43722/09/20101
584b43122/09/20101
584b47106/11/20101
584b44306/11/20101
584b44106/11/20101
584b47906/11/20101
584b44406/11/20101
584b43006/11/20101
584b42706/11/20101
584b48706/11/20101
584b48806/11/20101
584b43206/11/20101
584b4406/11/20101
584b43606/11/20101
584b41306/11/20101
0006648-801c65907/12/20061
0006648-801c65405/02/20071
0006648-801c66522/09/20061
0006648-801c68819/08/20071
0006648-801c67119/08/20071
0006648-801c63011/02/20051
0006648-801c66105/08/20101
0006648-801c63805/08/20101
0006648-801c66526/12/20101
0006648-801c61326/12/20101
76d16822/06/20141
00083-002e3319616/07/200630
00083-002e3316323/06/200630
00083-002e331431/07/200630
00083-002e3318412/11/200558
00083-002e3315201/11/200562
00083-002e3318903/09/200626
00083-002e3311919/04/200750
00083-002e3313406/03/200740
00083-002e3313819/09/2006174
00083-002e3312511/02/200734
00083-002e3314603/12/200634
00083-002e3312629/12/2006116
00083-002e3317930/04/200726
00083-002e331013/05/200610
00083-002e3318306/09/200520
00083-002e3314327/02/200530
00083-002e3313130/04/200530
00083-002e3315913/02/200821
00083-002e3314305/05/200840
00083-002e3314126/05/200827
00083-002e3318121/11/200752
00083-002e3316425/11/2007140
00083-002e3313506/01/200887
00083-002e3311302/06/2008700
00083-002e3313304/07/200828
00083-002e3313121/07/2008172
00083-002e3311407/05/201050
00083-002e331714/06/201075
00083-002e3311125/07/2010100
00083-002e3317710/11/201060
00083-002e3316325/11/201040
00083-002e331902/04/201121
00083-002e3319128/04/201179
00083-002e3316728/06/201149
00083-002e3315510/07/201151
00083-002e3312209/08/201152
00083-002e3312413/08/201148
00083-002e3313606/04/2012150
00083-002e3314619/07/2012100
00083-002e3311010/08/2012100
00083-002e3316327/01/2013200
00083-002e3317911/05/201326
00083-002e3315431/05/2013100
00083-002e3314510/06/201334
00083-002e3315914/09/201336
00083-002e3318524/09/201344
00083-002e3317228/10/201380
00083-002e3312222/06/201417
00083-002e3317826/06/201483
00083-002e3318424/11/2014100
00107-072-001f49522/02/20144
00107-072-001f49231/10/20162
00107-072-001f47428/07/20192
00107-072-001f42007/03/20202


Hey Sandy,
Thanks a lot for quick guidance.

I need another help, if possible:

I am applying a custom step for conditional running total as below but it is significantly slowing down my refresh status. I was hoping if you could help me clean this up (buffer command or something) to make it fast. My total data is about 200,000 rows and I have to refresh it every week. I cannot be waiting for 15 mins everytime to refresh it. Could you help on this as well?


let


Source = Table.Buffer(Merged), This step picks up data from another table - if I link the same table, it gives me circular reference error.


AddIndex = Table.AddIndexColumn(Source, "Row", 1, 1), This is adding an index which will be used in the next column.


RunTotal = Table.AddColumn(AddIndex, "Running Total", each let Item=[Item], Row=[Row] in List.Sum(Table.SelectRows(AddIndex, each [Row]<=Row and [Item]=Item)[GRN Qty])), Here is where the actual work happens, basically, if the 'item' is same, it should add the 'GRN Qty' with previous record otherwise start afresh.


RemoveIndex = Table.RemoveColumns(RunTotal,{"Row"})


in


RemoveIndex


Note: I know there is an alternative to use a function for adding running sum, however, my users are novices in power query and I don't want to complicate things for them. If this is the only solution, please let me know.


Appreciate your help and support.
 
Upvote 0
post whole code in code tags without comments
plus
code for merge

code should work with your examples

btw. your example is not representative
If total quantity in various invoices is higher than quantity on hand, data should pick up latest invoices (based on date) which make up the quantity on hand
 
Upvote 0
post whole code in code tags without comments
plus
code for merge

code should work with your examples

btw. your example is not representative


Hi Sandy,

Here you go:

Code:
Power Query:
let

Source = Table.Buffer(Merged),

AddIndex = Table.AddIndexColumn(Source, "Row", 1, 1),

RunTotal = Table.AddColumn(AddIndex, "Running Total", each let Item=[Item], Row=[Row] in List.Sum(Table.SelectRows(AddIndex, each [Row]<=Row and [Item]=Item)[GRN Qty])),

RemoveIndex = Table.RemoveColumns(RunTotal,{"Row"})

in

RemoveIndex

Note that the code is working, its just too slow.

And yes, I noticed that the sample I shared actually did not cover the real problem I am facing. Will try to share something.
 
Upvote 0
where is the code for "Merged"?
Sorry, missed that. Here you go:

Power Query:
let
    Source = Table.NestedJoin(GSV_R, {"Item"}, GRN_R, {"Item Number"}, "GRN_R", JoinKind.LeftOuter),
    #"Expanded GRN_R" = Table.ExpandTableColumn(Source, "GRN_R", {"Alpha", "GRN Ref", "GRN Date", "GRN Qty"}, {"Alpha", "GRN Ref", "GRN Date", "GRN Qty"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded GRN_R",{"Total"}),
    #"Sorted Rows" = Table.Sort(#"Removed Columns",{{"Item", Order.Ascending}, {"GRN Date", Order.Descending}}),
    #"Merged Queries" = Table.NestedJoin(#"Sorted Rows", {"Item"}, GSV_R_Pivot, {"Item"}, "GSV_R_Pivot", JoinKind.LeftOuter),
    #"Expanded GSV_R_Pivot" = Table.ExpandTableColumn(#"Merged Queries", "GSV_R_Pivot", {"Comments"}, {"Comments"})
in
    #"Expanded GSV_R_Pivot"
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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