Complex filter in Excel using AND/OR Nesting - sql

Is it possible to create a filter in Excel across multiple columns with AND/ORs, or what's the suggested way to do this? For example:
WHERE
(Name = "Tom" AND country in ['US', 'CA'])
OR
(Age > 14 and country not in ['FR', 'DE'])
If not, how does one normally do a SQL-like filter clause in Excel, or is this considered too much of an edge case for Excel to consider?

You may try:
=OR(AND(Name="Tom", OR(country="US", country="CA")),
AND(Age > 14, AND(country<>"FR", country<>"DE")))
The trick is that using AND/OR with Excel requires nesting the logic, which is different than how SQL does it.
You may replace Name and country with the actual Excel cells which contain that particular data.

In Excel, AND and OR are functions. See Excel / Formulas and functions.
Example:
=OR(AND(B2="Tom", OR(B3="US", B3="CA")), AND(B4>14, B3<>"FR", B3<>"DE")))

With the new Dynamic Array formulas (available in most versions of Office 365), the list can be filtered with the Filter() function and the result is displayed outside of the list.
The screenshot shows Tim Biegeleisen's formula with conditional formatting to highlight the TRUE values before filtering the table on that column. Next to that is the result of ONE formula in cell H2, which has automatically spilled to the right and down. For ease of identification, I have included an index column.
Note that AND logic is built with the multiplication operator, whereas OR logic uses the addition operator.
=FILTER(Table1,
((Table1[Name]="Tom")*((Table1[Country]="US")+(Table1[Country]="CA")))+
((Table1[Age]>14)*(Table1[Country]<>"FR")*(Table1[Country]<>"DE")))

Related

How can I remove duplicates in PowerPivot?

I have a PowerPivot opened in Excel that's loading a csv file.
I need to remove duplicates in a column but there is no query edit.
What's the best way to do this?
The column in question contains text values like addresses
There are two options, you can use power query (part of excel 2016, addonn in excel 2013) to clean the data up before it is loaded into power pivot. Note that if you are using power query it is case sensitive so it will consider ABC <> Abc.
It does have functions to force strings to be all upper case, lower case or Capital for the first word.
The option is to setup the connection in Power Pivot, then go to the loaded table in the model. In the ribbon do design --> Table properties which has options for editing the SQL statement which is used to generate the table.

Coloring Excel Cell based on a table condition

I am going through the users of a system and reviewing if they have appropriate role names. I then completed an excel table that looks abit like this:
I'm trying to turn the table into a more readable format. I have made a pivot that looks like this:
But I'm not sure how to highlight the cells to reflect the 'Access Appropriate? Yes/No' column. Ideally, it should be colored yellow if the 'Access Appropriate?' = 'No'. I'm thinking of using VBA, but was wondering if there is an easier solution using formulas or pivot table?
Your pivoted data isn't an actual excel pivot table, is it? I know what the x mean, but where do they come from?
Two possibilities come to mind if you want a flexible setup without VBA, aswell as an rather simple VBA-approach that uses an UDF.
Quick'n'dirty (really dirty) would be to
use 1/0 instead of yes/no (you could write that into a helper column with an if-function)
create a new pivot with ROLE_NAME for columns, USER_NAME for rows and SUM or MAX of [Access appropriate] for values
that means: instead of your x you will end up having 1 and 0. Empty cells will still be empty.
conditional format the value-range, e.g. If 1 then green If 0 then yellow if "" then Nothing
Alternatively, you could build your output-table with formulas like INDEX, MATCH and VLOOKUP-formulas.
An additional Key-Column with USERNAME&ROLE_NAME will be needed
conditional format the value-range
VBA: Provided your Rows are distinct a user defined function could do the following
read data into a recordset IF that hasnt been done already (meaning: declared on module-level, the first function call will fill it)
access the data in your recordset with a Recordset.Filter based on your input parameters - USERNAME and ROLE_NAME, in your case
output a certain Field.Value based on your input parameter - Access Appropriate in your case
conditional format the TRUE/FALSE values you get (since this can't easily be done inside an UDF)

Performing multiple vlookup operation in VBA

I have a logical doubt. Is it possible to return a value to a cell by performing Vlookup operation in 3 sheets, i.e the value to be returned might be in one of the three sheets. If so how can I approach this? I am trying to create a macro using VBA.
if there are no real differences between the worksheets you can use formulas instead of VBA using =IFERROR() and =VLOOKUP()
An example would be:
=IFERROR(VLOOKUP(valueToLookFor,sheet1Range,columnNumber,FALSE),IFERROR(VLOOKUP(valueToLookFor,sheet2Range,columnNumber,FALSE),VLOOKUP(valueToLookFor,sheet3Range,columnNumber,FALSE)))
This would just perform the search on the first sheet, if the value isn't there it will return an error, hence looking in the following sheet etc.
Bear in mind I've written the formula given your Excel is in English with commas (",") as separators, you might need to translate it in you default language and separators.

Conditional formatting comparing two adjacent columns for every other column

It's easy to do conditional formatting that compares 2 columns, e.g.: =$A1=$B1. But what if I also want to compare C and D, but NOT B and C? Ideally, I'd like to do this with a single conditional formatting formula since I have many columns, but I'll settle for VBA if necessary (I'm just unfamiliar with conditional formatting in VBA).
NOTE: I've tried some variations of =AND(MOD(COLUMN(),2),A1=B1), but that hasn't worked.
NOTE 2: Here's some more specifics: Basically I'm comparing data in two nearly identical tables, and I want to highlight any differences. So for example, each table has a "Study_ID" column, and I want to view those side by side on a new sheet, e.g. in Columns A and B, highlighting unmatched cells in both columns. It would be easy to say =$A1:$B1 and apply to =$A$A:$B$B. But I'm also viewing "Name" side by side, e.g. in Columns C and D. I could add a new formula =$C1:$D1. But since the source tables have like 20 columns, that would require like 20+ formulas. I'd prefer one.
Thanks!
The image below was accomplished using multiple formulas (=$A1<>$B1, applied to =$A$1:$B$4, and =$C1<>$D1, applied to =$C$1:$D$4)
Edit: Figured out a single formula that will do it:
=A2<>OFFSET(A2,,IF(MOD(COLUMN(),2)=1,1,-1))

Need a simple search function to display most common value in a column. (with ambiguous choices)

I have a very large array of data with many columns that display different outputs for the values presented. I would like to add a row above the data that will display the most common occurring value or word below.
Generally I would like to have each top of the column (right under the column label in row 1) have the most common value below. I will then use this value for various data analysis functions!
Is this possible, and if so, how? Preferably this will not require VBA, but simply a short code in the cell.
One caveat: The exact values may vary, so there is no set list where I can say "it will be one of these."
Any ideas appreciated!
Try a series of =COUNTIF(A:A,"VALUE TO SEARCH") functions if you want to stay away from VBA.
Otherwise, the best method would be to iterate through each column via VBA. With this method, you can even count the "varying" values and return the count and/or the value itself.
http://www.excel-easy.com/examples/most-frequently-occurring-word.html
This is a single formula you would write at the top of each column. Does not require VBA. You can replace the set range to an entire column, such as (A:A) instead of (A1:A7).
If you mean an array as in a data type, it could work differently but it depends what you're trying to do.
With data from A3 through A16, in A2 enter:
=INDEX($A$3:$A$16,MODE(MATCH($A$3:$A$16,$A$3:$A$16,0)))
This will work for text as well as numbers. Adjust this to match the column size.