New Member
September 1, 2020
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:
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!
July 16, 2010
Hi Ronak,
Welcome to our forum. Next time, please provide an Excel file containing your data because when I copy and pasted it to Excel I had to waste my time fixing the format of the dates and numbers before I could even look at your question.
You first need to create a running total grouped by invoice number. Then you can merge the Invoices and Stock queries (see Invoices in Stock query) and compare the stock values to the running invoice total and filter out the invoices that exceed stock levels. See my file attached.
Note: there are many invoices with the same date, but there is no way to decide how to sort those invoices. You may want to address this.
I hope that helps.
Mynda
Answers Post
New Member
September 1, 2020
Thanks Mynda, you are a star! My apologies on the noviceness of pasting the data on the post itself.
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 you did give me 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.
July 16, 2010
Hi Ronak,
Please don't re-quote my reply that is immediately above your reply. It wastes everyone's time having to re-read the same information. I've edited your post and removed the re-quote.
There are some Power Query performance tips on this post.
How long does my query take compared to yours without the function? If it's quicker then that is the solution. Your users should only be refreshing the query, not rebuilding it each time, so I'd have thought that no matter how novice they are it should be manageable for them to click a Refresh button.
Mynda
October 5, 2010
Hi Ronak,
See attached file that uses a very fast function to calculate grouped running totals.
On the 'In Stock GRT Function' sheet are the results I think you are after. This is based on the work Mynda did originally.
When functions are incorporated into a query like this, as Mynda's function was, the user doesn't have to know anything about it, they just click on Refresh.
Regards
Phil
1 Guest(s)