Print one result after combining multiple expression results using jq - if-statement

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"

Related

Two conditions in Google Sheets Function

I have a question on using two functions with an if statement in Google Sheets as one complete function. Both variables have to be true, otherwise it returns false. I need one function to check the date 20 months back from today. If said cell is less than today's date 20 months back it's true, naturally. However, for the complete function to return true it also searches for another text value in another cell and has to be an exact match. Both conditions have to be true (the date and the exact match) for the function to be true. So if the date in the cell is less than today's date 20 months back and the text value in the other cell is an exact match, function is true.
Problem is that it seems like the date function does not seem to apply.
=IF(D2<DATE(YEAR(TODAY()),MONTH(TODAY())-20,DAY(TODAY())),AND(REGEXMATCH(M2,"text")),TRUE,FALSE)
You current formula is not set up correctly (nor logically). Given only what you've shown here, this should work:
=IF(AND( D2<DATE(YEAR(TODAY()),MONTH(TODAY())-20,DAY(TODAY())), REGEXMATCH(M2,"text") ),TRUE,FALSE)
Notice that the AND( ) contains both conditions here, whereas your original formula had it only around the second condition.
However, a shorter version of this would be as follows:
=AND( D2<DATE(YEAR(TODAY()),MONTH(TODAY())-20,DAY(TODAY())), REGEXMATCH(M2,"text") )
... since the result of a properly functioning AND( ) is always TRUE or FALSE anyway.
It looks like you're supplying 4 arguments to the IF statement:
=IF(DATECHECK,AND(TEXTCHECK),TRUE,FALSE)
The IF statement expects 3 arguments instead. 1) the condition, 2) the value if true, and 3) the value if false. You can combine your two conditions using an AND statement like this:
AND(DATECHECK,TEXTCHECK)
The final formula would then be:
=IF(AND(D2<DATE(YEAR(TODAY()),MONTH(TODAY())-20,DAY(TODAY())), REGEXMATCH(M2,"text")),TRUE,FALSE)

Power Query M - We cannot convert the value null to type Logical

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

IF statement in Excel with date1 and endtime variables

I have got an excel which has an formula and I am not able to understand how its working.
Here is the formula
=IF(C6=date1,0,IF(D6-endtime<=0,0,IF(D6-endtime>0,(D6-endtime)*1440)))
Can someone spends few minutes to post an explanation, how it works
Thanks,
Try looking at it like this:
=IF(C6=date1,
0,
IF(D6-endtime<=0,
0,
IF(D6-endtime>0,
(D6-endtime)*1440
#no "else statement" here!
)
)
)
So the first thing checked is if the cell C6 equals date1, which I think is a named range. If they are equal, then the whole equation resolves to the next line, 0
If they are not equal then D6-endtime is evaluated, if it is less than or equal to zero, then the equation resolves to zero.
If D6-endtime is greater than 0, then the next test is true and the whole equation resolves to (D6-endtime)*1440. There is no else in this last test because equation assumes D6-endtime will always be numeric.
Here's how I understand IF statements work in excel
=if(logical test,value if test true,value if test false)
For logical test, you have to use something that resolves to TRUE or FALSE, or you can specify TRUE or FALSE directly(but then you don't need an IF statement)
value if test true, if the test resolves to TRUE(like 1=1), then the cell will display this value, and supply this value to other functions
value if test false, if the test resolves to FALSE(like 1=0), then the cell will display this value, and supply this value to other functions.
You can omit value if TRUE/FALSE, and excel will return TRUE or FALSE after evaluation of the statement.

what is wrong with my if -condition?

So I've been trying to figure out what is wrong with my if-condition, but I am getting nowhere. I am still new to R, so maybe I am not understanding some very basic concept here?
I have a dataframe (dc) to which I appended a column with logical "FALSE". Now I want to change each FALSE into a TRUE based on the values in two columns of dc (dc$Probe and dc$Resp) that I specified using regexpr().
What it does so far is that, for both if-conditions, it changes each FALSE into TRUE regardless of the values in column 5 of dc. When I run the if-conditions seperately, I can see that they seem to be working fine on the OR-part of the condition, meaning the code only generates TRUE when the strings in dc$Probe match one of the strings specified in the OR-part. However, the AND-part seems to be ignored? Thus, when I run the complete code, I get a column with only TRUE, which is not what I want.
Edit: I should get a TRUE only if the string in Probe ends in a certain pattern (as specified in either of the two if conditions I wrote) and if the corresponding value in Resp is a "100" for the patterns specified in my first condition or a "200" for the patterns specified in my second condition. Thus, for strings ending in (sg|s|w1|w3|s1|s2), Resp must be "100" to get a TRUE and for strings ending in (\d\dg|\d\d), Resp must be "200" to get a TRUE. All other cases should be FALSE. For example, if a string ends in s1 and the corresponding value in Resp is 200, the code should return FALSE.
Edit: Some example data:
>dc<-data.frame(Subject=rep("SN",6), item.c=(1:6), Stim=c("XYZc02s03","XYZc01s30","XYZc02s29", "XYZc01s38", "XYZc02s11", "XYZc06w21"), Probe=c("XYzf02s03","XYZf01s30g","XYZf02s29w1","XYZf01s38sg","XYZf02s11s","XYZv06w21s1"), Resp=c(200, 100, 100, 100, 100, 200))
This is my code:
>dc$Resp<-as.character(dc$Resp) #column 5 in dc
dc$Probe<-as.character(dc$Probe)
dc$correct_response <- FALSE
for (i in 1:nrow(dc)) {
if (regexpr("^.*sg$", dc$Probe[i])==1 || regexpr("^.*s$", dc$Probe[i])==1 || regexpr("^.*w1$", dc$Probe[i])==1 || regexpr("^.*w3$", dc$Probe[i])==1 || regexpr("^.*s1$", dc$Probe[i])==1 || regexpr("^.*s2$", dc$Probe[i])==1 && dc[i,5]=="100") {(dc$correct_response[i]<- TRUE)}
if (regexpr("^.*\\d\\dg$", dc$Probe[i])==1 || regexpr("^.*\\d\\d$", dc$Probe[i])==1 && dc[i,5]=="200") {(dc$correct_response[i]<- TRUE)}
}
Is there something wrong with the regular expressions I am using? I checked them with glob2rx() and it seems like they are ok...Is my use of "OR" (||) or/and "AND" (&&) incorrect? How do I implement the AND-part properly? I have also tried the following code for the AND-part, but it didn't change anything:
regexpr("200", dc$Resp[i])==1
I read the R-help on regular expressions and control flow, but I still don't see what I am doing wrong. Consulting other webpages on logical expressions did not help me either.
Please help!
Im wondering if it can all be reduced to the following:
dc<- read.table(header=T,text="Subject item.c Stim Probe Resp
SN 1 XYZc02s03 XYzf02s03 200
SN 2 XYZc01s30 XYZf01s30g 100
SN 3 XYZc02s29 XYZf02s29w1 100
SN 4 XYZc01s38 XYZf01s38sg 100
SN 5 XYZc02s11 XYZf02s11s 100
SN 6 XYZc06w21 XYZv06w21s1 200")
cond1<-regexpr("^.*(sg|s|w1|w3|s1|s2)$", dc$Probe)==1 & dc$Resp==100
cond2<-regexpr("^.*(\\d\\dg|\\d\\d)$", dc$Probe)==1 & dc$Resp==200
dc$correct_response<-cond1|cond2
For one thing, you are missing a logical operator between the 2nd and 3rd clauses of your first if statement.

XSL - Does evaluating conditional expressions "shortcut"?

Given an XSL 'If' statement:
<xsl:if test="a = 'some value' and b = 'another value'">
If a does not equal 'some value', is the value of b still checked? (As if the first test is false, the only outcome of the and is false.) This is what languages like C# do - I was wondering if the same goes in XSL. Does it depend on the engine/parser?
Yes, it is called lazy-evaluation or short-circuiting, and xsl supports it. See: http://www.w3.org/TR/xpath#booleans
An and expression is evaluated by
evaluating each operand and converting
its value to a boolean as if by a call
to the boolean function. The result is
true if both values are true and false
otherwise. The right operand is not
evaluated if the left operand
evaluates to false.
Yes, this is depending on the implementation. But since XSLT is side-effect-free (as opposed to C# and other languages, where a function call chainging some state or even an assignment can be in the expression), this does not matter.