Spotfire: Can you write a case statement based on a column containing a substring - filter

I want to create a calculated column based on a substring. I can't find the syntax to do something like the following
case
when [ProjectName] contains "substring" then [Value]
end
For example, when the [ProjectName] contains "overhead" then "overhead"
[ProjectName] would be equal to "Project 1 Overhead", "Project 2 billable", or something like that.

The easiest solution would be to use FIND() with an IF() or CASE() statement.
If(Find("overhead",Lower([ProjectName])) >0,[Value],"FALSE")
CASE
WHEN Find("overhead",Lower([ProjectName])) >0 THEN [Value]
END
Just remember, Find() is case sensitive.
If this is a step in replacing the sub-string with a value, then you would want to use RXReplace()

Related

How do I identify whether a column entry starts with a letter or a number using m code in power query?

I have a column that contains either letters or numbers. I want to add a column identifying whether each cell contains a letter or a number. The problem is that there are thousands of records in this particular database.
I tried the following syntax:
= Table.AddColumn(Source, "Column2", each if [Column1] is number then "Number" else "Letters")
My problem is that when I enter this, it returns everything as "Letter" because it looks at the column type instead of the actual value in the cell. This remains the case even when I change the column type from Text to General. Either way, it still produces "Letter" as it automatically assigns text as the data type since the column contains both text and numbers.
Use this expression:
= Table.AddColumn(Source, "Column2", each if List.Contains({"0".."9"}, Text.Start([Column1], 1)) then "Numbers" else "Letters")
Note: It would have been smart to add sample data to your question so I wouldn't have to guess what your data actually looks like!
Add column, custom column with
= try if Value.Is(Number.From([Column1]), type number) then "number" else "not" otherwise "not"
Peter's method works if the choice is AAA/111 but this one tests for A11 and 1BC as well

SPSS Syntax Concatenate Case Values From Single Column

I am trying to build a string of values to be inserted into an SQL IN list. For example -
SELECT * FROM TABLE WHERE field IN ('AAA', 'BBB', 'CCC', 'DDD')
The list that I want needs to be constructed from values within a single column of my dataset but I'm struggling to find a way to concatenate those values.
My first thought was to use CASESTOVARS to put each of the values into columns prior to concat. This is simple but the number of cases is variable.
Is there a way to concat all fields without specifying?
Or is there a better way to go about this?
Unfortunately Python is not an option for me in this instance.
A simple sample dataset would be -
CasestoConcat
AAA
BBB
CCC
DDD
You can use the lag function for this.
First creating a bit of sample data to demonstrate on:
data list free/grp (F1) txt (a5).
begin data
1 "aaa" 1 "bb" 1 "cccc" 2 "d" 2 "ee" 2 "fff" 2 "ggggg" 3 "hh" 3 "iii"
end data.
Now the following code makes sure that rows that belong together are consecutive. You can also sort by any other relevant variable to keep the combined text in a specific order.
sort cases by grp.
string merged (A1000).
compute merged=txt.
if $casenum>1 and grp=lag(grp) merged=concat(rtrim(merged), " ", rtrim(lag(merged))).
exe.
At this point if you want to just keep the line that has all the concatenated texts, you can use this:
add files /file=* /by grp /last=lst.
select if lst=1.
exe.

How to use AND & OR in powerquery Fromulas?

I want to use Power Query Text.Contains function to check if the text contains one of my specified values.
Example:
If Text.Contains([#"My Columns"], ("a" OR "b" OR "c")) Then "Found" Else "Not Found"
I've already tested syntax like:
Text.Contains([#"My Columns"], {"a","b","c"})
Text.Contains([#"My Columns"], ("a"||"b"||"c"))
But I got error message, does anybody know how can I use AND & OR in Power Query's Formulas?
I would use Text.PositionOfAny, something like:
if Text.PositionOfAny( [My Columns] , { "a", "b" , "c" } ) = -1 then "Not Found" else "Found"
Note PQ formulas are (irritatingly) case sensitive so "if" "then" and "else" must be lower case, and the function name must be mixed case exactly as above.
The above code assumes you have a single column named "My Columns". If you actually want to search across multiple columns, then just add a "Merge Columns" step upstream (from the Add Column ribbon).

Check for multiple values within CASE statement

How do I check for multiple things in my case statement without writing multiple lines, checking for each case.
I tried this and it's very ugly.
I want something clean like below but can't make it work.
select CASE
WHEN (UPPER(NAME) IN ('%AVG%', '%AVERAGE%') AND
FORMATTED_ENTRY NOT IN('<', '>'))
THEN FORMATTED_ENTRY END actual_avg
FROM VALUES_TABLE
Assuming that you want wildcard matching on those percent signs:
CASE WHEN (
( UPPER(NAME) LIKE '%AVG%' OR UPPER(NAME) LIKE '%AVERAGE%' )
AND FORMATTED_ENTRY NOT IN('<', '>')
)
THEN FORMATTED_ENTRY END actual_avg

Whats the XPath equivalent to SQL In query?

I would like to know whats the XPath equivalent to SQL In query. Basically in sql i can do this:
select * from tbl1 where Id in (1,2,3,4)
so i want something similar in XPath/Xsl:
i.e.
//*[#id= IN('51417','1121','111')]
Please advice
(In XPath 2,) the = operator always works like in.
I.e. you can use
//*[#id = ('51417','1121','111')]
A solution is to write out the options as separate conditions:
//*[(#id = '51417') or (#id = '1121') or (#id = '111')]
Another, slightly less verbose solution that looks a bit like a hack, though, would be to use the contains function:
//*[contains('-51417-1121-111-', concat('-', #id, '-'))]
Literally, this means you're checking whether the value of the id attribute (preceeded and succeeded by a delimiter character) is a substring of -51417-1121-111-. Note that I am using a hyphen (-) as a delimiter of the allowable values; you can replace that with any character that will not appear in the id attribute.

Resources