If Then statement - Tableau with multiple conditions - calculated-field

If, Then, ElseIf statement with two fields where one field has two values to be TRUE
The Lead Age and Lead Status where two statuses and both of them should be true. Tried OR, tried AND, tried adding parenthesis.
IF [Lead Age] >3 AND [Lead Status] = "A. Open" THEN "Open"
ELSEIF ([Lead Status] = "B. Attempting to Contact" OR [Lead Status] = "C. Contacted") AND [Lead Age] >30 THEN "Inactive"
ELSEIF ([Lead Status] = "B. Attempting to Contact" OR [Lead Status] = "C. Contacted") AND [Lead Age] <30 THEN "Active"
ELSEIF [Lead Status] = "N. Nurture" OR [Lead Status] ="Disqualified" THEN "Disqualified"
ELSE "Other"
END
Expected Result ->
If Lead Status is Attempting to Contact and also Lead Status is Contacted, add both statuses where Lead Age is also >30 days
I tried the below as well:
IF [Lead Status] = "N. Nurture" OR [Lead Status] ="Disqualified" THEN "Disqualified"
ELSEIF ([Lead Status] = "B. Attempting to Contact" AND [MQL Age] >30) OR ([Lead Status] = "C. Contacted" AND [MQL Age] >30) THEN "Inactive"
ELSEIF ([Lead Status] = "B. Attempting to Contact" AND [MQL Age] <30) OR ([Lead Status] = "C. Contacted" AND [MQL Age] <30) THEN "Active"
ELSEIF ([Lead Status] = "A. Open" AND [MQL Age] >3) THEN "Open"
ELSE "Other"
END

Below condition works for me:
If ([Lead Age] > 30 and
([Lead Status] = 'Attempting to Contact' or [Lead Status] = 'Contacted')) THEN
'Yes'
ELSE
'No'
END

Related

Dcount in unbound textbox

=DCount("*","tblMainDBASE","Year([Date of Payment]) = " & [Text159]) AND ([Type of Application] = 'New Transaction' ")
This is my expression on unbound textbox
What i am expecting is count all record on tblMainDBASE with date of payment is equal to text159 and type of application is equal to New Transaction
Correct the concatenating:
=DCount("*","tblMainDBASE","Year([Date of Payment]) = " & [Text159] & " AND [Type of Application] = 'New Transaction'")
For pluralis as well:
=DCount("*","tblMainDBASE","Year([Date of Payment]) = " & [Text159] & " AND ([Type of Application] = 'New Transaction' Or [Type of Application] = 'New Transactions')")

Teradata SQL Update Table "Query Error 3802 Database 'oo' does not exist"

I need help with the following query code. Currently it is giving me the 3802 query error "Database does not exist". Well I did my "from" selection query as 'oo', so that definitely exists. This worked yesterday with some minor changes to the "from" part of the query. The assistance is appreciated.
UPDATE snd_bqa.open_order_all_test
FROM (
SELECT
CASE WHEN m.order_status_desc = 'Completed' AND NVL(c.supptype, 'NULL') <> 'CAN'
THEN 'YES'
ELSE 'NO'
END AS "Order Complete",
CASE WHEN m.order_status_desc = 'Completed' AND c.supptype = 'CAN'
THEN 'YES'
ELSE 'NO'
END AS "Order Cancelled",
CASE WHEN pih.effective_start_date IS NOT NULL AND pih.product_instance_status_code = 3
THEN 'YES'
ELSE 'NO'
END AS "Service Active in PB",
c.ordernumber,
c1.order_requested_due_date,
c.creationdate AS "Order Creation Date",
c.ordersubmitdate AS "Order Submit Date",
c.lastupdateddate,
c.supptype,
m.order_status_desc,
m.phase_stage,
cus.sub_nasp_id,
cus.gch_id,
b.work_order_no,
b.itemcode,
b.instance_id,
COALESCE(b.specific_date, b.standard_date) AS "Service Req Due Date",
m.milestone,
m.milestone_desc,
cus.custlegalname,
a.account_number,
vle.vle_id,
vle.currency_cd,
cus.duns_number,
a.cle_vle_id,
CURRENT_TIMESTAMP(2) AS "Originally Loaded",
CURRENT_TIMESTAMP(2) AS "Last Upd",
CAST((CASE WHEN m.order_status_desc = 'Completed' AND NVL(c.supptype, 'NULL') <> 'CAN'
THEN c.lastupdateddate
ELSE NULL
END) AS TIMESTAMP(2)) "Order Completed Date",
CAST((CASE WHEN m.order_status_desc = 'Completed' AND c.supptype = 'CAN'
THEN c.lastupdateddate
ELSE NULL
end) AS TIMESTAMP(2)) "Order Cancelled Date",
pih.last_modified AS "Service Last Modified Date",
pih.effective_start_date AS "Service Effective Date"
FROM (
SELECT *
FROM edw_stg_ord_cw_vw.cwpc_basketitem
WHERE itemcode LIKE 'PR%'
AND (instance_id, lastupdateddate) IN (
SELECT instance_id, MAX(lastupdateddate)
FROM edw_stg_ord_cw_vw.cwpc_basketitem
GROUP BY instance_id
)) AS b
INNER JOIN edw_stg_ord_cw_vw.cworderinstance AS c
ON c.basket_id = b.basketid
AND c.isactive = 1
AND c.request_type = 'ORD'
LEFT JOIN (
SELECT c.ordernumber,
MAX(COALESCE(b.specific_date, b.standard_date)) order_requested_due_date
FROM edw_stg_ord_cw_vw.cworderinstance AS c
INNER JOIN edw_stg_ord_cw_vw.cwpc_basketitem AS b
ON b.basketid = c.basket_id
AND c.request_type = 'ORD'
AND c.isactive = 1
GROUP BY c.ordernumber
) AS c1
ON c1.ordernumber = c.ordernumber
LEFT JOIN edw_stg_ord_cw_vw.uno_customer AS cus
ON cus.cworderid = c.cwdocid
AND cus.oi_customer_id = c.ordering_customer_id
LEFT JOIN edw_stg_ord_cw_vw.uno_milestone AS m
ON m.milestone_id = c.milestone_id
LEFT JOIN edw_stg_ord_cw_vw.uno_account AS a
ON a.cworderid = c.cwdocid
AND b.account_id = a.oi_account_id
LEFT JOIN edw_stg_ord_cw_vw.uno_cle_vle AS vle
ON vle.cle_vle_id = a.cle_vle_id
AND c.cwdocid = vle.cworderid
INNER JOIN snd_bqa.open_order_all_test tst
ON tst."Service Instance ID" = b.instance_id
AND tst."Milestone" <> m.milestone
LEFT JOIN edw_pb_stg_vw.product_instance_history AS pih
ON pih.general_5 = tst."Service Instance ID"
WHERE cus.sub_nasp_id NOT IN ('19HNYO', '23MAXA', '19HNYR', '10INTD')
AND COALESCE(b.specific_date, b.standard_date, c1.order_requested_due_date) BETWEEN CURRENT_DATE - 60 AND CURRENT_DATE - 30
) oo
SET "Order Complete" = oo."Order Complete",
"Order Cancelled" = oo."Order Cancelled",
"Service Active in PB" = oo."Service Active in PB",
"Service Order Number" = oo.ordernumber,
"Order Requested Due Date" = oo.order_requested_due_date,
"Order Last Updated" = oo.lastupdateddate,
"Order Creation Date" = oo."Order Creation Date",
"Order Submit Date" = oo."Order Submit Date",
"Order Supp Type" = oo.c.supptype,
"Order Status" = oo.order_status_desc,
"Phase Stage" = oo.phase_stage,
"Milestone" = oo.milestone,
"Milestone Description" = oo.milestone_desc,
"NASP ID" = oo.sub_nasp_id,
"GCH ID" = oo.gch_id,
"Work Order Numbers" = oo.work_order_no,
"Product Code" = oo.itemcode,
"Service Requested Due Date" = oo."Service Req Due Date",
"Customer Name" = oo.custlegalname,
"Account Number" = oo.account_number,
"VLE ID" = oo.vle_id,
"Currency Code" = oo.currency_cd,
"DUNs Number" = oo.duns_number,
"CLE VLE ID" = oo.cle_vle_id,
"Last Record Update" = oo."Last Upd",
"Order Completed Date" = oo."Order Completed Date",
"Order Cancelled Date" = oo."Order Cancelled Date",
"Service Last Modified Date" = oo."Service Last Modified Date",
"Service Effective Date" = oo."Service Effective Date"
WHERE open_order_all_test."Service Instance ID" = oo.instance_id
;
I found the issue. I had oo.c.supptype, when it should have just been oo.supptype
This caused Teradata to think 'oo' was referring to a database. case closed.

Multiple filters in DAX with OR Keyword

I have a requirement where I need to evaluate column from multiple tables and select required records from my Fact table in my SSAS tabular Model.
Eg:
Pick Records from FactA where:
('DimA'[DimA Group] = "Contract" AND'DimB'[DimB Group] = "Contract") OR ('DimA'[DimA Group] = "Commercial", 'DimB'[DimB Group] = "Commercial")
Below is the DAX is have:
MeasureA :=
CALCULATE(SUM('FactA'[Amount Single]),filter(('DimA'[DimA Group] = "Contract", 'DimB'[DimB Group] = "Contract"),
('DimA'[DimA Group] = "Commercial", 'DimB'[DimB Group] = "Commercial"),))
This error I get is Operator Or Expression '()' is not supported in the current context.
Any help is much appreciated
Try this:
MeasureA :=
CALCULATE(
SUM('FactA'[Amount Single]),
FILTER(
CROSSJOIN(VALUES('DimA'[DimA Group]), VALUES('DimB[DimB Group])),
('DimA'[DimA Group] = "Contract" && 'DimB'[DimB Group] = "Contract")
|| ('DimA'[DimA Group] = "Commercial" && 'DimB'[DimB Group] = "Commercial")
)
)
More on this pattern is described here.

Multiple left join in ADO excel

The below SQL query has a syntax error, how can I solve it?
SELECT [CREDITORS$].[Account Code], [CREDITORS$].[Name], Sum([CREDITORS$]![Base Amount]*-1) AS BillAmt," & _
"SUM([SERVICETAX$].[Base Amount]) AS STAMT, Sum(([CREDITORS$]![Base Amount]*-1)-[SERVICETAX$]![Base Amount]) AS TDS " & _
"FROM [CREDITORS$] LEFT JOIN [SERVICETAX$] ON [CREDITORS$].[Transaction Reference]=[SERVICETAX$].[Transaction Reference]" & _
"LEFT JOIN [MASTER$] ON [MASTER$].[Account Code]=[CREDITORS$].[Account Code]" & _
"GROUP BY [CREDITORS$].[Account Code], [CREDITORS$].[Name];
On a dynamic query you need add space in before of LEFT JOIN and GROUP BY, that was missed in the query. So adding the space in end of the line will work.
So the below query will work:
"SELECT [CREDITORS$].[Account Code], [CREDITORS$].[Name], Sum([CREDITORS$]![Base Amount]*-1) AS BillAmt, " & _
"SUM([SERVICETAX$].[Base Amount]) AS STAMT, Sum(([CREDITORS$]![Base Amount]*-1)-[SERVICETAX$]![Base Amount]) AS TDS " & _
"FROM [CREDITORS$] LEFT JOIN [SERVICETAX$] ON [CREDITORS$].[Transaction Reference]=[SERVICETAX$].[Transaction Reference] " & _
"LEFT JOIN [MASTER$] ON [MASTER$].[Account Code]=[CREDITORS$].[Account Code] " & _
"GROUP BY [CREDITORS$].[Account Code], [CREDITORS$].[Name];"
You actual query will return as the below, it won't have proper spacing.
SELECT [CREDITORS$].[Account Code], [CREDITORS$].[Name], Sum([CREDITORS$]![Base Amount]*-1) AS BillAmt, SUM([SERVICETAX$].[Base Amount]) AS STAMT, Sum(([CREDITORS$]![Base Amount]*-1)-[SERVICETAX$]![Base Amount]) AS TDS FROM [CREDITORS$] LEFT JOIN [SERVICETAX$] ON [CREDITORS$].[Transaction Reference]=[SERVICETAX$].[Transaction Reference]LEFT JOIN [MASTER$] ON [MASTER$].[Account Code]=[CREDITORS$].[Account Code]GROUP BY [CREDITORS$].[Account Code], [CREDITORS$].[Name];

How do I get dataset from the query?

I have the following SQL query in MS Access / VB.net:
Dim sql = "SELECT [Product Name], [Customer Name], [Group Associated], [Customer Address], [Product Price], [Current Stock], [Date Ordered], [Volume Purchased] FROM (torder INNER JOIN tproducts ON tproducts.Product_ID = torder.Product_ID) INNER JOIN tcustomer ON torder.Customer_ID = tcustomer.Customer_ID WHERE torder.[Date Ordered] Between #" & StartOfTimePeriod & "# And #" & Today & "#;"
conn.Open()
Dim dataAdapter As New OleDbDataAdapter(sql, conn)
Dim ds As New DataSet()
dataAdapter.Fill(ds, "torder")
conn.Close()
DataGridView.DataSource = ds
DataGridView.DataMember = "torder"
When the query is run it doesn't display the full dataset that it should. Instead, the results are almost random. What could be wrong?
http://prntscr.com/5eh4ge
http://prntscr.com/5eh4ai
Do not stringize your date and time values. Use a parameterized query.
Dim sql = "SELECT [Product Name], [Customer Name], [Group Associated], " & _
"[Customer Address], [Product Price], [Current Stock], [Date Ordered], " & _
"[Volume Purchased] " & _
"FROM (torder INNER JOIN tproducts ON tproducts.Product_ID = torder.Product_ID) " & _
"INNER JOIN tcustomer ON torder.Customer_ID = tcustomer.Customer_ID " & _
"WHERE torder.[Date Ordered] >= ? And torder.[Date Ordered] <= ?"
Dim dataAdapter As New OleDbDataAdapter(sql, conn)
Dim p1 = new OleDbParameter() With
{
.ParameterName = "#p1",
.OleDbType = OleDbType.Date,
.Value = StartOfTimePeriod
}
Dim p1 = new OleDbParameter() With
{
.ParameterName = "#p2",
.OleDbType = OleDbType.Date,
.Value = DateTime.Today.AddMinutes(1339)
}
dataAdapter.SelectCommand.Parameters.Add(p1)
dataAdapter.SelectCommand.Parameters.Add(p2)
....
In this way the correct interpretation of your DateTime values is a job passed to the database engine together with actual DateTime values (I assume that StartOfTimePeriod is a DateTime variable).
Instead, if you convert these values to string, the conversion follow the rules of your locale environment that could produce a string not compatible with the expected format of your database engine (for example the inversion of position of months and days values)