Excel and Power Query: PQ is taking hours to execute

%3CLINGO-SUB%20id%3D%22lingo-sub-3436419%22%20slang%3D%22en-US%22%3EExcel%20and%20Power%20Query%3A%20PQ%20is%20taking%20hours%20to%20execute%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3436419%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%20everyone%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20new%20to%20Power%20Query%20and%20not%20sure%20why%20it%20is%20taking%20hours%20to%20execute%20the%20code%20below.%26nbsp%3B%20Because%20the%20data%20is%20sensitive%2C%20I%20am%20unable%20to%20share%20the%20file.%26nbsp%3B%20However%2C%20I%20can%20say%20that%20the%20file%20contains%20300K%2B%20records%2C%20and%20the%20following%20are%20the%20PQ%20queries%20I%20am%20executing%20(note%3A%20variable%20names%20have%20been%20modified)%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3D%20Table.NestedJoin(%23%22Transactions%20%22%2C%20%7B%22Account%20No.%22%7D%2C%20%23%22Orders%22%2C%20%7B%22Acct%22%7D%2C%20%22Orders%22%2C%20JoinKind.LeftOuter)%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3D%20Table.AddColumn(%23%22Transactions%22%2C%20%22Merge%20by%20Date%20Range%22%2C%20each%20Table.SelectRows(%23%22Orders%22%2C%20(x)%20%3D%26gt%3B%20x%5BShip%20Date%5D%20%26gt%3B%3D%20%5B%23%22Post%20Date%20-%2030%22%5D%20and%20x%5BShip%20Date%5D%20%26lt%3B%3D%20%5B%23%22Post%20Date%20%2B%2030%22%5D%20and%20x%5B%23%22Acct%22%5D%20%3D%20%5B%23%22Account%20No.%22%5D))%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3D%20Table.ExpandTableColumn(%23%22Added%20Custom%22%2C%20%22Merge%20by%20Date%20Range%22%2C%20%7B%22Acct%22%2C%20%22Exp%20Type%20Name%22%2C%20%22Product%20Description%22%2C%20%22Units%22%2C%20%22Retail%22%2C%20%22Ship%20Date%22%7D%2C%20%7B%22Acct%22%2C%20%22Exp%20Type%20Name%22%2C%20%22Product%20Description%22%2C%20%22Units%22%2C%20%22Retail%22%2C%20%22Ship%20Date%22%7D)%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3D%20Table.Group(%23%22Expanded%20Merge%20by%20Date%20Range%22%2C%20%7B%22Acct%22%2C%20%22Ship%20Date%22%7D%2C%20%7B%7B%22AllData%22%2C%20each%20_%2C%20type%20table%20%5B%23%22Post%20Date%20-%2030%22%3Dnullable%20date%2C%20%23%E2%80%9DPost%20Date%E2%80%9D%3Dnullable%20date%2C%20%23%22Post%20Date%20%2B%2030%22%3Dnullable%20date%2C%20%23%22%20Account%20No.%22%3Dnullable%20text%2C%20Type%3Dnullable%20text%2C%20Amount%3Dnullable%20number%2C%20Acct%3Dnullable%20text%2C%20Exp%20Type%20Name%3Dnullable%20text%2C%20Product%20Description%3Dnullable%20text%2C%20Units%3Dnullable%20number%2C%20Retail%3Dnullable%20number%2C%20Ship%20Date%3Dnullable%20date%5D%7D%2C%20%7B%22Sum%20of%20Retail%22%2C%20each%20List.Sum(%5BRetail%5D)%2C%20type%20nullable%20number%7D%7D)%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3D%20Table.ExpandTableColumn(%23%22Grouped%20Rows%22%2C%20%22AllData%22%2C%20%7B%22Post%20Date%20-%2030%22%2C%20%22Post%20Date%22%2C%20%22Post%20Date%20%2B%2030%22%2C%20%22Account%20No.%22%2C%20%22Type%22%2C%20%22Amount%22%2C%20%22Acct%22%2C%20%22Exp%20Type%20Name%22%2C%20%22Product%20Description%22%2C%20%22Units%22%2C%20%22Retail%22%2C%20%22Ship%20Date%22%7D%2C%20%7B%22Post%20Date%20-%2030%22%2C%20%22Post%20Date%22%2C%20%22Post%20Date%20%2B%2030%22%2C%20%22%20Account%20No.%22%2C%20%22Account%20Type%22%2C%20%22Amount%22%2C%20%22Acct.1%22%2C%20%22Exp%20Type%20Name%22%2C%20%22Product%20Description%22%2C%20%22Units%22%2C%20%22Retail%22%2C%20%22Ship%20Date.1%22%7D)%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3D%20Table.RemoveColumns(%23%22Expanded%20AllData%22%2C%7B%22Acct%22%2C%20%22Ship%20Date%22%7D)%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3D%20Table.RenameColumns(%23%22Removed%20Columns%22%2C%7B%7B%22Acct.1%22%2C%20%22Account%20No.%22%7D%2C%20%7B%22Ship%20Date.1%22%2C%20%22Ship%20Date%22%7D%7D)%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3D%20Table.Sort(%23%22Renamed%20Columns%22%2C%7B%7B%22Account%20No.%22%2C%20Order.Ascending%7D%2C%20%7B%22Post%20Date%22%2C%20Order.Ascending%7D%2C%20%7B%22Ship%20Date%22%2C%20Order.Ascending%7D%2C%20%7B%22Retail%22%2C%20Order.Descending%7D%7D)%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3EI%20have%20since%20learned%20about%20Table.Buffer%2C%20so%20I%20applied%20it%20to%20every%20statement.%26nbsp%3B%20But%20it%20has%20not%20helped.%26nbsp%3B%20What%20else%20do%20I%20need%20to%20do%20to%20make%20this%20quicker%3F%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3EThank%20you%2C%3C%2FDIV%3E%3CDIV%3ECindy%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3436419%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3436496%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20and%20Power%20Query%3A%20PQ%20is%20taking%20hours%20to%20execute%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3436496%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1138699%22%20target%3D%22_blank%22%3E%40Cindysc1218%3C%2FA%3E%26nbsp%3BIt%20may%20be%20difficult%20to%20judge%2C%20based%20on%20the%20code%20alone%2C%20but%20it%20seems%20that%20in%20the%20second%20step%20you%20add%20a%20column%20with%20filtered%20content.%20If%20I'm%20not%20mistaken%2C%20that%20creates%20a%20column%20with%20300K%20identical%20tables%20of%20filtered%20records%2C%20that%20you%20then%20Expand%20and%20Group.%20Could%20you%20not%20just%20use%20two%20filter%20steps%20(i.e.%20Table.SelectRows)%20and%20end%20with%20something%20like%20this%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EFilter1%20%3D%20Table.SelectRows(%23%22Orders%22%2C%20each%20%5BShip%20Date%5D%20%26gt%3B%3D%20%5B%23%22Post%20Date%20-%2030%22%5D%20and%20%5BShip%20Date%5D%20%26lt%3B%3D%20%5B%23%22Post%20Date%20%2B%2030%22%5D)%2C%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EFilter2%20%3D%20Table.SelectRows(Filter1%2C%20each%20%3C%2FSPAN%3E%3CSPAN%3E%5BAcct%5D%20%3D%20%5B%23%22Account%20No.%22%5D)%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3ENot%20being%20able%20to%20test%20any%20of%20this%2C%20I%20may%20be%20missing%20the%20point%26nbsp%3B%3C%2FSPAN%3Ealtogether.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3436497%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20and%20Power%20Query%3A%20PQ%20is%20taking%20hours%20to%20execute%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3436497%22%20slang%3D%22en-US%22%3ESo%20that%20second%20step%20was%20something%20I%20had%20to%20research%20to%20find%20out%20how%20to%20do%20something%20I%20need.%3CBR%20%2F%3E%3CBR%20%2F%3EWhat%20I%20need%20to%20do%20is%20merge%20(left%20join)%20two%20tables%20using%20account%20and%20the%20dates%20as%20my%20primary%20keys.%20I%20understand%20I%20do%20an%20exact%20match%20on%20accounts%20to%20merge%20the%20tables%2C%20but%20I%20cannot%20do%20an%20exact%20match%20on%20dates%20which%20is%20why%20I%20am%20attempting%20to%20match%20when%20Ship%20Date%20is%20either%20within%2030%20days%20before%20or%2030%20days%20after%20the%20Post%20Date.%20After%20spending%20much%20time%20online%2C%20the%20code%20in%20step%202%20appears%20to%20do%20what%20I%20need%20to%20do.%20Are%20you%20saying%20that%20your%20solution%20can%20do%20that%20the%20same%20thing%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3436502%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20and%20Power%20Query%3A%20PQ%20is%20taking%20hours%20to%20execute%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3436502%22%20slang%3D%22en-US%22%3EThis%20is%20also%20a%20one-to-many%20join.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3436534%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20and%20Power%20Query%3A%20PQ%20is%20taking%20hours%20to%20execute%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3436534%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1138699%22%20target%3D%22_blank%22%3E%40Cindysc1218%3C%2FA%3E%26nbsp%3BI'm%20not%20suggesting%20anything%2C%20but%20merely%20notice%20that%20you%20seem%20to%20add%20a%20column%20with%20identical%20tables%20containing%20filtered%20records%20to%20each%20of%20the%20300K%20rows.%20And%20I'm%20just%20wondering%20why%20you%20don't%20filter%20the%20date%20column%20first%20and%20then%20the%20account%20number.%20But%20as%20said%20that's%20difficult%20to%20judge%20without%20seeing%20the%20data.%20What%20stops%20you%20from%20trying%20on%20a%20copy%20of%20your%20file.%20If%20it%20doesn't%20work%2C%20I%20clearly%20missed%20the%20point.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOr%20perhaps%20you%20could%20just%20add%20a%20column%20stating%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3E%3D%20%5BShip%20Date%5D%20%26gt%3B%3D%20%5B%23%22Post%20Date%20-%2030%22%5D%20and%20x%5BShip%20Date%5D%20%26lt%3B%3D%20%5B%23%22Post%20Date%20%2B%2030%22%5D%20and%20x%5B%23%22Acct%22%5D%20%3D%20%5B%23%22Account%20No.%22%5D%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EThis%26nbsp%3B%3C%2FSPAN%3Eshould%20result%20in%20TRUE%20or%20FALSE.%20After%20that%2C%20filter%20TRUE%20to%20keep%20matching%20rows.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3436632%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20and%20Power%20Query%3A%20PQ%20is%20taking%20hours%20to%20execute%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3436632%22%20slang%3D%22en-US%22%3EI%20hope%20I%20understand%20you%20correctly.%3CBR%20%2F%3E%3CBR%20%2F%3EFor%20your%20first%20suggestion%2C%20I%20filtered%20dates%20in%20regular%20Excel%20before%20using%20PQ.%20For%20your%20second%20suggestion%2C%20doesn't%20that%20mean%20every%20row%20in%20the%20first%20table%20has%20to%20join%20with%20every%20row%20in%20the%20second%20table%20in%20order%20to%20find%20out%20if%20the%20conditional%20is%20true%20or%20not%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3436640%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20and%20Power%20Query%3A%20PQ%20is%20taking%20hours%20to%20execute%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3436640%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1138699%22%20target%3D%22_blank%22%3E%40Cindysc1218%3C%2FA%3E%26nbsp%3BCan't%20answer%20as%20I'm%20having%20a%20hard%20time%20visualizing%20your%20data.%20Sorry.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3436867%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20and%20Power%20Query%3A%20PQ%20is%20taking%20hours%20to%20execute%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3436867%22%20slang%3D%22en-US%22%3ENo%20worries.%20Thank%20you%20for%20the%20suggestions%20anyway.%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi, everyone:

 

I am new to Power Query and not sure why it is taking hours to execute the code below.  Because the data is sensitive, I am unable to share the file.  However, I can say that the file contains 300K+ records, and the following are the PQ queries I am executing (note: variable names have been modified):

 

= Table.NestedJoin(#"Transactions ", {"Account No."}, #"Orders", {"Acct"}, "Orders", JoinKind.LeftOuter)
 
= Table.AddColumn(#"Transactions", "Merge by Date Range", each Table.SelectRows(#"Orders", (x) => x[Ship Date] >= [#"Post Date - 30"] and x[Ship Date] <= [#"Post Date + 30"] and x[#"Acct"] = [#"Account No."]))
 
= Table.ExpandTableColumn(#"Added Custom", "Merge by Date Range", {"Acct", "Exp Type Name", "Product Description", "Units", "Retail", "Ship Date"}, {"Acct", "Exp Type Name", "Product Description", "Units", "Retail", "Ship Date"})
 
= Table.Group(#"Expanded Merge by Date Range", {"Acct", "Ship Date"}, {{"AllData", each _, type table [#"Post Date - 30"=nullable date, #”Post Date”=nullable date, #"Post Date + 30"=nullable date, #" Account No."=nullable text, Type=nullable text, Amount=nullable number, Acct=nullable text, Exp Type Name=nullable text, Product Description=nullable text, Units=nullable number, Retail=nullable number, Ship Date=nullable date]}, {"Sum of Retail", each List.Sum([Retail]), type nullable number}})
 
= Table.ExpandTableColumn(#"Grouped Rows", "AllData", {"Post Date - 30", "Post Date", "Post Date + 30", "Account No.", "Type", "Amount", "Acct", "Exp Type Name", "Product Description", "Units", "Retail", "Ship Date"}, {"Post Date - 30", "Post Date", "Post Date + 30", " Account No.", "Account Type", "Amount", "Acct.1", "Exp Type Name", "Product Description", "Units", "Retail", "Ship Date.1"})
 
= Table.RemoveColumns(#"Expanded AllData",{"Acct", "Ship Date"})
 
= Table.RenameColumns(#"Removed Columns",{{"Acct.1", "Account No."}, {"Ship Date.1", "Ship Date"}})
 
= Table.Sort(#"Renamed Columns",{{"Account No.", Order.Ascending}, {"Post Date", Order.Ascending}, {"Ship Date", Order.Ascending}, {"Retail", Order.Descending}})
 
I have since learned about Table.Buffer, so I applied it to every statement.  But it has not helped.  What else do I need to do to make this quicker?
 
Thank you,
Cindy
7 Replies

@Cindysc1218 It may be difficult to judge, based on the code alone, but it seems that in the second step you add a column with filtered content. If I'm not mistaken, that creates a column with 300K identical tables of filtered records, that you then Expand and Group. Could you not just use two filter steps (i.e. Table.SelectRows) and end with something like this?

 

Filter1 = Table.SelectRows(#"Orders", each [Ship Date] >= [#"Post Date - 30"] and [Ship Date] <= [#"Post Date + 30"]),

Filter2 = Table.SelectRows(Filter1, each [Acct] = [#"Account No."])

 

Not being able to test any of this, I may be missing the point altogether.

So that second step was something I had to research to find out how to do something I need.

What I need to do is merge (left join) two tables using account and the dates as my primary keys. I understand I do an exact match on accounts to merge the tables, but I cannot do an exact match on dates which is why I am attempting to match when Ship Date is either within 30 days before or 30 days after the Post Date. After spending much time online, the code in step 2 appears to do what I need to do. Are you saying that your solution can do that the same thing?
This is also a one-to-many join.

@Cindysc1218 I'm not suggesting anything, but merely notice that you seem to add a column with identical tables containing filtered records to each of the 300K rows. And I'm just wondering why you don't filter the date column first and then the account number. But as said that's difficult to judge without seeing the data. What stops you from trying on a copy of your file. If it doesn't work, I clearly missed the point.

 

Or perhaps you could just add a column stating: 

= [Ship Date] >= [#"Post Date - 30"] and x[Ship Date] <= [#"Post Date + 30"] and x[#"Acct"] = [#"Account No."]

 

This should result in TRUE or FALSE. After that, filter TRUE to keep matching rows.

I hope I understand you correctly.

For your first suggestion, I filtered dates in regular Excel before using PQ. For your second suggestion, doesn't that mean every row in the first table has to join with every row in the second table in order to find out if the conditional is true or not?

@Cindysc1218 Can't answer as I'm having a hard time visualizing your data. Sorry.

No worries. Thank you for the suggestions anyway.