简体   繁体   中英

Creating a consolidated set of pairs on Excel

I am working with an Excel file having import and export data between different countries for various shipping products, which looks pretty much like this:

原始数据

The goal is to create a consolidated state pair that have a trade relation between them. So the final list for the above example should look something like this:

结果目标

What would be the best way to go about this?

Try below formula-

=COUNTIFS(A:A,E4,B:B,F4)+COUNTIFS(A:A,F4,B:B,E4)

在此处输入图片说明

So assume your data is in a&b in the first screenshot, put this formula in column c =a1&b1 then hit enter, then drag the bottom right hand corner down so the formula works for all cells in that column. Then copy the column and paste it as values.

Then in column d, use the formula =countifs(c:c,c1) and drag it down in the same way. Now paste column d as values.

Finally, on the data tab, remove duplicates based on column c and then delete column c.

You can do this really easy with Pivot Tables. Just take Field Importing Country into rows section and also values section (make sure it's counting) and field Exporting Country to rows section.

Choose Tabular design, deactivate subtotals and activate Repeat labels. This is what you get:

在此处输入图片说明

You can do this with Power Query, available in Windows Excel 2010+ and O365

  • Sort each row horizontally
  • Group by the two resultant country rows, aggregating by Count

M Code

let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Importing Country", type text}, {"Exporting Country", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Country", each List.Sort({[Importing Country],[Exporting Country]})),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Importing Country", "Exporting Country"}),
    #"Extracted Values" = Table.TransformColumns(#"Removed Columns", {"Country", each Text.Combine(List.Transform(_, Text.From), ";"), type text}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "Country", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Country.1", "Country.2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Country.1", type text}, {"Country.2", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type1", {"Country.1", "Country.2"}, {{"Occurrences", each Table.RowCount(_), Int64.Type}})
in
    #"Grouped Rows"

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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