Friends
Today I would like to join the result of 2 evaluate Query in DAX Query can any one have the way to do it? Following are the codes of 2 evaluate queries which i would like to join.
evaluate
(
filter
(
summarize
(
'Total Days',
'Total Days'[Month],
'Total Days'[Number]
), AND('Total Days'[Month]>=201601 , 'Total Days'[Month]<201612)
)
)
EVALUATE
filter
(
SUMMARIZE (
'Sales Data',
'Date'[DateSID],
'Date'[Month],
'Office'[Number]
)
, AND('Date'[Month]>=201601 , 'Date'[Month]<201612)
)
I want to perform an inner join based on(Month, Number) in above 2 DAX queries.
I need following table as an output.
'Total Days'[Month] 'Total Days'[Number] 'Date'[DateSID]
Related
I have a generic Sales table that I'm querying against and I want to return two aggregates of the table, but on two separate rows instead of one.
I have a query to get a value like this:
SELECT
SUM(o.SalesAmount) Value
,'Yearly Sales' Name
,'Yearly' Timeframe
FROM Orders o
Which returns a single row
Value
Name
Timeframe
1,000,000
Yearly Sales
Yearly
Is there a way for me to structure my query so I can return another aggregate on a second row, but with a different Name and Timeframe on the same query? Right now I'm duplicating the first query and a bunch of filters that I need the same in the second query, but changing the aggregate and the "Name" and then unioning them all together like this:
SELECT
SUM(o.SalesAmount) Value
,'Yearly Sales' Name
,'Yearly' Timeframe
FROM Orders o
WHERE
/* bunch of filters */
UNION ALL
SELECT
COUNT(o.Orders) Value
, 'Yearly Orders' Name
,'Yearly' Timeframe
FROM Orders o
WHERE
/* bunch of filters which are the same as the first */
I have about 10 other aggregations that I need to return in a similar manner and I don't think unioning 12 really similar queries is the way to go about it.
Ideally I'd want something like this (I understand this won't give me what I need) so I can use the same filters and not have to duplicate code.
SELECT
SUM(o.SalesAmount) Value
, COUNT(o.Orders) Value2
,'Yearly Sales' Name
, 'Yearly Orders' Name2
,'Yearly' Timeframe
FROM Orders o
WHERE
/* bunch of filters that I only have to manage in one spot */
That would then return this:
Value
Name
Timeframe
1,000,000
Yearly Sales
Yearly
100,000
Yearly Orders
Yearly
You can unpivot it from a cte
with cte as (
SELECT
SUM(o.SalesAmount) Value1,
COUNT(o.Orders) Value2,
'Yearly' Timeframe
FROM Orders o
WHERE
/* bunch of filters */
)
SELECT Value1 Value,'Yearly Sales' Name, Timeframe
FROM cte
UNION ALL
SELECT Value2, 'Yearly Orders', Timeframe
FROM cte
Use cross apply to split each Orders row into two:
select sum(split.Value) Value
,split.Name
,'Yearly' Timeframe
from Orders o
cross apply (values
('Yearly Sales' ,o.SalesAmount)
,('Yearly Orders',1)
) split(Name,Value)
group by split.Name
You can unpivot it after aggregating in a derived table:
SELECT
v.Value
v.Name
t.Timeframe
FROM (
SELECT
SUM(o.SalesAmount) Sum,
COUNT(o.Orders) Count,
'Yearly' Timeframe
FROM Orders o
) o
CROSS APPLY (VALUES
('Yearly Sales', Sum),
('Yearly Orders', Count)
) v(Name, Value);
I am new to DAX.
Let's pretend I have a table that looks like this:
Table A:
status delivered sold
late 10 50
late 20 300
early 5 500
Let's pretend I am using this SQL query:
with cte_1 as (
select
status, count(*) as [row_count]
from [table a]
group by [status]
having count(*) > 1
)
select *
from [table a] as p1
inner join [cte_1] as p2
on p1.[status] = p2.[status]
What would be the dax equivalent of this?
The SQL query return the Table A rows with the status that occurrs at least twice in the table, adding the count of the number of rows with the same status. In Power BI we can write a calculated table that adds the count of the rows of the same status and then filter out those with a count less than 2
Result =
FILTER(
ADDCOLUMNS(
'Table A',
"row_count",
CALCULATE(
COUNTROWS( 'Table A' ),
ALLEXCEPT( 'Table A', 'Table A'[Status] )
)
),
[row_count] > 1
)
I am trying to run a query to gather the total items on hand in our database. However it seems i'm getting incorrect data. I am selecting selecting just the amount field and summing it using joins from separate tables based on certain parameters, however if I display additional fields such as order number, and date all of a sudden im getting different data, even though those fields are being used as filters in the query. Is it because its not in the select statement? If it needs to be in the select statement is it possible to not display them?
Here are the two queries.
-- Items On Hand
select CONVERT(decimal(25, 2), SUM(tw.amount)) as 'Amt'
from [Sales Header] sh
join
(
select *
from TWAllOrders
where [Status] like 'Released'
) tw
on tw.[Order Nb] = sh.No_
join
(
select *
from OnHand
) oh
on tw.No_ = oh.[Item No_]
where sh.[Requested Delivery Date] < getdate()
HAVING SUM(tw.Quantity) <= SUM(oh.Qty)
providing a sum of 21667457.20
and with the added columns
-- Items On Hand
select CONVERT(decimal(25, 2), SUM(tw.amount)) as 'Amt', [Requested Delivery Date], sh.No_, tw.[Status]
from [Sales Header] sh
join
(
select *
from TWAllOrders
where [Status] like 'Released'
) tw
on tw.[Order Nb] = sh.No_
join
(
select *
from OnHand
) oh
on tw.No_ = oh.[Item No_]
where sh.[Requested Delivery Date] < getdate()
group by sh.[Requested Delivery Date], sh.No_, tw.[Status]
HAVING SUM(tw.Quantity) <= SUM(oh.Qty)
order by sh.[Requested Delivery Date] ASC
Providing a sum of 12319998
I'm self taught in SQL so I may be misunderstanding something obvious, thanks for the help.
With no sample data, I am going to have to demonstrate this in principle. In the latter query you have a GROUP BY meaning the scope of the values in the HAVING will differ, and thus the filtering from said HAVING will be different.
Let's take the following sample data:
CREATE TABLE dbo.MyTable (Grp char(1),
Quantity int,
Required int);
INSERT INTO dbo.MyTable (Grp, Quantity, [Required])
VALUES('a',2,7),
('a',14,2),
('b',4, 7),
('b',3,4),
('c',17,5);
Now we'll perform an overly simplified version of your query:
SELECT SUM(Quantity)
FROM dbo.MyTable
HAVING SUM(Quantity) > SUM(Required);
This brings back the value 40; which is the SUM of all the values in Quantity. A value is returned because the total SUM of Required is 25.
Now let's add a GROUP BY like your second query:
SELECT SUM(Quantity)
FROM dbo.MyTable
GROUP BY Grp
HAVING SUM(Quantity) > SUM(Required);
Now we have 2 rows, with the values 16 and 17 giving a total value of 33. That's because the rows where Grp have a value of 'B' are filtered out, as the SUM of Quantity is lower that Required for 'B'.
The same is happening in your data; in the grouped data you have groups where the HAVING condition isn't met, so those rows aren't returned.
I have a table deposit which have column Refund_amt ,Deposit_amt having different Rows with same GR_no . here my question is ,I want to subtract deposit_amt column from Refund_amt
I tried various alternative in query but didn't succeed
My query :
SELECT d.Gr_no
, d.Rec_No
, d.Deposite_Amt
, d.penalty_Amt
, d.Refund_Amt - Refund
, s.Name
, s.cur_std
, cur_div
From
( select d.Refund_Amt refund
from deposite d
, std_gr s
where d.Gr_no = s.Gr_no )
Result would look like this in final total column :
Thank you
You are looking for an aggregation per std_gr: the sum of the deposites minus the sum of the refunds. One way is to do this aggregation in a subquery and join this subquery to your table.
select
d.*, sums.final_total
from deposite d
join
(
select std_gr, nz(sum(deposite_amt),0) - nz(sum(refund_amt),0) as final_total
from deposite
group by std_gr
) as sums on sums.std_gr = d.std_gr
order by d.rec_no;
I have the following two queries that build/feed into the third query. My goal is to have a crosstab query of [MCOs] down the left and possible responses/values for [DrpDown] across the top with the values shown as percentages of the total for each [MCO] (so % of row total).
What I have works, but I want to know if I can do it all in one query.
SELECT tblMCOs.MCOs, tblMCOs.DrpDwn, Count(tblMCOs.ID) AS CountOfID
FROM tblMCOs
GROUP BY tblMCOs.MCOs, tblMCOs.DrpDwn;
SELECT tblMCOs.MCOs, Count(tblMCOs.DrpDwn) AS CountOfDrpDwn
FROM tblMCOs
GROUP BY tblMCOs.MCOs;
TRANSFORM Sum(Round([qryMCODrpDwnCt]![CountOfID]/[qryMCOCtDrpDwn]!
[CountOfDrpDwn],4)*100) AS PCT
SELECT qryMCODrpDwnCt.MCOs
FROM qryMCODrpDwnCt INNER JOIN qryMCOCtDrpDwn ON qryMCODrpDwnCt.MCOs =
qryMCOCtDrpDwn.MCOs
GROUP BY qryMCODrpDwnCt.MCOs
PIVOT qryMCODrpDwnCt.DrpDwn;
Thanks in advance for your help.
What I have works, but I want to know if I can do it all in one query.
Crosstab queries can be a bit fussy, but simply inserting the SQL code as subqueries should work:
TRANSFORM Sum(Round([sqMCODrpDwnCt]![CountOfID]/[sqMCOCtDrpDwn]![CountOfDrpDwn],4)*100) AS PCT
SELECT sqMCODrpDwnCt.MCOs
FROM
(
SELECT tblMCOs.MCOs, tblMCOs.DrpDwn, Count(tblMCOs.ID) AS CountOfID
FROM tblMCOs
GROUP BY tblMCOs.MCOs, tblMCOs.DrpDwn
) AS sqMCODrpDwnCt
INNER JOIN
(
SELECT tblMCOs.MCOs, Count(tblMCOs.DrpDwn) AS CountOfDrpDwn
FROM tblMCOs
GROUP BY tblMCOs.MCOs
) AS sqMCOCtDrpDwn
ON sqMCODrpDwnCt.MCOs = sqMCOCtDrpDwn.MCOs
GROUP BY sqMCODrpDwnCt.MCOs
PIVOT sqMCODrpDwnCt.DrpDwn