Trying to group 4 columns by percentage. Running out of memory!

Darkstar_

New Member
Joined
Nov 24, 2018
Messages
13
I'm hoping there is a much easier way of doing what I am doing.

Firstly, this dummy document should illustrate things.

We have our raw data (i've made the data by using another larger sheet and have about 7 queries in excel just to make it look like the first tab in the linked sheet).
I then want to average the results by area for a specific criteria.
For example, Green results are above 70, amber is between 50 and 69 and red is below 50.

I managed to do this but it's messy. I ended up creating nearly 10+ queries to achieve this. The second tab of the linked sheet is a rough outline of my endgame. I couldn't be bothered to do all the results for this dummy document, it took so much time. So I ended up only making Result 1. But I want all 5 results in this table.
When I do make it at work, my computer (works PC's aren't the best) grinds to a halt and throws up out of memory errors.

So to achieve something (I think) should be easy to do, I end up having nearly 30 queries going.
I end up duplicating queries, filter results between 70-100, duplicate, change it and repeat for all 5 results. I then finally merge them. I'm pretty sure there is an easier way of doing this, but currently the way I do it, seems to burn out our computers at work.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Could you describe your algorithm because example excel file does not contain any queries
 
Upvote 0
One approach could be:
Unpivot the results columns.
Create a new conditional column to create the ranking group colour code
Group data by Area, Result, Rank colour code. Aggregate average value.
Load to Data Model
Create Pivot table.

Code perhaps like this. (Note I 'm afraid I haven't bothered to tidy it up).

let
Source = Excel.CurrentWorkbook(){[Name="dataTable"]}[Content],
#"Removed Columns" = Table.RemoveColumns(Source,{"Title"}),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"Result 1", type number}, {"Result 2", type number}, {"Result 3", type number}, {"Result 4", type number}, {"Area", type text}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Area"}, "Result", "Value"),
#"Added Conditional Column" = Table.AddColumn(#"Unpivoted Other Columns", "Rank Group", each if [Value] >= 70 then "Green" else if [Value] >= 50 then "Amber" else if [Value] < 50 then "Red" else null),
#"Reordered Columns" = Table.ReorderColumns(#"Added Conditional Column",{"Area", "Result", "Rank Group", "Value"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Reordered Columns",{{"Rank Group", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type2", {"Area", "Result", "Rank Group"}, {{"Average", each List.Average([Value]), type number}}),
#"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"Area", Order.Ascending}, {"Result", Order.Ascending}})
in
#"Sorted Rows"

Peter
 
Last edited:
Upvote 0
That worked rather well. Thank you.
Just proof that there is a lot I still don't know.
You managed to pretty much do what I wanted in one query rather than the 12 I ended up with.
As a side note if anyone can answer it, how can I improve memory usage? For example, I see the option to add to data model, but I never tick it. What advantages are there in adding it to a data model over just not?
 
Upvote 0
Personally I load the results of the query to the Data Model if the returned table is large and could possibly exceed the limit on the number of rows on a sheet, it's also essential if you want to utilise PowerPivot.

Peter
 
Upvote 0
Thanks.

I realised an error in my original question, or rather an omission.
The conditional column works, but to make things more complex, each result type has a different criteria. For example result 1 green = >80
But result 2 is green=>70 and so forth. This varies slightly for amber and red. Can this idea be expanded to accommodate these differences?
 
Upvote 0
A few ways spring to mind:
Make a cludgy if statement at the added conditional columns step. if result =result 1 etc, if result = result 2 etc.
Create a lookup table for the different criteria in a separate query. Merge the two queries then substitute the column names for the fixed values in the conditional formula.
Use a custom function, but I think it best to leave that if you are not fully familiar with M language yet.
If I get a chance I'll have a quick look later.

Peter
 
Upvote 0
Excel table called "criteriaTable":

ResultRedGreen
Result 15080
Result 24070
Result 33060
Result 42050

<tbody>
</tbody>

Query named "criteriaTable":
let
Source = Excel.CurrentWorkbook(){[Name="criteriaTable"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Result", type text}, {"Red", type number}, {"Green", type number}})
in
#"Changed Type"


Ranking with variable Criteria:
let
Source = Excel.CurrentWorkbook(){[Name="dataTable"]}[Content],
changedType = Table.TransformColumnTypes(Source,{{"Area", type text}}),
removedColumns = Table.RemoveColumns(changedType,{"Title"}),
unpivotOtherCols = Table.TransformColumnTypes(Table.UnpivotOtherColumns(removedColumns, {"Area"}, "Result", "Value"),{{"Value", type number}}),
mergedQueries = Table.NestedJoin(unpivotOtherCols,{"Result"},criteriaTable,{"Result"},"criteriaTable",JoinKind.LeftOuter),
expandedCriteriaTable = Table.ExpandTableColumn(mergedQueries, "criteriaTable", {"Red", "Green"}, {"Red", "Green"}),
conditionalColumn = Table.AddColumn(expandedCriteriaTable, "Ranked Group" as text, each if [Value] >= [Green] then "Green" else if [Value] >= [Red] then "Amber" else if [Value] < [Red] then "Red" else null,type text),
removedCriteria = Table.RemoveColumns(conditionalColumn,{"Red", "Green"}),
sortedResults = Table.Sort(removedCriteria,{{"Area", Order.Ascending}})
in
sortedResults

Peter
PS I enjoyed the film many years ago. Must look it out!
 
Last edited:
Upvote 0
Should have added the result when it's in a Pivot Table. Is this what you are after?

Average of ValueColumn Labels
Result 1Result 2Result 3Result 4
Row LabelsGreenAmberRedGreenAmberRedGreenAmberRedGreenAmberRed
Canada90.364.028.586.526.594.036.013.571.333.0
China89.865.125.487.155.420.481.043.314.975.834.611.6
Croatia58.062.066.045.095.022.0
France86.261.128.185.657.423.475.846.710.880.236.95.8
Germany24.796.063.05.045.516.082.010.5
Japan87.360.637.882.858.517.879.035.79.374.829.09.4
Russia91.063.427.885.652.821.180.245.714.674.735.114.0
United Kingdom19.042.058.059.0
United States71.826.386.352.620.288.447.714.567.634.64.0

<tbody>
</tbody>
 
Last edited:
Upvote 0
Just tried it and it looks good from my end. However the conditional column you added, I can't modify it. I wanted to see how it all works but alas.
I modified the criteria table with the correct percentages and wanted to change the conditional column. The only option it gives me is to delete.
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,399
Members
448,958
Latest member
Hat4Life

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