Sum values in one column of all rows which match one or more of multiple criteria - regex

I have some table data in which I'd like to sum all the values in a specific column of all rows where column A contains string A and/or column B contains string B. How can I achieve this?
This works for one criterium:
=SUM(FILTER(G:G,REGEXMATCH(F:F,"stringA")))
I tried this, but it didn't work:
=SUM(FILTER(G:G,OR(ISTEXT(REGEXMATCH(F:F,"stringA")),ISTEXT(REGEXMATCH(C:C,"stringB")))))

Please try:
=SUM(FILTER(G:G,REGEXMATCH(F:F,"stringA")+REGEXMATCH(C:C,"stringB")))
+ works for or logic. ISTEXT is not needed because REGEXMATCH gives true or false.
OR does not work because filter is an arrayformula, use + in array formulas.

=SUM(FILTER(G:G,REGEXMATCH(F:F&C:C,"stringA|stringB")))
OR is denoted by |
EDIT Added &C:C to denote different Columns

Related

Insert range using IF formula in SUMIF function with multiple criteria (using arrayformula)

I am using arrayformula with my sumif function which has several criterion and ranges to expand automatically across a set column (B). The formula below works perfectly:
=ARRAYFORMULA(SUMIF('DATA'!$N:$N&'DATA'!$U:$U&'DATA'!$V:$V&'DATA'!$BV:$BV,$B$6:$B&$E$30&$D$26&$C$36,'DATA'!$AG:$AG).
I want to make it more dynamic and insert an IF statement to actually refer to a range vs another based on certain conditions. For ex,
IF($A$6:$A=XX,'DATA'!$N:$N,'DATA'!$M:$M).
The formula then breaks if I do this
=ARRAYFORMULA(SUMIF(IF($A$6:$A=XX,'DATA'!$N:$N,'DATA'!$M:$M)&'DATA'!$U:$U&'DATA'!$V:$V&'DATA'!$BV:$BV,$B$6:$B&$E$30&$D$26&$C$36,'DATA'!$AG:$AG).
Any way/workaround to make it work? I tried multiple times, read multiple forums but can't find an answer.
if XX is not named range it should be in quotes
try:
=ARRAYFORMULA(SUM(IF(
IF(A6="XX", 'DATA'!N:N, 'DATA'!M:M)&DATA!U:U&DATA!V:V&DATA!BV:BV=B6:B&E30&D26&C36,
DATA!AG:AG, )))
UPDATE:
=ARRAYFORMULA(IFNA(VLOOKUP(B7:B&C7:C,
QUERY({DATA!A2:A&DATA!A1, DATA!C2:F; DATA!B2:B&DATA!B1, DATA!C2:F},
"select Col1,sum(Col5)
where Col2 = '"&D4&"'
and Col4 = '"&D3&"'
group by Col1
label sum(Col5)''"), 2, 0)))

Join only some cells from a column - Openrefine

I've a dataset like this:
I need to join only the last three columns (the authors) and the join function is not helping me. I don't have always 3 authors: they can be 2, it can be 1.
Is there a way to join only the cells which have empty cells in the near column?
A solution a bit simpler is to use slice() to select all values on each record except the first one :
row.record.cells['Column name'].value.slice(1).join("|")
Well, I used a workaround:
first I add another column with row.record.cells.NameColumn.value.join("|")
then in the new column I eliminated the book title doing value.replace(/(^[^\|]+)\|(.+)$/, "$2")

Iteratively divide column by column plus suffix (pandas)

In my for loop I would like to divide a column by another. The second column's name is dependent upon the first. Example data frame columns look like this:
column1, column1_mean, column2, column2_mean
I would like to iteratively divide each column by its corresponding mean column ( column1 / column1_mean; column2/column2_mean ).
Thanks for your help.
For a given list of column names cols = ['column1', 'column2', ... ], you can use .div() to divide a list of columns by another list of columns, and use string formatting to make the second list of columns based on the first:
df[cols].div(df[['{0}_mean'.format(col) for col in cols]])
But a simpler way would be to dispense with making the mean columns altogether:
df[cols].div(df[cols].mean())

How to search multiple strings in a string?

I want to check in a powerquery new column if a string like "This is a test string" contains any of the strings list items {"dog","string","bark"}.
I already tried Text.PositionOfAny("This is a test string",{"dog","string","bark"}), but the function only accepts single-character values
Expression.Error: The value isn't a single-character string.
Any solution for this?
This is a case where you'll want to combine a few M library functions together.
You'll want to use Text.Contains many times against a list, which is a good case for List.Transform. List.AnyTrue will tell you if any string matched.
List.AnyTrue(List.Transform({"dog","string","bark"}, (substring) => Text.Contains("This is a test string", substring)))
If you wished that there was a Text.ContainsAny function, you can write it!
let
Text.ContainsAny = (string as text, list as list) as logical =>
List.AnyTrue(List.Transform(list, (substring) => Text.Contains(string, substring))),
Invoked = Text.ContainsAny("This is a test string", {"dog","string","bark"})
in
Invoked
Another simple solution is this:
List.ContainsAny(Text.SplitAny("This is a test string", " "), {"dog","string","bark"})
It transforms the text into a list because there we find a function that does what you need.
If it's a specific (static) list of matches, you'll want to add a custom column with an if then else statement in PQ. Then use a filter on that column to keep or remove the columns. AFAIK PQ doesn't support regex so Alexey's solution won't work.
If you need the lookup to be dynamic, it gets more complicated... but doable you essentially need to
have an ID column for the original row.
duplicate the query so you have two queries, then in the newly created query
split the text field into separate columns, usually by space
unpivot the newly created columns.
get the list of intended names
use list.generate method to generate a list that shows 1 if there's a match and 0 if there isn't.
sum the values of the list
if sum > 0 then mark that row as a match, usually I use the value 1 in a new column. Then you can filter the table to keep only rows with value 1 in the new column. Then group this table on ID - this is the list of ID that contain the match. Now use the merge feature to merge in the first table ensuring you keep only rows that match the IDs. That should get you to where you want to be.
Thanks for giving me the lead. In my own case I needed to ensure two items exist in a string hence I replaced formula as:
List.AllTrue(List.Transform({"/","2017"},(substring) => Text.Contains("4/6/2017 13",substring)))
it returned true perfectly.
You can use regex here with logical OR - | expression :
/dog|string|bark/.test("This is a test string") // retruns true

Searching for unmatched ntheames when comparing spreadsheets

In one spreadsheet I have 3 columns with a first and last name of a person combined. In the 2nd spreadsheet, I have column a = first name and column b = last name.
I want to know which names in spreadsheet one cannot be found in spreadsheet two. I also need to verify the data to make sure that the formula was accurate on finding the correct lookup.
Do I have to combine my columns in spreadsheet 2 to make the first and last name in the same column to make this work?
Which formula would you use for either scenario?
Use this:
=ISNA(MATCH($A1&" "&$B1,Sheet2!$A:$A,FALSE)))
Where (in order):
A1 is the first name column in Sheet1
B1 is the last name column in Sheet1
Sheet2 is the sheet that has the data stored as names separately
$A:$A is the rows that have the two names together
FALSE is because it's an exact match
This will return FALSE if the element does not exist, and TRUE if it does
You can also use:
=VLOOKUP($A1&" "&$B1,Sheet2!$A:$D,3,FALSE)
If you want to retrieve data for a match.
Finally, if you need to do your lookups the other way, take a look at this thread for some ideas on how to split the string into two pieces.