How can I use 'OR' in GREL if statement?
I want to construct something like this
if(contains(value, "this") OR contains(value, "that"), True, False)
This worked for me
if(or(contains(value, "this"), contains(value, "that")), True, False)
Related
I want to have the output of either true or false.
I have this syntax
.[] | (if (select(.displayName=="display")) then "yes" else "no" end)
This is the json source
[{"displayName":"display","message":"bla bla"}, {"displayName":"test","message":"bla bla"}]
I only need to query against the array and if the value im looking for exists in one of them I need the output to be "yes", and if it doesnt a "no". So a single output value is what I am looking for.
This evaluates to "yes", however, if the value display is not present in the json, it does not output "no", its just empty. Can anyone tell me why?
Here is a snippet to try: https://jqplay.org/s/WKcZh91hk8L
The idea is right, but you shouldn't be using select for this. The way select works is by evaluating the boolean expression provided inside (..) and returning the object, if the expression evaluates to true and skip the object from being considered/printed on false.
On your code, for the object that evaluates to false, select is evaluated as select(false) which as explained above returns nothing - this is not evaluated to boolean false condition. i.e. the return value of select is not a boolean value.
To use explicit boolean conditions, drop the select from expression and to summarise the result to a single value do something like below
if ( [ .[] | .displayName == "display" ] | any ) then "yes" else "no" end
Demo on jqplay
The way this works, is any can evaluate an array of boolean values and produces true as output if any of the elements of the array are true. We collect the match condition results to the array and apply any on it. i.e. [true, false] evaluates to true, thereby printing yes only once
A concise solution is possible with select:
select( any(.displayName=="display") ) | "yes" // "no"
My main need is to use something like switch cases in other programming languages.
I have a ?x g1:state (true OR false OR UNDEF) The latest is a variable incoming from a REST API and I want to perform different things based on that value
What I want to achieve is: if ?x g1:state true, bring me all the true triples. If it is false, bring me all the false triples that correspond. If the latest is undefined, bring me all the triples with whatever values. Something like this.
Bind ( if ('+test+'=true, ?x g1:state true, Bind ( if ('+test+'=false, ?x g1:state false, ?x g1:state ?y1) as ?y) as ?xf)
Ι bypassed this obstacle with a different approach, outside the sparql query
var booleanvar = req.params.booleanvar
if (booleanvar==='true' || booleanvar==='false'){
} else {
booleanvar= '?'+booleanvar
}
and then I just added a simple triple statement into the query
?x g1:state '+booleanvar+'
Basically the title. Making a big Analytics based spreadsheet that tests for a lot of date conditions. I would like to know if the AND operator supports short-circuit evaluation.
"short-circuit evaluation" is basically nesting of IF functions:
=IF(A1=1, TRUE,
IF(A1=2, TRUE))
for multi-criteria it would be:
=IF(AND(A1=1, B1=1), TRUE,
IF(AND(A1=2, B1=2), TRUE))
when it comes to arrays/ranges it will be like this:
=ARRAYFORMULA(IF((A1:A=1) * (B1:B=1), TRUE,
IF((A1:A=2) * (B1:B=2), TRUE)))
In Power BI I have an M Query that tests for the value in a column equaling or not equaling to null.
When I add the statement for [Sale.Revenue] <> null I get an error however it works fine for the [UserRole.Name] = null it works fine. Tested just by removing the statement and adding it back.
We cannot convert the value null to type Logical.
This seems like it should work but just can't figure it out.
add_user_role_group = Table.AddColumn(
join_expand_sale,
"UserRole.Group1",
each (
if [UserRole.Name] = null and
[Sale.Revenue] <> null then
"Group1"
else if Text.Contains([UserRole.Name], "Manager") then
"Group2"
else
"Undefined"
)
)
I am sure it is something glaringly obvious :/ Thanks for your thoughts on this.
One of your rows has a null value for both UserRole.Name and Sale.Revenue. You need to check for that explicitly, and then add it to the "Undefined" group.
What happened is that the first condition fails because Sale.Revenue is null. The second condition calls Text.Contains, which returns null when [UserRole.Name] is null (Text.Contains returns a nullable logical value). null is not true or false, so you get the error.
After a such journey, finaly I found Text.Length !!
You can solve your problem like this:
if Text.Length([UserRole.Name]) = 0 and
Text.Length([Sale.Revenue]) > 0 then
I hope I have helped you.
Reference: Power Query M - Text.Length
Your issue is in the Text.Contains formula. You create an if statement that expects an expression that returns either true or false.
When the Text.Contains formula contains a null value, it returns 'null' as answer, and not true or false. You can adjust your code:
Text.Contains([UserRole.Name], "Manager")
To
Text.Contains([UserRole.Name]??"", "Manager")
The ?? is the COALESCE operator. In case it finds a null value, it now treats it as "". Instead of returning null it now returns true or false.
More on text functions in this article: https://gorilla.bi/power-query/text-functions/
Enjoy Power Query,
Rick
I'm currently trying to validate if a string is exactly true or false as I'm building a spreadsheet importer using CF10.
My users will either enter in one of the following.
NULL, which will equate to false
TRUE which obviously equates to true
FALSE which again will equate to false.
I'm trying to use the isValid('boolean',variable.data) to validate if the data is in fact a boolean string or not. However after reading quite a few posts I can see that it will validate true if its a positive number and false if it is a negative one etc.
I'm wondering if anyone has an easy fix to getting boolean validation working for strings easily ? Is this more a regular expressions scenario to make it work properly ?
Any help greatly appreciated.
Assuming you mean the literal string "NULL", one option is using list functions. Search a list of allowed values, ie "true,false,null". If the entry is found, it is valid:
<cfif listFindNoCase("true,false,null", theValue)>
this is a valid boolean value
<cfelse>
not found. do something here...
</cfif>
Then a simple string comparison would return false for everything other than the literal string "true":
isTrue = compareNoCase(e, "true") eq 0 ? true : false`
If you need to evaluate whether a string is exactly "true" or "false", then you are not doing a boolean comparison, you are doing a string comparison.
So to that end, you'd be wanting to use compare() or compareNoCase() (depending on how rigid you need to be).
You could just do some logic or am I missing something.
<cfset booleanResult = enteredValue eq 'TRUE' ? true : false />