IF statement equivalent in Google Data Studio - if-statement

I am trying to replicate the Excel formula
IF(AND('cost'> 0, 'orders' = 0),"TRUE") in Google Data Studio using the CASE statement:
CASE
WHEN COST > 0 AND ORDERS = 0 THEN "TRUE"
END
However, I receive the following error:
Only numeric literals are allowed if aggregated fields are used in CASE
Is there a way I can fix this?

Summary
Use Either #1 or #2.
To elaborate on the difference between using "True" and TRUE, the Semantic Type of:
#1 is Boolean (TRUE or FALSE);
#2 is a Text field (note the quotes around "TRUE" or "FALSE"):
1) IF
The Conditional Function, IF, was introduced in the 11 Mar 2021 Update; thus, tweaking the IF function in the question now works as expected in Google Data Studio.
Note: The Calculated Field below is a Boolean field; thus, to create a Text field, replace TRUE with "TRUE" and replace FALSE with "FALSE":
IF(Cost > 0 AND Orders = 0, TRUE, FALSE)
2) CASE
Alternatively, the CASE statement proposed in the question should work as expected. That said, the CASE statement below explicitly states the ELSE clause.
Note: The Calculated Field below is a Text field; thus, to create a Boolean field, replace "TRUE" with TRUE and replace "FALSE" with FALSE:
CASE
WHEN Cost > 0 AND Orders = 0 THEN "TRUE"
ELSE "FALSE"
END
Editable Google Data Studio Report and a GIF to elaborate:

The Google Data Studio team released a new IF function and a more convenient way to use CASE.
The update: Release notes (11 Mar 2021)
IF(Actual Sales > Forecast Sales, Bonus * 1.2, Bonus)
The documentation is in IF.
If it's not working, then use the below CASE documentation link. It is supposed to appear on the sidebar just where the two CASE support articles are.
CASE Premium Status
WHEN "Platinum" THEN CONCAT(Site URL, "platinum_welcome.html")
WHEN "Gold" THEN CONCAT(Site URL, "gold_welcome.html")
WHEN "Silver" THEN CONCAT(Site URL, "silver_welcome.html")
ELSE CONCAT(Site URL, "welcome.html")
END
CASE (simple)

I don't think there is an IF statement in Google Data Studio; however, your CASE WHEN should work. I tried to reproduce your problem:
I created a Google Sheet with two columns, cost and orders, and filled in some numbers
I created a data source connected to this sheet
I made a table in a Google Data Studio report using this data source, and added a calculated field with your exact formula
It didn't give me any error messages and the field values are as expected
From the error message it feels like this is related to where your data source comes from.

Related

Having trouble with IF NOT statments work in Sheets

this might be an easy fix but I just can't figure it out. I'm trying to get this IF statement, if the cell is not FALSE to return value, else to return a certain string. I've tried a couple of ways but I can't make the right combination. And I have a similar issue with excluding the FALSE value from a UNIQUE search statement.
This is the sample. Sorry if I'm missing on smth very obvious
Regarding your concern in setting a value if the cell is NOT FALSE, you can use this formula in Row 2:
=arrayformula(if(A2:A<>"",if(A2:A=FALSE,"Blank",iferror(year(to_date(datevalue(A2:A))),"No year found")),""))
What it does?
Check if cell value is FALSE, If yes, set cell value to "Blank", else convert the date string to value using datevalue(). Then use to_date() to convert date value into a date object. Use year() to get the year. Use iferror() to set a default value if the formula encountered an error (when your string is not a valid date string)
Loop each row using arrayformula()
Output:
Regarding filtering your data without FALSE:
=filter(A2:A,A2:A <> FALSE)
What it does?
Using filter(), filter the data if the cell value is not FALSE
Output:
Note:
You can also use UNIQUE() once you filter your data
=unique(filter(A2:A,A2:A <> FALSE))

DAX problem, filter/relation is ignored when I use IF in the RETURN clause

I'm getting an unexpected result when I use an IF in the RETURN clause of a DAX expression. If I don't use the IF, but instead just a variable, then the result is ok.
I've created a test scenario to explain my problem:
I have two test tables:
Table: "Test Object"
Table: "Test Group"
These have a unidirectional relation on "Group code"
I have created a measure "Test measure":
This gives the correct result:
I have set a page filter to only show Group Code "G01".
This all works ok up to this point.
But it goes wrong when I use an IF function:
I then get the following (incorrect) result. Apparently the relation and/or page filter seems to be ignored now:
NB: The result is the same regardless of from which table I use the "Group code" field.
What am I missing here?
I've created a PBIX file that shows the problem:
https://www.dropbox.com/s/76ld1kv503ul6nm/DAX%20problem%20with%20IF.pbix?dl=0
This is called "Auto-Exist" in PBI:
https://www.sqlbi.com/articles/understanding-dax-auto-exist/
If you look closer to the results, you'll notice that your report shows all possible combinations between Group Codes and Object Codes.
This is happening whenever you use a combination of fields from the different tables in a report: PBI first creates a cross-join between these fields, and then eliminates those combinations that result in blanks, so you only see meaningful combinations.
However, you IF statement overrides this logic - you are returning a result always, even if a combination is blank (Blank < 40 test returns "low end" because blank is treated as zero).
To fix it, calculate results only if the variable is not blank, i.e:
Price category =
var lowestPrice = MIN(Object[Price])
var result = IF( NOT ISBLANK(lowestPrice), IF(lowestPrice < 40, "Low end", "High end"))
Return result
You will get:
P.S. Page filter is irrelevant here, it simply filters the table after it's calculated.

Extracting String Portions in SQL using Regular expressions

Hi All,
I have a query related to Regular expressions in SQL.
I have a case where a portion of string has to be extracted from a column. The portion of that column will be prefixed with my column A. Please see the screenshot for the sample data. I have also added the output expected in a separate column (highlighted in green).
Scenarios:
Now if a column value has more than 1 unique number then that has to be shown up with Null
Eg: To verify CAN06010025, CAN06010026 & CAN06010030 after the approval.
In the above string I have more than 1 number(bold portion)
and this case should be ignored (meaning it has to give me Null Value).
If there is only one number and if it is repetitive then I have to consider that case and extract the portion of String..
Eg: Project USA12: Id USA12S001: Contact required -USA12S001- form to be updated
In this example, the portion I wanted to extract is repetitive and I am looking to extract the highlighted portion alone.
The same applies to the other cases as well.
I tried with the below sql. The challenge is my Col A can also be present in Col B (Line 2 in screenshot) and this code is considering my Col A portion when I count with REGEXP_COUNT function and is giving me the value as Null. My expectation is to extract that USA12S001 portion from the column.
Could you please help in achieving this where the above two conditions satisfies.
SQL:
SELECT
ColA,
ColB,
case when REGEXP_COUNT(ColB,ColA) >2 THEN NULL
ELSE REPLACE(REPLACE(concat(regexp_substr(ColB,ColA||'([[:alnum:]]+\.?)'),
nvl(regexp_substr(ColB,ColA||'(\-[[:digit:]]+)'),
regexp_substr(ColB,ColA||'([[:space:]]\-[[:space:]][[:digit:]]+)'))),
' ',''),'.','')
END AS Result
FROM
table
Test Data:
Col A
CAN06
USA12
USA27
HUN04
CAN05
USA24
CAN06
Col B
to verify CAN06010025, CAN06010026 & CAN06010030 after the approval
Project USA12: Id USA12S001: Contact required -USA12S001- form to be updated
Project USA27: Id: USA27S001: Prod
To review id HUN04S002-HUN04S004 after the due date.
ID: CAN05S005 with the details as CAN05S005 are completed.
Project USA24: Id: USA24S009: Data Issue
"Project: Subject CAN06S009: V2 & V3- Id CAN06S010: V1"
If the REGEXP_COUNT is the only issue, then the answer is simple: change
case when REGEXP_COUNT(ColB,ColA) >2
to:
case when REGEXP_COUNT(ColB,ColA || '[[:alnum:]]') >2

RPA(blueprism) date validation

I am trying to validate date which is coming from an excel sheet , the format should be in dd/mm/yyyy
i tried with regex pattern [0-9]{2}/[0-9]{2}/[0-9]{4}
but this won't work with single digit date and we since we cannot add 0 at start in excel sheet this pattern ain't working. (this is for blueprism tool which have a action for regex matching]
To build in the resiliency you require, you'll have to accept either 1 or 2 digits for both dd and mm:
[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}
Since you are mentioning that you are working with BluePrism, are you sure that you really need Regex for validating dates? Because BP has built-in feature for that callable directly inside a Calc stage - checkout following example (you can see Expression of selected Calc stage in top Expression bar).
The function used for validating dates is IsDate([Some date as string]), result is saved into a Flag data-item.
After the check you can use that Flag data-item in a Decision block and do whatever you consider appropriate if a date is not an actual date.
Note: of course, if you are working with lists/datatables in a Code stage instead of iterating over a collection in process layout, then you need something else, but this might be still helpful.
In Code stage I would probably simply use DateTime.Parse(String) method which is able to automatically convert a date in a form of a String into a DateTime object instance; example:
' DateTime.Parse throws an Exception if parsing failed.
Dim valid As Boolean = False
Try
Dim d = DateTime.Parse(First_Date)
valid = True
Catch e As Exception
valid = False
End Try
See more about parsing dates using DateTime.Parse at MSDN: https://msdn.microsoft.com/en-us/library/1k1skd40(v=vs.110).aspx
There is also a nice post about parsing dates here: https://stackoverflow.com/a/18465222/7439802
In blue prism you can use
FormatDate(Now(), FormatOfDate)
For comparing two dates, initially Convert(" FormatDate ") in same format and then you can compare.
For " FormatDate " option you can refer Help of Blue prism and search for dateadd --> select the Calculation and decision

Date ranges in where clause of a proc SQL statement

There is a large table containing among other fields the following:
ID, effective_date, Expiration_date.
expiration_date is datetime20. format, and can be NULL
I'm trying to extract rows that expire after Dec 31, 2014 or do not expire (NULL).
Adding the following where statement to the proc sql query gives me no results
where coalesce(datepart(expiration_date),input('31/Dec/2020',date11.))
> input('31/Dec/2014',date11.);
However, when I only select NULL expiration dates and add the following fields:
put(coalesce(datepart(expiration_date),input('31/Dec/2020',date11.)),date11.) as value,
put(input('31/Dec/2014',date11.),date11.) as threshold,
case when coalesce(datepart(expiration_date),input('31/Dec/2020',date11.)) > input('31/Dec/2014',date11.)
then 'pass' else 'fail' end as tag
It shows 'pass' under TAG and all the other fields are correct.
This is an effort to duplicate what I used in SQL Server
where isnull(expiration_date,'9999-12-31') > '2014-12-31'
Using SAS Enterprise Guide 7.1 and while trying to figure it out I've been using
proc sql inobs=100;`
What am I doing wrong ? Thank you.
Some Expiration Dates:
30OCT2015:00:00:00
30OCT2015:00:00:00
29OCT2015:00:00:00
30OCT2015:00:00:00
I would recommend using a date constant ("31DEC2014"d) rather than date functions, or else either use explicit passthrough or disable implicit passthrough. Date functions are challenging when going between databases and so avoiding them when possible is best.