简体   繁体   English

根据最新的modifiedDate获取Excel表中的不同行

[英]Get the distinct rows in an Excel table based on the latest modifiedDate

Following is the table we have in the Excel. 以下是Excel中的表格。 it contains duplicate entries of the tickets. 它包含票证的重复条目。 we need to convert this excel table into another table having only unique rows having the latest Modified_Date 我们需要将此excel表转换为另一个表,该表仅包含具有最新Modified_Date的唯一行

Tket Status Modified_Date
---- ------ -------------
5184 Active 20-07-2015
5184 Active 22-07-2015
5184 Closed 25-07-2015
5292 Active 22-07-2015
5292 Closed 23-07-2015
5480 Active 23-07-2015
5480 Closed 24-07-2015

Do i need to write macro code to achieve the same or is there any other alternate in MS Excel. 我需要编写宏代码来实现相同的功能还是MS Excel中是否有其他替代方法。 I am using office 2013. 我正在使用Office 2013。

There is a non-VBA solution which is possible here; 这里有一个非VBA解决方案。 doing it in this way does not create a new table, though. 但是,以这种方式执行操作不会创建新表。 Instead, it creates a column to check for whether each row is the latest date of that ticket number - you can then filter the column to show only matches; 相反,它创建一列来检查每一行是否是该票证号码的最新日期-然后,您可以过滤该列以仅显示匹配项; this column can then be copied to a new table if required. 然后可以根据需要将此列复制到新表中。

Add a column, say column F, which will check whether that row has the latest modified-by date for the unique ticket number. 添加一列,例如F列,它将检查该行是否具有唯一票证号的最新修改日期。 Assuming Ticket Number is in column A, and the Modified-by date is in column C, type this in F2, and drag down [assuming data goes down only to row 14]: 假设票证编号在A列中,并且修改日期在C列中,请在F2中键入此内容,然后向下拖动[假设数据仅向下移至第14行]:

=MAX(IF($A$2:$A$14=A2,$C$2:$C$14,""))=C2

This is an Array Formula, which must be confirmed by pressing CTRL + SHIFT + ENTER, rather than just ENTER. 这是一个数组公式,必须通过按CTRL + SHIFT + ENTER(而不是ENTER)来确认。 It works by checking each possible iteration of the function for each cell involved, one at a time. 它通过每次检查所涉及的每个单元的功能的每个可能的迭代来工作。 It then provides an array of possible results, and you must collapse that resulting array into a single value. 然后,它提供了可能结果的数组,您必须将该结果数组折叠为单个值。 Here, the IF statement checks each cell from A2 to A14 to see if it is equal to A2. 在这里,IF语句检查从A2到A14的每个单元,以查看它是否等于A2。 If the Ticket numbers match, then it gives the date from column C, for each row, otherwise, it gives "". 如果票证号匹配,则为每一行提供C列的日期,否则为“”。 ie: if the only Ticket #'s which matched A2 were A2 and A5, the array of results would show up like this: 即:如果唯一匹配A2的票证编号是A2和A5,则结果数组将显示如下:

={DATE IN C2,"","",DATE IN C5,"","",...}

Now we need to collapse the array of results into a single value. 现在我们需要将结果数组折叠为一个值。 Because we are working with numbers, doing this is fairly straightforward (collapsing an array of string results can be harder, because there are few functions in Excel which work with arrays of strings). 因为我们正在处理数字,所以这样做非常简单(折叠字符串结果数组可能会比较困难,因为Excel中很少有函数可以处理字符串数组)。 Simply take the MAX value of the array, and it will return the latest date where there is a match for that Ticket number. 只需取数组的MAX值,它将返回与该票证号匹配的最新日期。

This is then turned into a value of either TRUE [this row contains the latest date for that Ticket Number], or FALSE [this row does not contain the latest date for that Ticket Number], by comparing the result from the MAX formula with the date in the current C2 cell. 然后,通过将MAX公式的结果与以下公式进行比较,将其转换为TRUE(此行包含该工单号的最新日期)或FALSE(该行不包含该工单号的最新日期)的值。当前C2单元中的日期。 This method has the added benefit of leaving multiple matches where more than 1 line has the same latest modified-by date for that ticket number. 此方法还有一个好处,就是可以保留多个匹配项,其中有多于1行的那个票证号具有相同的最新修改日期。

Then with that column of TRUE / FALSE values, filter for TRUE, and if necessary, simply copy & paste to another sheet. 然后,使用该列TRUE / FALSE值,对TRUE进行过滤,如果需要,只需将其复制并粘贴到另一张纸上。

However note that for your purposes VBA may be better as this is somewhat manual. 但是请注意,出于您的目的,VBA可能会更好,因为它有些手动。

It is a two simple step solution. 这是两个简单的步骤解决方案。 (tried to use concept of SQL group-by clause) (尝试使用SQL group-by子句的概念)

Step 1: sort the entire range on 步骤1:对整个范围进行排序

  • Ticket ID ASC 机票ID ASC
  • Modified Date DESC 修改日期DESC

then in a new column besides ModifiedDate use below formula 然后在ModifiedDate之外的新列中使用以下公式

=IF(A2 = A1,"","1")


Ticket  Status  Modified    UniqueRows
5184    Closed  25-07-2015  1
5184    Active  20-07-2015
5184    Active  20-07-2015
5292    Closed  23-07-2015  1
5292    Active  22-07-2015
5480    Closed  24-07-2015  1
5480    Active  23-07-2015

My table starts at A1 cell. 我的桌子从A1单元格开始。

since, after sorting I have all the tickets together, they form a group. 因为整理完所有票后,我将它们组合在一起。 within that group I sorted on modified date in descending order. 在该组中,我以降序对修改日期进行了排序。 thus I have the latest record on top for that ticket group. 因此,我有该票务组的最新记录。

I know the latest record (within the group) is on top, Thus, I compared the current ticket Id with the one above, if it changes then it is a new group and output value as 1. 我知道最近的记录(在该组中)位于最前面,因此,我将当前票证ID与上面的票证ID进行了比较,如果更改,则它是一个新组,输出值为1。

now filter all the '1' and you have the records of your interest. 现在过滤所有的“ 1”,您就拥有了自己感兴趣的记录。

Ticket  Status  Modified    UniqueRows
5184    Closed  25-07-2015  1
5292    Closed  23-07-2015  1
5480    Closed  24-07-2015  1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM