• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • SALE 20% Off All Courses
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member
  • Login

Extract specific rows based on criteria in power query|Power Query|Excel Forum|My Online Training Hub

You are here: Home / Extract specific rows based on criteria in power query|Power Query|Excel Forum|My Online Training Hub
                    BLACK FRIDAY SALE 20% OFF ALL COURSES
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumPower QueryExtract specific rows based on crit…
sp_PrintTopic sp_TopicIcon
Extract specific rows based on criteria in power query
Avatar
Ronak Choudhary

New Member
Members
Level 0
Forum Posts: 2
Member Since:
September 1, 2020
sp_UserOfflineSmall Offline
1
September 1, 2020 - 7:12 am
sp_Permalink sp_Print sp_EditHistory

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!

sp_AnswersTopicSeeAnswer See Answer
Avatar
Mynda Treacy
Admin
Level 10
Forum Posts: 4647
Member Since:
July 16, 2010
sp_UserOfflineSmall Offline
2
September 1, 2020 - 10:37 am
sp_Permalink sp_Print

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

sp_AnswersTopicAnswer
Answers Post
Avatar
Ronak Choudhary

New Member
Members
Level 0
Forum Posts: 2
Member Since:
September 1, 2020
sp_UserOfflineSmall Offline
3
September 2, 2020 - 2:46 am
sp_Permalink sp_Print sp_EditHistory

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. 

Avatar
Mynda Treacy
Admin
Level 10
Forum Posts: 4647
Member Since:
July 16, 2010
sp_UserOfflineSmall Offline
4
September 2, 2020 - 8:56 am
sp_Permalink sp_Print sp_EditHistory

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

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1563
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
5
September 29, 2020 - 9:54 am
sp_Permalink sp_Print

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

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online:
Guest(s) 12
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Catalin Bombea: 1864
SunnyKow: 1432
Anders Sehlstedt: 886
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 241
Hans Hallebeek: 225
Jessica Stewart: 219
A.Maurizio: 213
Newest Members:
Robert Corbyn
Tamara Nelson
Phil Collins
Robert Moran
Mark Niemiec
Michael Groh
Paula Holbrook
Russ Cockings
Huub Poppelaars
Pieter Brandsen
Forum Stats:
Groups: 3
Forums: 24
Topics: 6639
Posts: 29131

 

Member Stats:
Guest Posters: 49
Members: 33079
Moderators: 2
Admins: 3
Administrators: Mynda Treacy, Philip Treacy, Jessica
Moderators: Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel Office Scripts
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook

Sign up to our newsletter and join over 400,000
others who learn Excel and Power BI with us.

 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate
  • Sponsor Our Newsletter

Support

  • Contact
  • Forum
  • Helpdesk – For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.