Remove duplicate in 2 columns

mduntley

Board Regular
Joined
May 23, 2015
Messages
139
Office Version
  1. 365
Platform
  1. Windows
I have the table on the left in powerquery, but i am trying to get it to look like the one on the right. I honestly don't remember what i did.

Book2
ABCDEFGHIJK
1FSCCONTACT DATECPT/CDMAttributeValue.1FSCCONTACT DATECPT/CDMAttributeValue.1
27810/14/2022992852627810/14/202299285262
37810/14/202299285TC299285TC2
47810/14/202299285No Modifier499285No Modifier4
57810/14/202299285SL999285SL9
6781/1/202399285262781/1/202399285262
7781/1/202399285TC299285TC2
8781/1/202399285No Modifier499285No Modifier4
9781/1/202399285SL999285SL9
10782/13/202399285262782/13/202399285262
11782/13/202399285TC299285TC2
12782/13/202399285No Modifier499285No Modifier4
13782/13/202399285SL999285SL9
14784/19/202399285262784/19/202399285262
15784/19/202399285TC299285TC2
16784/19/202399285No Modifier499285No Modifier4
17784/19/202399285SL999285SL9
18785/12/202399285262785/12/202399285262
19785/12/202399285TC299285TC2
20785/12/202399285No Modifier499285No Modifier4
21785/12/202399285SL999285SL9
22785/12/2023992842665992842665
23785/12/202399284TC7899284TC78
24785/12/202399284No Modifier14399284No Modifier143
Sheet1
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
My way with PQ
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table4"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"CONTACT DATE", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "FSC_", each if Value.Is([Attribute],type number) then [FSC] else null),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Contact Date.1", each if Value.Is([Attribute],type number) then [CONTACT DATE] else null),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Custom1",{"FSC_", "Contact Date.1", "FSC", "CONTACT DATE", "CPT/CDM", "Attribute", "Value.1"}),
    #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"FSC", "CONTACT DATE"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns",{{"Contact Date.1", type date}})
in
    #"Changed Type1"

FSC_Contact Date.1CPT/CDMAttributeValue.1
7810/14/202299285262
99285TC2
99285No Modifier4
99285SL9
781/1/202399285262
99285TC2
99285No Modifier4
99285SL9
782/13/202399285262
99285TC2
99285No Modifier4
99285SL9
784/19/202399285262
99285TC2
99285No Modifier4
99285SL9
785/12/202399285262
99285TC2
99285No Modifier4
99285SL9
785/12/2023992842665
99284TC78
99284No Modifier143
 
Upvote 0
That will not always work as I might not have a number in the attribute field
 
Upvote 0
Can you provide a sample of what that file may look like?
Power Query:
Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jc4xCwIxDIbhvyKZD3JJ2/M6u6qLbkc3FW468P8PtoKgadq4tHzkGd5lgf0MA9CI5JFH5jxi5Dnkn6fyQBp66Hr4A5233Wm7rY/1/szLG/pyLOMLIRXiemk1qcJq0s6qrYxiJGdVaUZmaabZpWEZ5pGiFaYZGaaZZpiGZVhAYitMMzJMM80wDVth/hM2hT56l+VrF/2mkXeQ0gs=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [FSC = _t, #"CONTACT DATE" = _t, CPTCDM = _t, Attribute = _t, Value.1 = _t])
 
Upvote 0
Try this:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Columns = Table.ColumnNames(Source),
    Previous = Table.FromColumns(Table.ToColumns(Source) & {{null} & List.RemoveLastN(Source[CONTACT DATE],1)}, Columns & {"Previous"}),
    AlteredRows = Table.TransformRows(Previous, each if [CONTACT DATE] = [Previous] then _ & [FSC = null, CONTACT DATE = null] else _),
    Table = Table.FromList(AlteredRows, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    Result = Table.ExpandRecordColumn(Table, "Column1", Columns)
in
    Result
 
Upvote 0
Note: My code only looks for the same value of the previous row's date value. If FSC should be also checked for the same value, then the code should be a bit modified for that. Just FYI.
 
Upvote 0
There will be a bit more FSC, and it doesnt work if i add additional one.

Power Query:
= Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jc4xC8IwEIbhvyKZC9e73MVmdlUX3Uo3FToV/P+DaaGgl0vikvCRh/COozsOrnPYAzJQT5RGjDRIuimsh5u6Grqf/kDX5XBZHvNrfr7T4oa+ndfxhQBX4mtpOcnCclLOyq2OIkDfqrKMzrJMscvCOowBYyvMMjrMMsUwC+swAaRWmGV0mGWKYRZuhfEeFqSOtrL0WkW/ach+4ySce8l9kPT99AE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [FSC = _t, #"CONTACT DATE" = _t, CPTCDM = _t, Attribute = _t, Value.1 = _t])
 
Upvote 0
There will be a bit more FSC, and it doesnt work if i add additional one.

Power Query:
= Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jc4xC8IwEIbhvyKZC9e73MVmdlUX3Uo3FToV/P+DaaGgl0vikvCRh/COozsOrnPYAzJQT5RGjDRIuimsh5u6Grqf/kDX5XBZHvNrfr7T4oa+ndfxhQBX4mtpOcnCclLOyq2OIkDfqrKMzrJMscvCOowBYyvMMjrMMsUwC+swAaRWmGV0mGWKYRZuhfEeFqSOtrL0WkW/ach+4ySce8l9kPT99AE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [FSC = _t, #"CONTACT DATE" = _t, CPTCDM = _t, Attribute = _t, Value.1 = _t])
Right. The following is the complete version. (The code could be certainly shortened by creating a record column instead of two columns, but just using the same method for the second field).

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Columns = Table.ColumnNames(Source),
    PreviousDate = Table.FromColumns(Table.ToColumns(Source) & {{null} & List.RemoveLastN(Source[CONTACT DATE],1) }, Columns & {"PreviousDate"}),
    PreviousFSC = Table.FromColumns(Table.ToColumns(PreviousDate) & {{null} & List.RemoveLastN(PreviousDate[FSC],1) }, Table.ColumnNames(PreviousDate) & {"PreviousFSC"}),
    AlteredRows = Table.TransformRows(PreviousFSC, each if [CONTACT DATE] = [PreviousDate] and [FSC] = [PreviousFSC] then _ & [FSC = null, CONTACT DATE = null] else _),
    Table = Table.FromList(AlteredRows, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    Result = Table.ExpandRecordColumn(Table, "Column1", Columns)
in
    Result
 
Upvote 0
No need to add two previous value columns as I mentioned in my post. Here is the simplified version. Intentionally formatted the code to make functions and their parameters more readable.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Columns = Table.ColumnNames(Source),
    Previous = Table.FromColumns(
                    Table.ToColumns(Source) 
                        & {{null} & List.RemoveLastN(Source[CONTACT DATE],1)}
                        & {{null} & List.RemoveLastN(Source[FSC],1) },
                    Columns & {"PreviousDate", "PreviousFSC"}
                ),
    AlteredRows = Table.TransformRows(Previous, each
                            if [CONTACT DATE] = [PreviousDate] and [FSC] = [PreviousFSC]
                            then _ & [FSC = null, CONTACT DATE = null]
                            else _
                    ),
    Table = Table.FromList(AlteredRows, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    Result = Table.ExpandRecordColumn(Table, "Column1", Columns)
in
    Result
 
Upvote 0
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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