当一个单元格有多个值以逗号分隔时,对 Excel 中的列数据进行排序

Sorting column data in Excel when one cell has multiple values seperated by a comma

我这里有两栏要看。在每个相应的单元格中,它们是多个值,全部用逗号分隔。外观如下:

Column A (States) Column B (Dates)
(Cell A2): Ohio, Georgia, Alabama (Cell B2): 06-01-2021, 04-01-2021, 10-01-2021
(Cell A3): Alaska, Utah, California (Cell B3): 11-01-2021, 12-01-2021, 06-01-2021

所以你可以看到一个单元格可以有多组数据,用逗号分隔。州与日期相关,因此在单元格 A2 示例中:俄亥俄州是 06-01-2021,乔治亚州是 04-01-2021,阿拉巴马州是 10-01-2021。

这里的问题是我需要将日期从最旧到最新排序,并根据该更改调整状态。我试过排序,但由于一个单元格包含多个值,所以似乎不起作用。

工作簿之所以是这种格式是因为我在MySQL中使用了group_concat查询,现在我需要在这里进行分析。我在这里尝试了几个小时,但我被困住了。有什么想法吗?

单元格 A2 和 B2 的预期输出是这样的:

Column A (States) Column B (Dates)
(Cell A2): Georgia, Ohio, Alabama (Cell B2): 04-01-2021, 06-01-2021, 10-01-2021

上面的示例是正确的,因为日期是按从旧到新的顺序排列的,并且状态也随之调整。我这里有 30k 行数据。

如果你有 Windows Office 365,你可以使用公式,

  • FILTERXML 将字符串转换为数组
  • SORT用于对日期进行排序
  • SORTBY用于按排序的日期顺序对各州进行排序

如果你有 Office 365 但在 Mac 上,你将没有 FILTERXML 函数,但还有其他方法可以从逗号创建必要的数组分隔列表

排序状态

=TEXTJOIN(", ",,SORTBY(FILTERXML("<t><s>" & SUBSTITUTE(A2,",","</s><s>")& "</s></t>","//s"),(FILTERXML("<t><s>" & SUBSTITUTE(B2,",","</s><s>")& "</s></t>","//s"))))

排序日期

=TEXTJOIN(", ",,TEXT(SORT(FILTERXML("<t><s>" & SUBSTITUTE(B2,",","</s><s>")& "</s></t>","//s")),"m/d/yyyy"))

您也可以在 Windows Excel 2010+ 和 Office 365

中提供的 Power Query 中执行此操作

使用 Power Query

  • Select 数据中的某个单元格 Table
  • Data => Get&Transform => from Table/Range
  • 当 PQ 编辑器打开时:Home => Advanced Editor
  • 记下第 2 行中的 Table 名称
  • 粘贴下面的 M 代码代替您看到的内容
  • 将第 2 行中的 Table 名称更改回最初生成的名称。
  • 阅读评论并探索 Applied Steps 以了解算法

M码

let
    Source = Excel.CurrentWorkbook(){[Name="Table14"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"States", type text}, {"Dates", type text}}),

/*Sort the string of Dates 
    Split the string into a List and trim each element
    Transform each list element into a "real date"
    Sort the list of dates
    Convert each date into a text string with the same format as original column
    Combine the list of dates back into a comma separated string
*/
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Sorted Dates", each Text.Combine(
        List.Transform(
            List.Sort(
                List.Transform(
                    List.Transform(
                        Text.Split([Dates],","), each Text.Trim(_)),
                    each Date.FromText(_))
                ), each Date.ToText(_,"M/d/yyyy")),
                    ", "), Text.Type),

/*Sort the string of states
    Split the string of states, string of dates, and string of sorted dates into a trimmed list
    Find the Position of each Date (from sorted List) in the Unsorted List
    Use that position as an Index into the unsorted State list to create a sorted state list
    Combine the sorted state list into a comma separated string
*/
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Sorted States", each 
        let 
            dts = List.Transform(Text.Split([Dates],","),each Text.Trim(_)),
            srtDts = List.Transform(Text.Split([Sorted Dates],","), each Text.Trim(_)),
            sts = List.Transform(Text.Split([States],","),each Text.Trim(_)),
            
            states = List.Generate(
                ()=> [state=sts{List.PositionOf(dts,srtDts{0})}, idx=0],
                each [idx] < List.Count(dts),
                each [state = sts{List.PositionOf(dts,srtDts{[idx]+1})}, idx = [idx]+1],
                each [state])
        in 
            Text.Combine(states,", "), Text.Type),

//Remove unneeded column and reorder the date/state columns        
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"States", "Dates"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Sorted States", "Sorted Dates"})
in
    #"Reordered Columns"

原创

已排序