Multiple filters in DAX with OR Keyword - powerpivot

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.

Related

query is returning " single-row subquery returns more than one row"

I am not sure why the below query is giving the " single-row subquery returns more than one row" please let me know if I am missing anything.
CASE
WHEN s.servprov_gid = 'IFFCO.CAR-60041'
THEN
(SELECT E.EQUIPMENT_NUMBER
FROM S_EQUIPMENT SE,
EQUIPMENT E,
SHIPMENT_S_EQUIPMENT_JOIN SSEJ,
SHIPMENT S
WHERE SSEJ.SHIPMENT_GID = 'IFFCO/LOGISTICS.L171203007'
AND SE.S_EQUIPMENT_GID = SSEJ.S_EQUIPMENT_GID
AND E.EQUIPMENT_GID = SE.EQUIPMENT_GID
AND SSEJ.SHIPMENT_GID = S.SHIPMENT_GID
)
ELSE
(SELECT MIN(se.equipment_number)
FROM shipment_s_equipment_join ssej,
s_equipment se
WHERE ssej.shipment_gid = 'IFFCO/LOGISTICS.L171203007'
AND ssej.s_equipment_gid = se.s_equipment_gid
AND se.equipment_number IS NOT NULL
)
END
The error message advise very clearly, more than one row are returned from the sub-query after the key word "Then". You can add "Top 1" after the first SELECT keyword for SQL Serve4.
Or get the sub query end with "and rownum =1" for Oracle. The following is for SQL Server.
CASE
WHEN s.servprov_gid = 'IFFCO.CAR-60041'
THEN
(SELECT TOP 1 E.EQUIPMENT_NUMBER
FROM S_EQUIPMENT SE,
EQUIPMENT E,
SHIPMENT_S_EQUIPMENT_JOIN SSEJ,
SHIPMENT S
WHERE SSEJ.SHIPMENT_GID = 'IFFCO/LOGISTICS.L171203007'
AND SE.S_EQUIPMENT_GID = SSEJ.S_EQUIPMENT_GID
AND E.EQUIPMENT_GID = SE.EQUIPMENT_GID
AND SSEJ.SHIPMENT_GID = S.SHIPMENT_GID
)
ELSE
(SELECT MIN(se.equipment_number)
FROM shipment_s_equipment_join ssej,
s_equipment se
WHERE ssej.shipment_gid = 'IFFCO/LOGISTICS.L171203007'
AND ssej.s_equipment_gid = se.s_equipment_gid
AND se.equipment_number IS NOT NULL
)
END

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.

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)

Nested Linq Queries Saga

So I am trying to write something like this:
SELECT s.CompanyID,
s.ShareDate,
s.OutstandingShares,
s.ControlBlock
FROM (
SELECT MAX(ShareDate) AS Sharedate,
CompanyID
FROM ShareInfo
WHERE (ShareDate <= #filter_date)
GROUP BY CompanyID
) AS si
INNER JOIN
tblShareInfo AS s ON s.ShareDate = si.Sharedate AND s.CompanyID = si.CompanyID
Essentially this is trying to return the most recent Share Information, we keep a running history. Now I am trying to write something similar to this in LINQ.
Here was my closest attempt:
From a _
In db_context.ShareInfos _
Where a.ShareDate <= filter_date _
Group a By a.CompanyID Into Group _
Select CompanyID, MostRecentShareDate = Group.Max(Function(a) a.ShareDate) _
Join b In db_context.ShareInfos On b.CompanyID Equals a.CompanyID _
Select b.CompanyID, b.ShareDate, b.OS, b.CB()
Unfortunately this does not compile. Obviously I'm not understanding the LINQ syntax somehow. Can anyone steer me in the right direction?
Thanks.
with your last select statement you should use
select new {
CompanyID = b.CompanyID,
ShareDate = b.ShareDate,
OS = b.OS,
CB = b.CB
};
that's a start...
Okay so looks like this needs to be done using two statements:
Dim MostRecentShareDates = _
From s2 In query_collection.DBContext.ShareInfos _
Where s2.ShareDate <= filter_date _
Group s2 By s2.CompanyID Into Group _
Select New With { _
.CompanyID = CompanyID, _
.MostRecentShareDate = Group.Max(Function(s3) s3.ShareDate) _
}
Return From s In query_collection.DBContext.ShareInfos _
Join s1 In MostRecentShareDates On s.CompanyID Equals s1.CompanyID And s.ShareDate Equals s1.MostRecentShareDate _
Select New With { _
.CompanyID = s.CompanyID, _
.ShareDate = s.ShareDate, _
.OS = s.OS, _
.CB = s.CB _
}
I tried using the 'Let' keyword to embed the first statement into the second, but that would not compile either. Now the nice thing about this is the Linq has delayed execution, so until you traverse the collection returned by the second statement, no SQL gets generated. Linq is then smart enough to combine the two code fragments into one SQL statement, essentially exactly the same statement as I wrote in my original SQL above.

Linq syntax in VB.NET

What I really want is to select these two tables in to an anon type like in Scott Gu's blog: here However, I would settle for this created type "ActiveLots" I am joining two tables together and want to be able to reference columns from each in my result set.
I don't seem to be getting the syntax correctly.
Dim pi = From p In dc.Inventories Join i In dc.InventoryItems On p.InventoryItemID _
Equals i.InventoryItemID Where p.LotNumber <> "" _
Select New ActiveLots LotNumber = p.LotNumber, Quantity = p.Quantity, Item = i.Item, Uom = i.UnitofMeasure, Description = i.Description
Have a look at Daniel Moth's blog entry. I suspect you want:
Dim pi = From p In dc.Inventories _
Join i In dc.InventoryItems
On p.InventoryItemID Equals i.InventoryItemID _
Where p.LotNumber <> "" _
Select New With { .LotNumber = p.LotNumber, .Quantity = p.Quantity, _
.Item = i.Item, .Uom = i.UnitofMeasure, _
.Description = i.Description }
That's using an anonymous type - to use a concrete type, you'd use New ActiveLots With { ... (where ActiveLots has to have a parameterless constructor).