How to use AND & OR in powerquery Fromulas? - powerquery

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).

Related

If results are not >= "Number" then show blank

New to building Crystal Reports and SQL.
I'm trying to write a script to where if results is >= 12.1 then show result else show no results.
Same goes for the <=9.9.
Here is what I have so far:
if {Test.Name} = "xyz" and {TestResults.numresult}>= 12.1 then {TestResults.numresult} else "";
Below is an image showing the same results across the board. I just want the results to show when its <=9.9 or >=12.1.
Hope this make sense.
Your statement returns a number from one branch and a string from the other. It must return the same data type.
One option is to use a True/False expression in a Suppress expression.
Another option is to return a zero in the other branch and use number formatting to suppress if zero (it's a built-in option for numbers).
Another option is to modify your expression so it returns a string from both branches. For example:
if {Test.Name} = "xyz" and {TestResults.numresult}>= 12.1 then ToText({TestResults.numresult}, 1, ",") else "";
The 1 argument requests 1 decimal point. The "," argument requests a comma as thousands separator. You can adjust those to match your number formatting requirements.

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

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

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()

PowerQuery - Check if Column value equals true

I have build several additional columns in PowerQuery based on my source data.
This includes two "TextContains" columns which only return "TRUE" or "FALSE".
I now want an additional Column highlighting the different service types and used this:
if [PSTag] = "PS" then "PS"
else if [Trainingskit] = "TrainingsKit" then "Training"
else if [Training] = "Training" then "Training"
else if [HardwareService] = "TRUE" then "HardwareService"
else if [TelephoneService] = "TRUE" then "TelephoneService" else "NonService"
It works fine for the first three IF statements, but doesn't work at all for the Columns containing only "TRUE" or "FALSE".
The first three contain either e.g. "PS" or "NonPS" or "Training" or "NonTraining"
I'm sure I'm "just" missing a very fundamental here.
Any help is highly appreciated.
Columns containing only TRUE or FALSE are likely the "True/False" datatype. Their values appear in italic font in the Query Preview window.
If that is the case with your [HardwareService] and [TelephoneService] columns then I would remove the quotes e.g. write something like:
... if [HardwareService] = true then ...
You should write in lowercase:
...
else if [HardwareService] = "true" then "HardwareService"
else if [TelephoneService] = "true" then "TelephoneService"
...

struggling with my pseudocode

I'm about to build a program written in pseudocode. I've already done most of the work , but I'm stuck on the code and I don't know what to do exactly. im a begginer and not everything is clear to me ...in one of the tasks i have to do , i have to make the program ask for the players name , which will be stored as a string then the program has to check if it exceeds the limit between 2/20 characters and inform the user if the input is wrong . i have researched and tried to figure out how i might be able to fix my code but i have a really short amount of time left and coudn't find anything regarding my problem :/ . this is the code ive done for this specific task. i know its wrong but i just dont know how to fix it . any help with be much appreciated . Thanks in advance :)
pseudocode:
// Getting user's name
valid = false
loop until valid is equal to true
Output" please enter your name "
Input playName
If (playName is => 1)AND(=<20)then
Valid = true
Otherwise
output "name exceeds the character limit"
I'm not sure what the syntax of your pseudo code is but :
assuming tabulation has meaning, you may have forgot to indent some lines to include them in the loop
'valid' is first declared with a lower case first letter so you may continue referencing it same way in line "Valid = true" -> "valid = true"
In the 'If' you want to test the lenght of the String and not compare the string to an int so maybe call a function length(String) that would return the length of the string or access a string.length attribute (as you wish in pseudo code)
You want the playName to be superior or equal to 2 "length(playName) >= 2" and inferior or equal to 20 "length(playname) <= 20"
The commonly used keyword meaning Otherwise is 'Else' as in
IF (Condition) THEN (code) ELSE (code)
I may modify you code like this :
// Getting user's name
valid = false
loop until valid is equal to true
Output" please enter your name "
Input playName
If (length(playName) >= 2) AND (length(playName) <= 20)
Then
valid = true
Else
output "name exceeds the character limit"

Resources