How about a fun Excel challenge? I have data in below format in the table named trips
I want to know which employees visited both USA & UK?
How would you solve this problem? Post your solutions in the comments.
Need sample data & my solution? Click here to download the file.
Want more challenges & home-work problems? See this page.
13 Responses to “Who went to both USA & UK? [Excel Challenge]”
Filter for USA & UK in col 2, remove col 2, keep duplicates col 1, remove duplicates col 1.
Is it cheating to use PY formulas?
```python
focal_locs: list[str] = ["UK", "USA"]
df_trips = xl("trips[#All]", headers=True)
df = df_trips.drop_duplicates().query('Country in @focal_locs')
recs_both = df[df['Employee'].duplicated()]
recs_both['Employee'].sort_values()
```
I started to do an Excel solution, but need to run to a meeting. It wasn't as concise as yours anyway!
```
=LET(
focalLocs,{"USA","UK"},
allEmps, UNIQUE(trips[Employee]),
blnEmpsWentToEither, ISNUMBER(XMATCH(trips[Country],focalLocs)),
recsWentToEither, FILTER(trips,blnEmpsWentToEither),
uniqRecsWentToEither, SORT(UNIQUE(recsWentToEither)),
empsWentToEither, CHOOSECOLS(uniqRecsWentToEither,1),
empsWentToEither
)
```
Just need to return the dups from `empsWentToEither`.
Power Query solution based on Group By and List.Contains.
let
Source = Excel.CurrentWorkbook(){[Name="trips"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Employee", type text}, {"Country", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Employee"}, {{"Countries", each _, type table [Employee=nullable text, Country=nullable text]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "CountriesList", each Table.Column([Countries], "Country")),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Went to both UK and USA", each if List.Contains([CountriesList], "UK") and List.Contains([CountriesList], "USA") then "Went to both UK and USA" else "Did not go to both UK and USA")
in
#"Added Custom1"
Does this require a formula or power query ? A simple pivot table added to data model will allow a count distinct output. A country filter on US & UK should return the two employees.
Good one 🙂 Of course, formula / pq / dax options will give you flexibility to parameterize the country list. That is all.
Hi
=TOCOL(UNIQUE(BYROW(trips,LAMBDA(k,@FILTER(trips[Employee],IF(INDEX(k,,2)="UK",(trips[Employee]=INDEX(k,,1))*(trips[Country]="Usa")))))),3)
Another one, very elegant in my opinion.
=LET(
e, trips[Employee],
c, trips[Country],
one, UNIQUE(FILTER(e, c = "USA")),
two, UNIQUE(FILTER(e, c = "UK")),
combo, XLOOKUP(one, two, two, ""),
SORT(FILTER(combo, combo ""))
)
In the last filter, "less than" and "more than" signs are gone.
Same formula, this time not using the missing signs:
=LET(
e, trips[Employee],
c, trips[Country],
one, UNIQUE(FILTER(e, c = "USA")),
two, UNIQUE(FILTER(e, c = "UK")),
combo, XLOOKUP(one, two, two, ""),
SORT(FILTER(combo, NOT(combo = "")))
)
I've translated from italian language, maybe some separators need to be changed, but is very short:
=LET(
E, trips[Employee],
UNIQUE(
FILTER(
E,
MMULT(
--(
COUNTIFS(
E, E,
trips[Country], {
"UK",
"USA"
}
) > 0
),
{1;1}
) > 1,
""
)
)
)
=LET(
usa,UNIQUE(FILTER(trips[Employee],trips[Country]="USA")),
uk,UNIQUE(FILTER(trips[Employee],trips[Country]="UK")),
both,FILTER(usa,ISNUMBER(XMATCH(usa,uk))),
SORT(both)
)
Or...
=LET(
emp,UNIQUE(trips[Employee]),
num,MMULT(SIGN(COUNTIFS(trips[Employee],emp,trips[Country],{"USA","UK"})),{1;1}),
SORT(FILTER(emp,num=2))
)
Hi, could be great to add min Excel version required.
LET(
UK_USA,TAKE(UNIQUE(FILTER(trips,(trips[Country]="UK")+(trips[Country]="USA"))),,1),
UNIQUE(VSTACK(UNIQUE(UK_USA),UNIQUE(UK_USA,,TRUE)),,TRUE))