Mdx error creating a calculated script member - mdx

I have an mdx script which I took from the browser but edited it a little and created a script in calculations but it's wrong?
WITH MEMBER [Measures].[RTY]
AS
'SELECT NON EMPTY { [Measures].[Person ID Count] } ON COLUMNS
FROM ( SELECT ( { [Order Date Time Base].[Month Number Of Year].&[1], [Order Date Time Base].[Month Number Of Year].&[2], [Order Date Time Base].[Month Number Of Year].&[3] } ) ON COLUMNS
FROM ( SELECT ( { [Order Date Time Base].[Calendar Year].&[2015] } ) ON COLUMNS
FROM [Scv Cube])) WHERE ( [Order Date Time Base].[Calendar Year].&[2015], [Order Date Time Base].[Month Number Of Year].CurrentMember)'
SELECT
{[Measures].[Person ID Count]} ON COLUMNS
FROM [Scv Cube]
Whats wrong with this? I am very new to mdx

SELECT is not allowed in the WITH clause
Looks like you have a sub select - this goes in the FROM clause. I have commented out the currentmember near the end of the script as I am unsure of what happens if this is included in a WHERE clause
SELECT
{[Measures].[Person ID Count]} ON COLUMNS
FROM
(
SELECT
NON EMPTY { [Measures].[Person ID Count] } ON COLUMNS
FROM
(
SELECT
(
{
[Order Date Time Base].[Month Number Of Year].&[1]
, [Order Date Time Base].[Month Number Of Year].&[2]
, [Order Date Time Base].[Month Number Of Year].&[3]
}
) ON COLUMNS
FROM
(
SELECT
( { [Order Date Time Base].[Calendar Year].&[2015] } ) ON COLUMNS
FROM [Scv Cube]
)
)
WHERE
(
[Order Date Time Base].[Calendar Year].&[2015]
//, [Order Date Time Base].[Month Number Of Year].CurrentMember
)
);
What is the final goal of this script? Do you just want the total for [Person ID Count] in the first three months of 2015? If so this should help...
SELECT
NON EMPTY { [Measures].[Person ID Count] } ON COLUMNS
FROM [Scv Cube]
WHERE
(
[Order Date Time Base].[Calendar Year].&[2015],
{
[Order Date Time Base].[Month Number Of Year].&[1]
, [Order Date Time Base].[Month Number Of Year].&[2]
, [Order Date Time Base].[Month Number Of Year].&[3]
}
);

Related

Convert SQL query to MDX - have Group by & Count Functions

I have the following SQL query which I am trying to convert into MDX:
select avg(skucount)
from
(
SELECT count(distinct [SKUCode]) as skucount
--,[SHOPCODE_WITHOUT_DIST]
FROM [HFPL_DW].[dbo].[FactSecondarySales]
where DISTCODE in
(
SELECT [DISTRIBUTORCODE]
FROM [HFPL_DW].[dbo].[DimDistHierarchy]
where REGION = 'KARACHI'
)
and month(saledate) = 7 and year(saledate) = 2018
group by [SHOPCODE_WITHOUT_DIST]
) as inner_query
The inner query returns the count of SKU saled on each shop(which is fulfilled by using "Group by ShopCode")
First I am trying to convert the inner query to MDX, I have tried the following:
WITH MEMBER [Measures].[SKU Count] AS
COUNT( NonEmpty( { [Product Hierarchy].[SKU].[SKU].Members }, ( [Shop Hierarchy].[SHOPCODE WITHOUT DIST] ) ) )
SELECT
{
[Measures].[SKU Count]
} ON COLUMNS,
NonEmpty(
{ [Product Hierarchy].[SKU].[SKU].Members },
([Shop Hierarchy].[SHOPCODE WITHOUT DIST] )
) ON ROWS
FROM
[Consolidated Sales]
where(
[Time Analysis].[Month].&[2018-07-01T00:00:00],
[Distribution Hierarchy].[DISTRIBUTORCODE].&[1002]
)
Reference: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/51988607-78cc-4520-88db-c6d3e99dd1fc/mdx-to-count-the-number-of-members-in-a-dimension-based-on-another-dimension?forum=sqlanalysisservices
It is not returning anything.
Kindly help me acheive the desired output of the Average SKUs saled(The outer query), the number of SKUs saled per shop (inner query)

DAX Query on (Day Over Day)

I have the Sales table and a Date table
I wrote the below query to calculate the DOD Sales
Sales Volume := SUM([Sales])
Sales Volume (Prev) := CALCULATE([Sales Volume], PREVIOUSDAY('Date'[Date])
Sales Volume (DOD) = DIVIDE([Sales Volume]-[Sales Volume (Prev)],[Sales Volume (Prev)])
However, these query above will calculate DOD based on continuous day of a month. My concern is I would like to calculate only those Order Date. For example, I would like to compare Sales on 4/12/2016 and 1/12/2016. ((50-20)/20).
How should i amend the query to achieve that?
You have to create a calculated column to get the previous date:
Previous Date =
CALCULATE (
MAX ( [Order Date] ),
FILTER ( ALL ( 'Table' ), [Order Date] < EARLIER ( 'Table'[Order Date] ) )
)
Then just create the measures with the following expressions:
Sales Volume := SUM('Table'[Sales])
Sales Volume Prev :=
CALCULATE (
SUM ( [Sales] ),
FILTER ( ALL ( 'Table' ), 'Table'[Order Date] = MAX ( [Previous Date] ) )
)
Sales Volume (DOD) :=
DIVIDE ( [Sales Volume] - [Sales Volume Prev], [Sales Volume Prev] )
Let me know if this helps.

DAX Cumulative Total Comparison from Different Tables

How do I create a DAX measure that will show me the First Receipt Date where the Receipt Balance is greater than or equal to the Payment Balance?
For example, for Jun, I want the [Payment Receipt Date Avg] measure to show as Jan because the Receipt Balance is 10 which is greater than the Payment Balance of 8.
For Aug, it would show as Feb because that is the first date where the Receipt Balance is at least the Payment Balance of 13.
Desired Result:
Data Model
Table: Po Receipt
Table: Payment
Attempted Solution
I already have the below measures defined:
Receipt Balance:
=CALCULATE
(
SUM(PoReceipt[Quantity Received]),
FILTER
(
ALL ( PaymentDates ),
PaymentDates[Payment_FullDate] <= MAX ( PaymentDates[Payment_FullDate] )
)
)
Payment Balance:
=CALCULATE
(
SUM(Payment[Payment Amount]),
FILTER
(
ALL ( PaymentDates ),
PaymentDates[Payment_FullDate] <= MAX ( PaymentDates[Payment_FullDate] )
)
)
Create calculated columns to get the first date which has a net receipt running total (i.e. receipts less payments running total) greater than the payment amount in the current row context. The calculated columns will need to do the following:
Return the running total of [Payment Amount] for each [PO Num]. The EARLIER function is used so that for each row in the outer context, you sum all rows in the inner context where the inner row has a [Payment Date] less than or equal to the outer row, and the inner row's [Po Num] is equal to that of the outer row.
Return a summary table based on the PoReceipt table, which has an extended column "Receipt Balance Calc". The extended column calculates the running total of the PoReceipt[Quantity Received] similar to step 1. The summary table is grouped by the columns [Po Num] and [Receipt Date] to avoid performing the same calculation more than once (since there are multiple rows with the same [Po Num] due to the [Receipt Num] column).
Filter the result to only include rows where the Receipt Balance is greater than or equal to the Payment Balance.
See DAX code for calculated columns:
Payment[Payment Balance Calc]
=CALCULATE (
SUM ( Payment[Payment Amount] ),
FILTER (
Payment,
Payment[Payment Date] <= EARLIER ( Payment[Payment Date] )
&& Payment[Po Num] = EARLIER ( Payment[Po Num] )
)
)
Payment[Payment Receipt Date]
=CALCULATE (
MIN ( PoReceipt[Receipt Date] ),
ALL ( PaymentDates ),
FILTER (
ADDCOLUMNS (
SUMMARIZE (
PoReceipt,
PoReceipt[Po Num],
PoReceipt[Receipt Date]
),
"Receipt Balance Calc",
CALCULATE (
SUM ( PoReceipt[Quantity Received] ),
FILTER (
PoReceipt,
PoReceipt[Receipt Date] <= EARLIER ( PoReceipt[Receipt Date] )
&& PoReceipt[Po Num] = EARLIER ( PoReceipt[Po Num] )
)
)
),
[Receipt Balance Calc] >= EARLIER ( Payment[Payment Balance Calc] )
)
)
Finally, create the below measure to calculate the weighted average of the receipt dates:
Payment Receipt Date Avg:=
SUMX (
Payment,
Payment[Payment Amount] * Payment[Payment Receipt Date]
/ SUM ( Payment[Payment Amount] )
)

Get data from two related fact tables based on common date dimension in MDX

We have two fact tables Fact_Order and Fact_Product.
In fact_Order we have columns OrderId,OrderDate and OrderQuantity.
In fact_Product we have columns ProductReleaseDate and ProductCost
Now we want to join the Releasedate with all the orders that fall within a week in MDX.
Help would be highly appreciated because i am stuck on this from last two days
This is the MDX that i am trying to write for the same.
WITH MEMBER [Week1 order] AS
FILTER
(
Measures.OrderQuantity,
[Release Date].[Date].currentmember : [Release Date].[Date].currentmember.LEAD(6)
)
SELECT [Week1 order] ON 0,
NON EMPTY
{
[Release Date].[Date].[Date],
FILTER
(
[DimOrder].[OrderID].[OrderID],
[Release Date].[Date].currentmember : [Release Date].[Date].currentmember.LEAD(6)
)
,FILTER
(
[Order Date].[Date].[Date],
[Release Date].[Date].currentmember : [Release Date].[Date].currentmember.LEAD(6)
)
} ON 1
FROM
[ProductReleaseCube]
Does this help?
WITH SET ValidCombOfDates AS
FILTER
(
NonEmpty
(
{
[Release Date].[Date].[Date].MEMBERS * [Order Date].[Date].[Date].MEMBERS
}, Measures.OrderQuantity
),
[Order Date].[Date].CURRENTMEMBER.MEMBER_VALUE - [Release Date].[Date].CURRENTMEMBER.MEMBER_VALUE <=7
)
MEMBER Measures.ExpectedOrderQuantity AS
IIF
(
ISEMPTY(Measures.OrderQuantity),
0,
Measures.OrderQuantity
)
SELECT
ValidCombOfDates * [DimOrder].[OrderID].[OrderID].MEMBERS
ON 1,
Measures.ExpectedOrderQuantity
ON 0
FROM
[ProductReleaseCube]

Count an intersection where two dates match

I have several Role Playing dimensions that are FK's to the Date Dimension. How would I best count the intersection where a date from from Date Dimension matches the Date in a Role Playing Dimension?
In SQL it would be something like:
Select DimDate,
COUNT(1)
From DimDateTable D
join RolePlayingTable R on D.Date = R.Date
Group by DimDate
I thought this would work:
With
Member [Measures].[First Date In] as
(
[Measures].[Account Count],
[Sprocket Date].[Date].CurrentMember
)
Member [Measures].[First Date Out] as
(
[Measures].[Account Count],
[Widget Date].[Date].CurrentMember
)
Select
{
[Measures].[First Date In],
[Measures].[First Date Out]
} on 0,
Non Empty
[Dim Calendar].[Date].[Date] on 1
From [Cube-Bert]
But it only counts the intersecting points and not where the Sproket.Date = Calendar.Date
I also tried this, but it's not right either:
with
Member [Measures].[Count] as
Sum(
Exists(
{[Sproket Date].[Date].[Date]},
{[Dim Calendar].[Date].[Date]}
)
,[Measures].[Account Count]
)
select [Measures].[Count] on 0,
Non Empty
[Dim Calendar].[Date].members on 1
from [Cube-Bert]
I put the date matching logic into the DSV and produced a flag column in the fact table, 1 = true, 0 = false.