Sorting with exceptions

jc364698

New Member
Joined
Sep 24, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm trying to sort a testing schedule that routinely has more tests added to it. The workbook has a macro that pulls the information for each test from another sheet and writes them to the table I am trying to formal. The table may appear as such, before sorting:

1632511926244.png


Machine 1 or 2 indicates that the test in currently running on the specified machine. This is how the tests should be sorted:

First, by priority from A to Z. Then, by status (machine 1, then machine 2, then queue). Third, sort by Index from smallest to largest (only as a tiebreaker). The other columns don't matter However, no matter its priority, any tests with the status "not ready" must be sent to the bottom. With these rules, the table above should look like this:

1632512175666.png


My thoughts are that they should all be sorted based on priority, then status, then index, then pick out any tests with "not ready" status and send them to the bottom.

Thanks!
 

Attachments

  • 1632511090306.png
    1632511090306.png
    19 KB · Views: 4
  • 1632511626453.png
    1632511626453.png
    18.5 KB · Views: 4

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
In Power Query:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table6"]}[Content], // Change "Table6" to the name of your table
    tblAddPrioritySortColumn = Table.AddColumn(Source, "Custom", each if [Status] = "Not Ready" then "Z" & [Priority] else [Priority]),
    tblAddStatusSortColumn = Table.AddColumn(tblAddPrioritySortColumn, "Custom.1", each 
        if [Status] = "Machine 1" then "AMachine 1" else
        if [Status] = "Machine 2" then "BMachine 2" else
        if [Status] = "Queue" then "CQueue" else "ZNot Ready"),
    tblSort = Table.Sort(tblAddStatusSortColumn,{{"Custom", Order.Ascending}, {"Custom.1", Order.Ascending}, {"Index", Order.Ascending}}),
    tblResult = Table.RemoveColumns(tblSort,{"Custom", "Custom.1"}) 
in
    tblResult

Filter Function.xlsm
ABCDEFGHIJKLM
1IndexTitleMaterialStatusDuration (Days)PriorityIndexTitleMaterialStatusDuration (Days)Priority
22048Test1IronMachine 14C3851Test5Steel RodMachine 17A
33408Test2WoodQueue4C2201Test6WoodMachine 27A
42786Test3WoodQueue2B3551Test8Steel RodMachine 15B
53567Test4Steel RodMachine 12C3868Test10Steel RodMachine 25B
63851Test5Steel RodMachine 17A2786Test3WoodQueue2B
72201Test6WoodMachine 27A2048Test1IronMachine 14C
83315Test7WoodMachine 26C2377Test11IronMachine 15C
93551Test8Steel RodMachine 15B3567Test4Steel RodMachine 12C
103777Test9TubingNot Ready4B3315Test7WoodMachine 26C
113868Test10Steel RodMachine 25B3408Test2WoodQueue4C
122377Test11IronMachine 15C3777Test9TubingNot Ready4B
Sheet6
 
Upvote 0
Thank you for the reply - this does what I need it to by sending "not ready" tests to the end of the list, but there is an issue with the sorting based on "status".

I may not have explained this thoroughly enough, but: a status of "machine 1" or "machine 2" means that the test is currently being run on that particular machine. Therefore, only one test at a time can have either of those at any time.

Additionally, any test currently being run (i.e. its status is "machine 1" or "machine 2") it should be at the top of the list no matter its priority. So, if for some reason a priority C test is being run on machine 1, it should be at the top of the list even if there is a priority A in queue.
 
Upvote 0
see if this sorts the way you intend:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], // Change "Table1" to the name of your table
    tblAddPrioritySortColumn = Table.AddColumn(Source, "Custom", each if [Status] = "Not Ready" then "Z" & [Priority] else [Priority]),
    tblAddStatusSortColumn = Table.AddColumn(tblAddPrioritySortColumn, "Custom.1", each 
        if [Status] = "Machine 1" or [Status] = "Machine 2" then "A" else
        if [Status] = "Queue" then "C" else "Z"),
    tblSort = Table.Sort(tblAddStatusSortColumn,{{"Custom.1", Order.Ascending}, {"Custom", Order.Ascending}, {"Index", Order.Ascending}}),
    tblResult = Table.RemoveColumns(tblSort,{"Custom", "Custom.1"}) 
in
    tblResult

Book5
ABCDEFGHIJKLM
1IndexTitleMaterialStatusDuration (Days)PriorityIndexTitleMaterialStatusDuration (Days)Priority
22048Test1IronMachine 14C2201Test6WoodMachine 27A
33408Test2WoodQueue4C2048Test1IronMachine 14C
42786Test3WoodQueue2B2786Test3WoodQueue2B
53567Test4Steel RodNot Ready2C3408Test2WoodQueue4C
63851Test5Steel RodNot Ready7A3851Test5Steel RodNot Ready7A
72201Test6WoodMachine 27A3551Test8Steel RodNot Ready5B
83315Test7WoodNot Ready6C3777Test9TubingNot Ready4B
93551Test8Steel RodNot Ready5B3868Test10Steel RodNot Ready5B
103777Test9TubingNot Ready4B2377Test11IronNot Ready5C
113868Test10Steel RodNot Ready5B3315Test7WoodNot Ready6C
122377Test11IronNot Ready5C3567Test4Steel RodNot Ready2C
Sheet1
 
Upvote 0
Solution

Forum statistics

Threads
1,214,547
Messages
6,120,139
Members
448,948
Latest member
spamiki

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