mdx put dimension member in measures - mdx

In a SSAS 2005 cube.
I have a product dimension, it has a attribute: sp (selling price)
I want to list the sp along with other facts for products. But the query below returns null for sp. Idea?
with member [measures].[sp] as
[Products].[Current Sp].currentmember
select {
[Measures].[Sold value]
, [measures].[sp]
} on 0
,
nonempty(
{[Sales order details].[Receipt No].[Receipt No].allmembers}
*{[Sales order details].[Line No].[Line No].allmembers}
*{[Products].[Product code].[Product code].allmembers}
, [Measures].[Sold value]
) on 1
from (
select [Time].[Day].&[20140430] on 0 from (
select [Branch].[Branch].&[2] on 0 from (
select [Sales order details].[Receipt No].[680207] on 0 from [Rmis]
)
)
)
update:
this is the final working query. I added [Products].[SKU].[SKU] because otherwise Current Sp returns 'All' (the null in original question is because of not using .Member_value). Current Sp and Product code are not related while they both related to [Products].[SKU].
with member [measures].[sp] as
[Products].[Current Sp].currentmember.MEMBER_value
select {
[Measures].[Sold value]
, [measures].[sp]
} on 0
,
nonempty(
{[Sales order details].[Receipt No].[Receipt No].allmembers}
*{[Sales order details].[Line No].[Line No].allmembers}
*{[Products].[Product code].[Product code].allmembers}
*{[Products].[SKU].[SKU]}
, [Measures].[Sold value]
) on 1
from (
select [Time].[Day].&[20140430] on 0 from (
select [Branch].[Branch].&[2] on 0 from (
select [Sales order details].[Receipt No].[680207] on 0 from [Rmis]
)
)
)

with member [measures].[sp] as
[Products].[Current Sp].CurrentMember.MemberValue
very close...just need to specify which property to display

Related

Convert sql rank to mdx rank

This is my sql code - I want to convert it into an mdx query.
Also those results set use into power bi report.
I am unable to write this sql query into mdx query. Can anybody help me?
Select * From(
Select
dense_RANK()over(partition by t.MonthName order by t.amount desc)rank
,t.*
From(
Select DSP.SalesPointShortName ,dd.MonthName
,SUM(fs.salesAmount)Amount
From FactSales FS
INNER JOIN DimDate dd on fs.DateKey=dd.DateKey
INNER JOIN DimSalesPoint DSP on DSP.SalesPointID=FS.SalesPointID
group by dsp.SalesPointShortName ,dd.MonthName
)as t
)as f where f.rank=1
My expected output is:
Lots of google searching i have the answer from below link
After following this link desire results comes.
https://bipassion.wordpress.com/2013/06/22/mdx-dense-rank/
`
WITH SET [Sorted Models] AS
// For each month get Top 25 records, choosed a Top 25 from business case
Generate (
[Dim Date].[Month Name].[Month Name].Members,
TopCount
( nonempty(
{
[Dim Date].[Month Name].CurrentMember
* [Dim Sales Point].[SalesPoint].[Sales Point Short Name].MEMBERS
}
,[Measures].[Sales Amount]
)
, 1
,[Measures].[Sales Amount]
)
)
//Select
//[Measures].[Sales Amount] on 0,[Sorted Models] on 1
//From [MdCubeTest]
//where [Dim Product Group Item].[Sub Category Name].&[Bag]
MEMBER [Measures].[Rank] AS
// Get the Rank of current member Tuple to Current Month Set
// Do not use Sorted Models set due to dynamic for each set
Rank (
(
[Dim Date].[Month Name].CurrentMember
, [Dim Sales Point].[SalesPoint].CurrentMember
)
, Generate ( [Dim Date].[Month Name].CurrentMember,
TopCount
( nonempty(
{ [Dim Date].[Month Name].CurrentMember
* [Dim Sales Point].[SalesPoint].[Sales Point Short Name]
}
,[Measures].[Sales Amount]
)
, 25
,[Measures].[Sales Amount]
)
)
, [Measures].[Sales Amount]
)
MEMBER [Measures].[Previous Set Index] AS
// Get the Set Index using Rank
(
[Measures].[Rank] - 2
)
MEMBER [Measures].[Dense Rank] AS
// Get the Dense Rank using the Index position value
CASE
WHEN [Measures].[Rank] = 1
THEN 1
ELSE
(
[Sorted Models].Item([Measures].[Previous Set Index]),
[Measures].[Dense Rank]
)
+
Iif
(
(
[Sorted Models].Item([Measures].[Previous Set Index]),
[Measures].[Sales Amount]
)
=
[Measures].[Sales Amount]
,0
,1
)
End
SELECT
{
[Measures].[Sales Amount]
, [Measures].[Rank]
, [Measures].[Dense Rank]
//, [Measures].[Previous Set Index]
} ON rows
,NON EMPTY
{
// FILTER Can be used to get the TOP 3 using DENSE RANK
FILTER (
[Sorted Models]
, [Measures].[Dense Rank] <=1
)
} ON columns
FROM [MdCubeTest]
where [Dim Product Group Item].[Sub Category Name].&[Bag]`

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)

Incorrect results in TOPCOUNT query for some attributes

I am trying to find out which year had the highest reseller sales across each of the State-Province. I am using Adventure Works DW 2008 R2 database.
Here is the query that I have got -
SELECT { [Reseller Sales Amount] } ON COLUMNS,
{
Generate (
[Geography].[Geography].[Country].Members,
TopCount (
Order (
Descendants (
[Geography].[Geography].CurrentMember
, [Geography].[Geography].[State-Province]
)
*[Date].[Calendar].[Calendar Year].Members,
[Reseller Sales Amount],
DESC
),
1
)
)
} ON ROWS
FROM [Adventure Works]
When it comes to France it's displaying (null) as result set. Also why is it not listing all the State-Provinces but only select few of them? Is it also possible to get the Country alongside the results. When I tried another cross-join I get error saying Geography hierarchy already exists.
To get rid of the null you can use BDESC rather than DESC. The extra B means that all natural hierarchy order will be Broken
SELECT { [Reseller Sales Amount] } ON COLUMNS,
{
Generate (
[Geography].[Geography].[Country].Members,
TopCount (
Order (
Descendants (
[Geography].[Geography].CurrentMember
, [Geography].[Geography].[State-Province]
)
*[Date].[Calendar].[Calendar Year].Members,
[Reseller Sales Amount],
BDESC //<<<
),
1
)
)
} ON ROWS
FROM [Adventure Works];
To add in country and avoid the error that you are seeing use the unused attribute hierarchy [Geography].[Country] rather than the user hierarchy [Geography].[Geography] which you already have on the 1 axis:
SELECT
{[Reseller Sales Amount]} ON COLUMNS
,{
Generate
(
[Geography].[Geography].[Country].MEMBERS
,TopCount
(
Order
(
[Geography].[Country].[Country]*
Descendants
(
[Geography].[Geography].CurrentMember
,[Geography].[Geography].[State-Province]
)*
[Date].[Calendar].[Calendar Year].MEMBERS
,[Reseller Sales Amount]
,bDESC
)
,1
)
)
} ON ROWS
FROM [Adventure Works];
Results are the following:
We will explore the universal option realization of TOP-filters on the levels.
TOP-rules can be set for every level.
It is used for detailed of analysis of data (Expand, Drilldown).
Aggregates are formed based on TOP.
Setting the Sort order will do, as long as the data is sorted in the
most Pivot Table.
SELECT
{ [Measures].[Reseller Sales Amount] }
DIMENSION PROPERTIES PARENT_UNIQUE_NAME , HIERARCHY_UNIQUE_NAME , CUSTOM_ROLLUP , UNARY_OPERATOR , KEY0 ON 0
, ORDER
( HIERARCHIZE
( HIERARCHIZE ( [Geography].[Geography].Levels ( 0 ).Members )
)
, ([Measures].[Reseller Sales Amount])
, BDESC
)
DIMENSION PROPERTIES PARENT_UNIQUE_NAME , HIERARCHY_UNIQUE_NAME , CUSTOM_ROLLUP , UNARY_OPERATOR , KEY0 ON 1
FROM
( SELECT
{ Generate
( [Geography].[Geography].Levels ( 3 ).Members
, TopCount
( Filter
( [Geography].[Geography].CurrentMember.Children
, NOT IsEmpty ( [Measures].[Reseller Sales Amount] )
)
, 3
, [Measures].[Reseller Sales Amount]
)
)
} ON COLUMNS
FROM
( SELECT
{ Generate
( [Geography].[Geography].Levels ( 2 ).Members
, TopCount
( Filter
( [Geography].[Geography].CurrentMember.Children
, NOT IsEmpty ( [Measures].[Reseller Sales Amount] )
)
, 3
, [Measures].[Reseller Sales Amount]
)
)
} ON COLUMNS
FROM
( SELECT
{ Generate
( [Geography].[Geography].Levels ( 1 ).Members
, TopCount
( Filter
( [Geography].[Geography].CurrentMember.Children
, NOT IsEmpty ( [Measures].[Reseller Sales Amount] )
)
, 3
, [Measures].[Reseller Sales Amount]
)
)
} ON COLUMNS
FROM
( SELECT
{ Generate
( [Geography].[Geography].Levels ( 0 ).Members
, TopCount
( Filter
( [Geography].[Geography].CurrentMember.Children
, NOT IsEmpty ( [Measures].[Reseller Sales Amount] )
)
, 3
, [Measures].[Reseller Sales Amount]
)
)
} ON COLUMNS
FROM
( SELECT
{ [Geography].[Geography].Levels ( 0 ).Members } ON COLUMNS
FROM [Adventure Works]
)
)
)
)
)
CELL PROPERTIES BACK_COLOR , CELL_ORDINAL , FORE_COLOR , FONT_NAME , FONT_SIZE , FONT_FLAGS , FORMAT_STRING , VALUE , FORMATTED_VALUE , UPDATEABLE , ACTION_TYPE

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]

MDX Calculated Member in where clause

I am new to MDX queries. I have the following query and would like to limit the results to only show records where the Margin Pct is > 0. Any help would appreciated.
WITH
MEMBER [Measures].[Margin Pct] as ([Measures].[Mgmt Margin Excluding Markup]/[Measures].[Net Sales])*100,format_string="0.0"
MEMBER [Measures].[Mgmt Margin] as [Measures].[Mgmt Margin Excluding Markup],format_string="0.0"
MEMBER [Measures].[Mgmt Cost Unit] as [Measures].[Mgmt Cost Unit Excluding Markup],format_string="0.00"
MEMBER [Measures].[FOBPrce] as [Measures].[FOB Price],format_string="0.00"
MEMBER [Measures].[CommUnt] as [Measures].[Comm/Unit],format_string="0.000"
MEMBER [Measures].[RebUnt] as [Measures].[Reb/Unit],format_string="0.00"
MEMBER [Measures].[FrtUnt] as [Measures].[Frt/Unit],format_string="0.00"
MEMBER [Measures].[PriceUnt] as [Measures].[Price/Unit],format_string="0.00"
SELECT NON EMPTY {
[Measures].[Rpt Inv Shp Date],
[Measures].[Lbs Shipped],
[Measures].[Net Sales],
[Measures].[FOBPrce],
[Measures].[CommUnt],
[Measures].[RebUnt],
[Measures].[FrtUnt],
[Measures].[PriceUnt],
[Measures].[Mgmt Cost Unit],
[Measures].[Mgmt Margin],
[Measures].[Margin Pct]
} ON COLUMNS, NON EMPTY {
(
[Item].[Group Sort].[Group Sort],
[Item].[Form Sort].[Form Sort],
[Item].[Specie Sort].[Specie Sort],
{[Item].[Group thru Item ID].[Group].ALLMEMBERS},
[Shrimp Group].[Shrimp Group].[Shrimp Group Name].ALLMEMBERS ,
{[Item].[Form].[Form].ALLMEMBERS},
[Item].[Meat - In Shell].[Meat or Inshell].ALLMEMBERS ,
[Item].[Super Specie].[Super Specie].ALLMEMBERS ,
{[Item].[Species].[Species].ALLMEMBERS},
{[Item].[Item ID].[Item ID].ALLMEMBERS},
[Item].[Desc-ItemID].[Item ID Description].ALLMEMBERS ,
[Item].[Package Type].[Packaging].ALLMEMBERS ,
{[Brand].[Brand].[Brand Name].ALLMEMBERS},
{[Warehouse].[Warehouse].[Warehouse Code].ALLMEMBERS},
[Order Invoice Lot].[Order-Invoice-Lot].[Lot].ALLMEMBERS ,
{[Customer Account Number].[Customer Account No].Levels(1)},
{[Ship To Customer].[Customer Name].Levels(1)},
{[Sales Person].[Person].Levels(1)},
[Order Invoice Lot].[Sales Order].[Sales Order].ALLMEMBERS ,
[Order Invoice Lot].[Invoice].[Invoice].ALLMEMBERS
)
} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS FROM (
SELECT StrToSet( '{[Breaded Group].[Breaded Group].[All]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Inventory Category].[Inventory Category].[All]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Sold To Customer].[Customer Buying Group].[All]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Ship To Customer Sales Group].[Ship To Customer Sales Group].[All]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Sold To Customer].[Customer Legal Group].[All]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Country Of Origin].[Long Name].[All]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Is Sample].[Sample].[Description].[Regular]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Invoicing Status].[Invoicing Status-Detail].[Detail].[Sale Only],[Invoicing Status].[Invoicing Status-Detail].[Detail].[Credit Only]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Invoice Date].[Fiscal Year-Quarter-Month].[Fiscal Month].[Jul-FY13]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Sold To Customer].[Name].[All]}' ,CONSTRAINED ) ON COLUMNS FROM (
SELECT StrToSet( '{[Is NRV].[NRV].[All]}' ,CONSTRAINED )
ON COLUMNS FROM [FishTrackerReporting] ) ) ) ) ) ) ) ) ) ) )
I have tried to use a where clause against the [Measures].[Margin Pct] but get this error:
The WHERE clause function expects a tuple set expression for the argument. A string or numeric expression was used.
I also tried to use a filter after the on columns part of the query but get out of memory issues so I think I am missing something.
Too bad the reference of MDX is not the best. You can use HAVING or FILTER to achieve what you want. I'd go with HAVING since it's easier to use.
Please have a look here and find the last example.