Is there a way to sort a column on just the date portion of a day+time field?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,691
Office Version
  1. 365
Platform
  1. Windows
Consider this table containing 7 items that each have a Last Used field and a Rating field.

Sorting.xlsx
CDE
4ItemLast UsedRating
5A8/28/23 14:33 91
6B8/27/23 18:25 81
7C8/28/23 08:21 89
8D8/27/23 09:35 78
9E8/28/23 17:53 83
10F8/27/23 11:22 95
11G8/28/23 07:02 77
Dates


I would like to sort the table by Last Used (oldest to newest) and then by Rating (high to low). If I do that, I get this:

Sorting.xlsx
CDE
4ItemLast UsedRating
5D8/27/23 09:35 78
6F8/27/23 11:22 95
7B8/27/23 18:25 81
8G8/28/23 07:02 77
9C8/28/23 08:21 89
10A8/28/23 14:33 91
11E8/28/23 17:53 83
Dates


In this sort, the Rating field is irrelevant because no two Last Used values are identical. There are many that happened on the same date, but at different times. I only want the sort to take into account the date portion of the Last Used field.

I can get the result I want by adding a helper column with just the date portion of the Last Used field.

Sorting.xlsx
CDEF
4ItemLast UsedRatingDate
5F8/27/23 11:22 958/27/23 00:00
6B8/27/23 18:25 818/27/23 00:00
7D8/27/23 09:35 788/27/23 00:00
8A8/28/23 14:33 918/28/23 00:00
9C8/28/23 08:21 898/28/23 00:00
10E8/28/23 17:53 838/28/23 00:00
11G8/28/23 07:02 778/28/23 00:00
Dates
Cell Formulas
RangeFormula
F5:F11F5=ROUNDDOWN([@[Last Used]],0)


Is there a way I can sort this table and get this result without the use of a helper column?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
You didn't mention if you were looking for a formula solution, VBA solution, either or.
 
Upvote 0
A Power Query solution

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Last Used", type date}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Last Used", Order.Ascending}, {"Rating", Order.Descending}})
in
    #"Sorted Rows"
 
Upvote 0
A Power Query solution

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Last Used", type date}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Last Used", Order.Ascending}, {"Rating", Order.Descending}})
in
    #"Sorted Rows"
I have been meaning to read up on Power Queries, so this is the nudge I need. I am assuming that this code goes in a macro?

Thanks
 
Upvote 0
If you don't mind employing a helper column:
Book2
CDEF
4ItemLast UsedRatingHelper
5A28 Aug 23 14:339145,165.9999090
6B27 Aug 23 18:258145,164.9999190
7C28 Aug 23 08:218945,165.9999110
8D27 Aug 23 09:357845,164.9999220
9E28 Aug 23 17:538345,165.9999170
10F27 Aug 23 11:229545,164.9999050
11G28 Aug 23 07:027745,165.9999230
Sheet1
Cell Formulas
RangeFormula
F5:F11F5=INT(D5)-E5*10^-6


Then sort column F (Helper)

Book2
CDEF
4ItemLast UsedRatingHelper
5F27 Aug 23 11:229545,164.9999050
6B27 Aug 23 18:258145,164.9999190
7D27 Aug 23 09:357845,164.9999220
8A28 Aug 23 14:339145,165.9999090
9C28 Aug 23 08:218945,165.9999110
10E28 Aug 23 17:538345,165.9999170
11G28 Aug 23 07:027745,165.9999230
Sheet1
Cell Formulas
RangeFormula
F5:F11F5=INT(D5)-E5*10^-6
 
Upvote 0
I am assuming that this code goes in a macro?
When you pull a table into Power Query a new window opens, Click the Advanced Editor icon and another window will open and the code goes into there.

1693463845966.png

1693464370429.png
 
Upvote 0
When you pull a table into Power Query a new window opens, Click the Advanced Editor icon and another window will open and the code goes into there.
Thanks for that additional detail. I will try to get up to speed on Power Queries.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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