PowerQuery - Check if Column value equals true - powerquery

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

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

How can I match part of a string to a list in google sheets?

I need to create a google sheet that I can enter a string of text which will then crosscheck against a list of about 800 keywords/phrases and if there is a match, I need it to set a true flag on the keyword/phrase it matches on. For example:
string: hihowareyou doing
List:
Example = False
Example = False
Example = False
Example = False
Example = False
hi = True
Example = False
Example = False
are = True
Example = False
Example = False
Is this possible and can someone tell me how to set it up?
Assuming text in B1 and list in A2 etc then in B2:
=ArrayFormula(REGEXMATCH(B$1,A2:A))
For case insensitivity you might force text and list elements into the same case, for example:
=ArrayFormula(REGEXMATCH(lower(B$1),lower(A2:A)))
One way to restrict the cells visibly populated in ColumnB:
=ArrayFormula(if(len(A2:A)=0,"",REGEXMATCH(lower(B$1),lower(A2:A))))
Another way (frees up the "empty" cells"):
=array_constrain(ArrayFormula(REGEXMATCH(lower(B$1),lower(A2:A))),counta(A:A),1)

What's Visual FoxPro's string lt(<)/gt(>)/lte(<=)/gte(>=) comparison rule?

?"ABC">"A" returns .F.
?"ABC">"B" returns .F.
?"ABC">"a" returns .T.
This seems not an ASCII comparison (as in other common programming languages).
I'v searched MSDN, however Relational Operators only explained comparison across numeric type or boolean. String and Value Comparison didn't mention lt/gt/lte/gte operators.
Why string comparison work like this? What's VFP's internal rules for string lt/gt comparison?
Actually VFP's string comparison is not much different than in other languages (some languages -ie: C#- doesn't even let you use >=, >, <= and < with strings). However, in VFP there is one setting that affects string comparison and it is 'SET EXACT'. Other than that, VFP strings are case sensitive, thus "ABC" > "a" is false (alpha sort is based on their ASCII character codes).
With default SET EXACT OFF, strings are compared up to the length of right operand. ie:
"ABC" = "A"
is compared like:
"A" = "A"
thus they are equal, so "ABC" > "A" is false (and also "ABC" < "A" is false, "ABC" = "A" is true).
To recap, with default SET EXACT setting, which is OFF:
"ABC" = "A" (but "A" = "ABC" is false - compare up to right operand's length).
AND, the alphabetic ordering use ASCII code.
SET EXACT OFF && Default if not set explicitly
? "ABC" = "A" && .T.
? "ABC" > "A" && .F.
? "ABC" < "B" && .T.
SET EXACT ON
? "ABC" = "A" && .F.
? "ABC" > "A" && .T.
? "ABC" < "B" && .T.
A special note: If you are doing this equality check in SQL commands, then there the ANSI rules are applied and SET EXACT has no effect there. By default SET ANSI is OFF and "ABC" = "A" (and due to SQL rules, changing the operands' left and right sides wouldn't matter, thus "A" = "ABC" is also true). ie:
Select * from myTable where firstName = "A"
would return records having firstName "Anthony", "Alice" ... and so on.
There is a special == (exactly equal) operator in VFP that works independantly from SET EXACT or SET ANSI setting. Beware its behavior is different with regular commands and SQL commands. With regular commands it really means 'exactly equal' including the trailing spaces in both of the compared strings. ie:
? 'ABC' == 'ABC ' && false, regardless of SET EXACT
With SQL however (regardless of SET ANSI setting):
select * from myTable where myField == 'A'
select * from myTable where myField == 'A '
select * from myTable where myField == 'A '
all mean the same and searches for records whose myField content is A (whatever the field size is - trailing spaces on both sides of the comparison are ignored).
Note: I think you can request document topics to be added in stack overflow. VFP documentation has just started and no additions done yet, AFAIK.

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