Istiak Mahmood
Istiak Mahmood

Reputation: 2422

Check each values on Column a column with another column values

Is there any way in Excel or in DAX i can check if all the values of a single column exist or don't on another column.

Example - I have a column called Column 1 where i have some values, like 4,5,2,1. now i want to check how many of those values exists on Column 2 !

As an Output, i expected it can Go Green if the value exists else Red.

enter image description here

I have looked in a lot of place but the only useful result i have found where i can find for a sngle value, not for all the values in a single column.

Do anyone knows any way of doing this work !

Upvotes: 0

Views: 1594

Answers (3)

Surani Matharaarachchi
Surani Matharaarachchi

Reputation: 589

You can do this easily without adding hidden columns as below. This will updated anytime if you change numbers in column A.

  1. Select column B
  2. Conditional Formatting -> New Rule -> Use a formula to determine which cells to format
  3. insert formula as =OR(B2=$A$2,B2=$A$3,B2=$A$4,B2=$A$5) = TRUE and format cell as your wish (here in Green)
  4. Repeat steps 1 to 2
  5. insert formula as =OR(B2=$A$2,B2=$A$3,B2=$A$4,B2=$A$5) = FASLE and format cells as your wish (here in Red)
  6. Select the column name cell (To remove column heading formatting)
  7. Conditional Formatting -> Clear Rule -> Clear Rules from selected cells

Result

Upvotes: 2

Samuel Hulla
Samuel Hulla

Reputation: 7099

Create a (optionally hidden) column that will be adjacent to your search column (in my example that will be column C to column B)

=IF(ISERROR(VLOOKUP(B1,$A$1:$A$4, 1, 0)), FALSE, TRUE)

This will determine, if the value is contained within the first data-list (returns true if it is) And then just use simple conditional formatting

enter image description here

Provides the result as expected:

enter image description here

Upvotes: 2

jpp
jpp

Reputation: 164773

Since you mention Python, this is possible programmatically with the Pandas library:

import pandas as pd

# define dataframe, or read in via df = pd.read_excel('file.xlsx')
df = pd.DataFrame({'col1': [4, 5, 2, 1] + [np.nan]*4,
                   'col2': [6, 8, 3, 4, 1, 6, 3, 4]})

# define highlighting logic    
def highlight_cols(x):
    res = []
    for i in x:
        if np.isnan(i):
            res.append('')
        elif i in set(df['col2']):
            res.append('background: green')
        else:
            res.append('background: red')
    return res

# apply highlighting logic to first column only
res = df.style.apply(highlight_cols, subset=pd.IndexSlice[:, ['col1']])

Result:

enter image description here

Upvotes: 3

Related Questions