Forum Discussion
Power Query doesn't use 100% of the processor
Hello,
In this example I'm using Power Query in Excel, but I gess it whould be the same on PowerBI.
I have currently a very complex query taking an average of 30 minuts to execute, while my processor (Intel I3 10th gen) is at 20% and my memory at 600Mo (8Go in total, 2Go still available).
I am wondering if there is a way to tell Power Query to use all the capacity it needs? Currently it's running really slowly, while I have a lot of unused capacity on my computer.
I don't have any recursive tasks in my query, all rows could theorically be processed at the same time (that's why I don't think it's a logical bottleneck).
The screenshot of my task manager (columns are: process name, processor, memory, hard disk, network):
Thank you for your help.
Alexandre
I think I understand your requirements now. I'm pretty sure you can achieve some performnce improvements by leveraging Group, still, but there are a couple extra steps. To test performance better, I switched to randomly generated data to test 10k and 100k rows in the structure you specified above for your testing.
The below works pretty well, just takes 1-2 seconds to load. Approach is to merge grouped rows (when grouped on a column, that column becomes primary key, which improve join performance), filter merged grouped rows as needed, sum values for running total, then do a second group to get the min:
let Source = PerfTest_10k, MaterialGroups = Table.Group( Source, {"Material"}, {{ "Current qty", each _, type table [Material=nullable text, Date=nullable date, Stock movement qty=nullable number] }} ), MergeGroups = Table.NestedJoin( Source, "Material", MaterialGroups, "Material", "Groups", JoinKind.Inner ), ExpandGroups = Table.ExpandTableColumn(MergeGroups, "Groups", {"Current qty"}, {"Current qty"}), GetCurQtyRows = Table.TransformRows( ExpandGroups, (row)=> Record.TransformFields( row, { "Current qty", each let _t = Table.SelectRows( row[Current qty], each [Date] <= row[Date] ) in List.Sum( Table.Column(_t, "Stock movement qty") ) } ) ), GetCurQty = Table.FromRecords( GetCurQtyRows, type table [Material=text, Date=date, Stock movement qty=number, Current qty=number] ), GetMinQty = Table.Group( GetCurQty, {"Material"}, { { "Min qty", each List.Min([Current qty]), type number } } ) in GetMinQtyOutput:
The above doesn't work so great when you up the rows to 100k, though. For that I think you have to turn to DAX. This takes about 2 sec to work over 100k rows (probably there are ways to improve performance further on this). Note that [Running Total] and [Min Running Total] are measures:
Running Total = VAR _thisDt = MAX( PerfTest_100k[Date] ) VAR _matGroup = CALCULATETABLE( PerfTest_100k, REMOVEFILTERS( PerfTest_100k ), VALUES( PerfTest_100k[Material] ) ) VAR _curPrevRows = FILTER( _matGroup, PerfTest_100k[Date] <= _thisDt ) RETURN CALCULATE( SUM( PerfTest_100k[Stock movement qty] ), _curPrevRows ) Min Running Total = MINX( SUMMARIZE( PerfTest_100k, PerfTest_100k[Material], PerfTest_100k[Date] ), [Running Total] )Output (note it's all randomly generated which is why these numbers don't match output above):
In case interested and to show my work, here is the M for the test data. Below generates 10k rows for
PerfTest_10k. It's same code, but 10000 replaced with 100000 in line 4, for PerfTest_100k:
let Source = List.Generate( ()=>0, each _ < 10000, each _ + 1, each [ Material = Character.FromNumber( List.Min( { Int32.From( Number.RandomBetween(65, 91) ), // A-Z 90 } ) ), Date = Date.AddDays( #date(2022,1,1), List.Min({ Int32.From( Number.RandomBetween( 0, 365 ) ), // 1/1/2022-12/31/2022 364 } ) ), Stock movement qty = Int64.From( Number.RandomBetween( -100, 100 ) ) // -100 - +100 ] ), Ouput = Table.FromRecords( Source, type table [Material=text,Date=date,Stock movement qty=number] ) in Ouput
18 Replies
- EhrenMicrosoft Employee
There are a few ways to do this without recursion, but some of them involve repeatedly summing all the previous rows, which can be inefficient.
Here's an approach that only scans the data once.
let Source = ..., #"Sorted Rows" = Table.Sort(Source,{{"Date", Order.Ascending}}), #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type), RunningTotal = List.Generate( () => [Index = 0, Total = #"Added Index"{0}[Stock movement qty], Min = Total], each [Index] < Table.RowCount(#"Added Index"), (previous) => let newIndex = previous[Index] + 1 in [ Index = newIndex, Total = previous[Total] + #"Added Index"{newIndex}[Stock movement qty], Min = List.Min({previous[Min], Total}) ]), Custom1 = Table.AddColumn(#"Added Index", "Current qty", each RunningTotal{[Index]}[Total], type number), Custom2 = Table.AddColumn(Custom1, "Min qty", each RunningTotal{[Index]}[Min], type number) in Custom2That said, these kinds of cumulative calculations are probably better done in DAX, after loading the data to the (Power Pivot) Data Model.
- EhrenMicrosoft Employee
You can learn more about creating efficient M running totals here:
https://gorilla.bi/power-query/running-total/#fast-running-totals
- _AlexandreRM_Helper II
Hello Ehren , sorry for the very late reply, I was in vacation.
I think the List.Generate is a good alternative, I never tried to use it with records to store multiple values at each iteration!
After tests, it seems that both solutions (recursivity and list generation) have almost the same (low) performance. I think it's just the fact to iterate manually over each row of a table which is time-consuming. For those who are interested in, here is the formula using List.Generate:
f = (codeTable as table) => let iMax = Table.RowCount(codeTable), qtList = List.Buffer(codeTable[#"Qté entrées/besoins"]), cumulativeStockList = List.Generate( () => [ i = 1, qt = qtList{0}, minQt = qtList{0} ], each [i] <= iMax, each [ i = [i] + 1, qt = [qt] + qtList{[i]}, minQt = List.Min({[qt] + qtList{[i]}, [minQt]}) ]) in List.Last(cumulativeStockList)[minQt],So, except if Microsoft add a way to access more efficiently rows content for custom formulas, I don't think this problem will be solved!
I will also take a look at DAX, but its interface in Excel isn't... user-friendly, let's say.
- EhrenMicrosoft Employee
Just curious: do you see a perf difference if you omit the call to List.Buffer?
- BA_PeteSuper User
Hi _AlexandreRM_ ,
Really hard to give a definitive answer here, but a few things to think about:
- Some of it depends on whether the work you're asking Excel/Power Query to do is serial or parallelable i.e. whether the work has to be done one part after another, or whether it can be threaded across different processing cores. Please don't ask me how to find out if this is the case, I haven't got a clue. It would probably be a question for someone like Ehren.
- If you're using a 32-bit version of Excel, then there's a hard RAM cap of 2GB per Windows process instance which, I believe, would then be subdivided across your evaluation containers that handle your query transformations. In this instance, you could consider upgrading to Excel 64-bit to avoid the cap.
- If you would consider using Power Query within Power BI Desktop, then you could get both the RAM cap gains by using the 64-bit version, as well as super-charge the RAM usage on the evaluation containers by updating the app MaxEvaluationWorkingSetInMB registry entry. More details on that from Chris Webb here:
Pete
- _AlexandreRM_Helper II
Hi BA_Pete , thank you very much for your tips.
- In my opinion all calculations could be paralell. Ehren , I mention you, if you have a clue, here is a simplified version of my query code (function f is a custom function taking a very long time to complete):
let base = Table.Buffer(#"RRP1 + RRP4"), baseMaterials = Table.Distinct(base, {"Numéro de produit", "Désignation du produit"}), baseMaterialsJoinBase = Table.NestedJoin(baseMaterials, {"Numéro de produit"}, base, "Numéro de produit", "join", JoinKind.LeftOuter), bufferJoinTable = Table.TransformColumns(baseMaterialsJoinBase, {"join", Table.Buffer}), addMin = Table.AddColumn(bufferJoinTable, "Qty", each f([join], 0, 9999999999, 0)) in addMin- I just checked in my account data, I am already on a 64bits version,
- This is incredibly powerful, thank you for sharing!!! I defined the parameter in my PowerBI desktop, but I didn't found it in Excel Power Query, it seems Microsoft still didn't pushed this change, sadly.
Alexandre
- EhrenMicrosoft Employee
What is the CPU/memory utilization of the Microsoft.Mashup.Container*.exe processes? They're the ones doing the bulk of the PQ work.
Also, it's possible the processing is slow because of data source access, which wouldn't show up in CPU/memory.
If you remove the addMin step, is everything faster? If so, it would be important to share what the f function does.
- MarkLafSuper User
As far as I can tell from your M, you can get what you want, along with a huge performance improvement, by grouping, which you seem to basically be implementing yourself with your joins, custom function, etc.
Example, given the below test data:
Numéro de produit Désignation du produit Stock movement qty 1 A 12 1 A 32 1 B 543 2 A 123 2 A 421 2 B 51 2 B 36 3 A 8 3 A 21 3 A 43 Through UI:
M:
let Source = base, GroupRows = Table.Group( Source, {"Numéro de produit", "Désignation du produit"}, { {"Min qty", each List.Min([Stock movement qty]), type nullable number} } ) in GroupRowsFYI in case you are trying to do something a little more nuanced than get the minimum by the given grouping, you actually have the whole original table (with only the rows applicable to the grouping for each distict values combo) to manipulate. You'll see if you select All Rows through the UI or add something like to the third argument list of lists in M: { "Grouped Original Table", each _, Value.Type( Source ) }
- _AlexandreRM_Helper II
Hello MarkLaf , thank you for looking for a solution!
Unfortunately, your code doesn't meet my need.
Here is an example of what I want to achieve. The example table down here could be one of the rows from your column "Grouped Original Table".
The result I want to get is in red, and the result of your code is in blue.
Columns in italic aren't in the native table, and are only here to illustrate the logic of the red result.
Material Date Stock movement qty Current qty Min qty A 01/01/2023 +30 30 30 A 01/02/2023 +10 40 30 A 01/03/2023 -15 25 25 A 01/04/2023 -5 20 20 A 01/05/2023 +40 60 20 To achieve this, I need to execute a running total to get the minimum cumulated stock over an ascending list of dates. Your code would have been perfect if I had the "Current qty" column in my native table, which is sadly not the case.
- MarkLafSuper User
I think I understand your requirements now. I'm pretty sure you can achieve some performnce improvements by leveraging Group, still, but there are a couple extra steps. To test performance better, I switched to randomly generated data to test 10k and 100k rows in the structure you specified above for your testing.
The below works pretty well, just takes 1-2 seconds to load. Approach is to merge grouped rows (when grouped on a column, that column becomes primary key, which improve join performance), filter merged grouped rows as needed, sum values for running total, then do a second group to get the min:
let Source = PerfTest_10k, MaterialGroups = Table.Group( Source, {"Material"}, {{ "Current qty", each _, type table [Material=nullable text, Date=nullable date, Stock movement qty=nullable number] }} ), MergeGroups = Table.NestedJoin( Source, "Material", MaterialGroups, "Material", "Groups", JoinKind.Inner ), ExpandGroups = Table.ExpandTableColumn(MergeGroups, "Groups", {"Current qty"}, {"Current qty"}), GetCurQtyRows = Table.TransformRows( ExpandGroups, (row)=> Record.TransformFields( row, { "Current qty", each let _t = Table.SelectRows( row[Current qty], each [Date] <= row[Date] ) in List.Sum( Table.Column(_t, "Stock movement qty") ) } ) ), GetCurQty = Table.FromRecords( GetCurQtyRows, type table [Material=text, Date=date, Stock movement qty=number, Current qty=number] ), GetMinQty = Table.Group( GetCurQty, {"Material"}, { { "Min qty", each List.Min([Current qty]), type number } } ) in GetMinQtyOutput:
The above doesn't work so great when you up the rows to 100k, though. For that I think you have to turn to DAX. This takes about 2 sec to work over 100k rows (probably there are ways to improve performance further on this). Note that [Running Total] and [Min Running Total] are measures:
Running Total = VAR _thisDt = MAX( PerfTest_100k[Date] ) VAR _matGroup = CALCULATETABLE( PerfTest_100k, REMOVEFILTERS( PerfTest_100k ), VALUES( PerfTest_100k[Material] ) ) VAR _curPrevRows = FILTER( _matGroup, PerfTest_100k[Date] <= _thisDt ) RETURN CALCULATE( SUM( PerfTest_100k[Stock movement qty] ), _curPrevRows ) Min Running Total = MINX( SUMMARIZE( PerfTest_100k, PerfTest_100k[Material], PerfTest_100k[Date] ), [Running Total] )Output (note it's all randomly generated which is why these numbers don't match output above):
In case interested and to show my work, here is the M for the test data. Below generates 10k rows for
PerfTest_10k. It's same code, but 10000 replaced with 100000 in line 4, for PerfTest_100k:
let Source = List.Generate( ()=>0, each _ < 10000, each _ + 1, each [ Material = Character.FromNumber( List.Min( { Int32.From( Number.RandomBetween(65, 91) ), // A-Z 90 } ) ), Date = Date.AddDays( #date(2022,1,1), List.Min({ Int32.From( Number.RandomBetween( 0, 365 ) ), // 1/1/2022-12/31/2022 364 } ) ), Stock movement qty = Int64.From( Number.RandomBetween( -100, 100 ) ) // -100 - +100 ] ), Ouput = Table.FromRecords( Source, type table [Material=text,Date=date,Stock movement qty=number] ) in Ouput