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.
Related
I am trying to fetch the sums of tot_ack on MDX, so it would be shown on erit dimension and acc (account dimesion). However I can not figure out how to model the between dates. I am new to MDX.
select SUM(tot_ack) from
DBO.DIM_OBJ_ACC AS ACC INNER JOIN
DBO.FAKTA_SALDO AS saldo ON ACC.ACC_ID = saldo.ACC_ID INNER JOIN
DBO.DATE_OBJ_KP AS KP ON saldo.KP_ID = KP.KP_ID INNER JOIN
DBO.PERIOD AS PERIOD ON saldo.PERIOD = PERIOD.PERIOD
where
ACC.ACC _ID = '3001' and ACC.erit_ID = '1'
(DATEADD(day, 0,DATEADD(month, 11,DATEADD(year,2011-1900, 0))) BETWEEN KP.KP_DATE_START AND KP.KP_DATE_END) AND
(PERIOD.MONTH= 12 OR PERIOD .MONTH=0) AND (PERIOD.YEAR =2011)
My MDX-code
SELECT {[DIM OBJ ERIT].[ERIT ID].MEMBERS} ON COLUMNS,
{[DATE OBJ ACC].[ACC ID].members} ON ROWS
FROM [REPORT]
WHERE (Measures.[tot ack],
[PERIOD].[year].[2011],
[PERIOD].[month].[12],
[DATE OBJ KP].[KP DATE START].[Year].[2011],
[DATE OBJ KP].[KP DATUM END].[Year].[2011]
)
In mdx you specify range with ":". So your query should be
SELECT {[DIM OBJ ERIT].[ERIT ID].MEMBERS} ON COLUMNS,
{[DATE OBJ ACC].[ACC ID].members} ON ROWS
FROM [REPORT]
WHERE (Measures.[tot ack],
[PERIOD].[year].[2011],
[PERIOD].[month].[12],
[DATE OBJ KP].[KP DATE START].[Year].[2011]:[DATE OBJ KP].[KP DATUM END].[Year].[2011]
)
Take a look at the example below
select
[Measures].[Internet Sales Amount]
on columns,
[Product].[Category].[Category]
on rows
from [Adventure Works]
Result
Now lets filter it for 2013 -01-01 to 2013-01-10
select
[Measures].[Internet Sales Amount]
on columns,
[Product].[Category].[Category]
on rows
from [Adventure Works]
where
[Date].[Date].&[20130101]:[Date].[Date].&[20130110]
Result
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)
i have been doing a query to get previous year value in MDX but can't the the desired output. In sql i would do it like this. Question is how do i apply this query in MDX?
select data1, sum(data2) as sumthisyear, sumlastyear
from table1 a
left outer join
( select data1, sum(data2) 'sumlastyear'
from table1
where tableyear = 2017 group by data1
) b on a.data1 = b.data1
where tableyear= '2018' and datafilter = 'SampleFilter'
group by data1, sumlastyear
SSAS Cube Structure Image
I have done some research and did mdx function like parallelperiod but not giving the correct values as data are filtered based on 2018 year.
Here's my MDX query
CREATE MEMBER CURRENTCUBE.Measures.[SPLY Registered Sales] AS
iif( [DimProdDate].[Dates].CurrentMember Is
[DimProdDate].[Dates].[All].LastChild,
( PARALLELPERIOD( [DimProdDate].[Dates].[Calendar Year],
1,
Tail( NonEmpty( [DimProdDate].[Dates].[Full Date].MEMBERS,
[MEASURES].[Registered Sales]),
1
).Item(0)
),
[MEASURES].[Registered Sales] ),
( PARALLELPERIOD( [DimProdDate].[Dates].[Calendar Year],
1,
[DimProdDate].[Dates].CurrentMember
),
[MEASURES].[Registered Sales] )
);
Any help will be very much appreciated.
Thanks!
My SSAS cube has the following fact and dimensions with the columns as shown below
FactActivity
DateKey, UserKey, ActivityKey, ActivityCount
DimDate
DateKey, Date, Week, Year
DimUser
UserKey, UserName, Gender
DimActivity
ActivityKey, ActivityName
I have created the distinct count measures of users and dates as follows
[Distinct Users]
COUNT(NONEMPTY([DimUser].[UserKey].[UserKey].Members, [Measures].[ActivityCount])
[Distinct Dates]
COUNT(NONEMPTY([DimDate].[DateKey].[DateKey].Members, [Measures].[ActivityCount])
Both these measures are working correctly as expected when I slice/pivot by ActivityName.
Now I wanted to calculate average days per user, so I created the metric as follows
[Avg Days Per User]
AVG([DimUser].[UserKey].[UserKey].Members, [Measures].[Distinct Dates])
But this is giving me wrong results.! i also tried
DIVIDE([Measures].[Distinct Days], [Measures].[Distinct Users])
Still I get wrong results...what i'm doing wrong?
Maybe just adding in EXISTING will help?
AVG(
EXISTING [DimUser].[UserKey].[UserKey].Members,
[Measures].[Distinct Dates]
)
Although trying to recreate something similar in AdvWks I seem top get a valid return without EXISTING:
WITH
MEMBER [Measures].[Avg Count Per Reseller] AS
Avg
( [Reseller].[Reseller].[Reseller].MEMBERS
,[Measures].[Reseller Order Count]
), format_string = "0.0000"
SELECT
{[Measures].[Reseller Order Count],[Measures].[Avg Count Per Reseller]} ON 0
,{[Promotion].[Promotion].[All Promotions].Children} ON 1
FROM [Adventure Works]
WHERE [Date].[Calendar Year].&[2005];
Is there a way for me to view the first and/or last leaf value in a level of a hierarchy?
I am trying to create a calculation dimension in SSAS which will include, for example, a year to date calculation which I would prefer not to display for dates in the future.
I've worked out how to make that happen at the lowest level (date), but am getting errors ath te aggregation levels when trying to implement the technique.
To help me accomplish what I want I've included a [Date In Past] member in my dimension, which contains a 0 if the date is in the past, and a if it is not.
For example this query, which returns calculations by date:
with member [Measures].[Year To Date] as
Sum(
{ IIF(strtovalue(
[Time Order Date].[Date In Past].Currentmember.membervalue
) = 0, null, [Measures].[Product Rev (with ship, no disc)]
) } *
PeriodsToDate(
[Time Order Date].[Fiscal Date].[Fiscal Year Name],
[Time Order Date].[Fiscal Date].CurrentMember
)
)
select
{[Measures].[Product Rev (with ship, no disc)],
[Measures].[Year To Date]} on 0,
[Time Order Date].[Date].Children on 1
from [Sales Analysis]
returns nulls in the [Year to Date] measure for all dates in the future.
This query, which returns calculations by the week:
with member [Measures].[Year To Date] as
Sum(
{ IIF(strtovalue(
[Time Order Date].[Date In Past].Currentmember.membervalue
) = 0, null, [Measures].[Product Rev (with ship, no disc)]
) } *
PeriodsToDate(
[Time Order Date].[Fiscal Date].[Fiscal Year Name],
[Time Order Date].[Fiscal Date].CurrentMember
)
)
select
{[Measures].[Product Rev (with ship, no disc)],
[Measures].[Year To Date]} on 0,
[Time Order Date].[Fiscal Week Name].Children on 1
from [Sales Analysis]
returns errors for all of the [Year To Date] values, I assume because there are more than one member in the week.
I would like to compare it with the last day of the week. How can I do that?
Thanks, --sw
To answer your question literally: Yes, you can get the first or last member of a set, and hence of a level, using the Head and Tail methods. Just note that these return a one-element set, hence you would often use Tail(something).Item(0).Item(0) to get a member.
However, as I understand your question, what you really need is to know if in the currently context, the member [Time Order Date].[Date In Past].[a] exists.
In that case, I would use
with member [Measures].[Year To Date] as
IIf(Intersect(EXISTING [Time Order Date].[Date In Past].[Date In Past].Members,
{[Time Order Date].[Date In Past].[a]}
).Count = 1,
NULL,
Sum(PeriodsToDate([Time Order Date].[Fiscal Date].[Fiscal Year Name],
[Time Order Date].[Fiscal Date].CurrentMember
)
[Measures].[Product Rev (with ship, no disc)]
)
)
select
{[Measures].[Product Rev (with ship, no disc)],
[Measures].[Year To Date]} on 0,
[Time Order Date].[Fiscal Week Name].Children on 1
from [Sales Analysis]
EXISTING gets the set of all Date In Past members that exist in the current context. And the intersection of that with the one-element set of member a has either 1 element (if a is member of the first set), or 0 elements (in case a is not contained in the set), which explains the condition for the IIf.