Friday, March 23, 2012
Median
median sales price of a particular item from my sales history.
From Northwind:
select
Median = avg (Quantity)
from
(
select
Quantity = min (Quantity) -- first value ...
from
(
select top 50 percent -- ... in upper half
Quantity = sum (Quantity)
from
[Order Details]
group by
OrderID
order by
Quantity desc
) x
union all
select
Quantity = max (Quantity) -- last value ...
from
(
select top 50 percent -- ... in lower half
Quantity = sum (Quantity)
from
[Order Details]
group by
OrderID
order by
Quantity asc
) x
) y
Tom
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
..
"Ron Hinds" <__ron__dontspamme@.wedontlikespam_garageiq.com> wrote in message
news:O1vtO0OrFHA.1984@.tk2msftngp13.phx.gbl...
How do I find the median value for a column? What I want to do is find the
median sales price of a particular item from my sales history.
Median
median sales price of a particular item from my sales history.From Northwind:
select
Median = avg (Quantity)
from
(
select
Quantity = min (Quantity) -- first value ...
from
(
select top 50 percent -- ... in upper half
Quantity = sum (Quantity)
from
[Order Details]
group by
OrderID
order by
Quantity desc
) x
union all
select
Quantity = max (Quantity) -- last value ...
from
(
select top 50 percent -- ... in lower half
Quantity = sum (Quantity)
from
[Order Details]
group by
OrderID
order by
Quantity asc
) x
) y
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
.
"Ron Hinds" < __ron__dontspamme@.wedontlikespam_garagei
q.com> wrote in message
news:O1vtO0OrFHA.1984@.tk2msftngp13.phx.gbl...
How do I find the median value for a column? What I want to do is find the
median sales price of a particular item from my sales history.
Median
median sales price of a particular item from my sales history.From Northwind:
select
Median = avg (Quantity)
from
(
select
Quantity = min (Quantity) -- first value ...
from
(
select top 50 percent -- ... in upper half
Quantity = sum (Quantity)
from
[Order Details]
group by
OrderID
order by
Quantity desc
) x
union all
select
Quantity = max (Quantity) -- last value ...
from
(
select top 50 percent -- ... in lower half
Quantity = sum (Quantity)
from
[Order Details]
group by
OrderID
order by
Quantity asc
) x
) y
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
.
"Ron Hinds" <__ron__dontspamme@.wedontlikespam_garageiq.com> wrote in message
news:O1vtO0OrFHA.1984@.tk2msftngp13.phx.gbl...
How do I find the median value for a column? What I want to do is find the
median sales price of a particular item from my sales history.sql
Wednesday, March 21, 2012
Measures dependant on another measure.
I have a fact table that has sales data for products.
I have to fields in my fact table, WantDateId and InvoicedDateId which are integer representation of a date that join to the time dimension table.
I also have measures for feet, pounds, sales dollars, and material cost.
I have a bunch of dimensions for product type, sales rep, territory, shipped date…
I my cube I want to show a picture of sales for three things broken down over the dimensions.
What has been ordered
What has been invoiced
What is outstanding (backlog).
Most important is the current month where some will be shipped and some will be backlog.
The key here is that if the invoiceDateId = 0 then it has not shipped.
In order for aggregations to work, do I have to add columns to my fact table?This would mean I would need three of each measure.Is there a way to get this with calculated measures?
Hello. Here you have some links to design advices for your question.
http://www.kimballgroup.com/html/designtipsPDF/DesignTips2000%20/KimballDT2MultipleTime.pdf
http://www.kimballgroup.com/html/designtipsPDF/DesignTips2002/KimballDT37ModelingPipeline.pdf
http://www.kimballgroup.com/html/designtipsPDF/DesignTips2003/KimballDT42Combining.pdf
http://www.kimballgroup.com/html/articlesArchitecture/articlesAdvancedFact.html
I am not sure of what you would like to accomplish but sales data and snapshots, like OrderStock are normally not included in the same fact table.
HTH
Thomas Ivarsson
Monday, March 19, 2012
Measure expressions and currency conversion
OK - here is my scenario... I have a fact table containing one measure: Sales. The values of the measure are stored in the same currency (DKK) for all records. In another fact table I have exchange rates for four different currencies per day for a 15 year period (approx. 22000 records). I have built a cube around these fact tables containing two measure groups.
Measure Group 1
Measure: Sales
Dimensions: Time, Product, Company
Measure Group 2
Measure: Exchange rate
Dimensions: Time, Currency
The only shared dimension between the two measure group is thus Time... The currency dimension contains one attribute hierarchy (CurrencyCode) with IsAggregatable set to False and DefaultMember set to DKK.
For the measure Sales, I have created a measure expression like this:
[Measures].[Sales] / [Measures].[Currency]
I thought this should work - i.e. give me the opportunity to select any of the four available currencies and have the value of Sales displayed accordingly. However - it does not. Instead my Sales measures is multiplied by 4, which corresponds to the number of currencies for which I have exchange rates in my exchange rate fact table. Why is this? Am I not modelling the scenario correctly?
I hope someone can help, since this really puzzles me!
I think I might know what's going on here: you need to give your Currency dimension a many-to-many relationship with Measure Group 1 using Measure Group 2 as the intermediate measure group. Does this work?
Regards,
Chris|||Hi Chris,
Sound very reasonable... I will give it a try. Do you think this approach will perform better than an MDX script like this (inspired by one of your own blog entries: http://spaces.msn.com/members/cwebbbi/Blog/cns!1pi7ETChsJ1un_2s41jm9Iyg!299.entry)
([Measures].[Sales],LEAVES([Time]),LEAVES([Currency])) = [Measures].[Sales] / [Measures].[Exchange Rate]
?
Chris, thanks for your input. It would be a shame to say that Microsoft has spent too much time documenting how to use measure expressions. |||Well, I think it might perform a little bit better, but to be honest I'm not sure. You'll have to try both!|||Michael,
Since you've marked my first reply as solving your problem, I don't suppose you can tell us what the performance on your cube is like now? Do your currency conversion queries run fast enough with measure expressions? I'd really be interested to know.
Chris|||Hi Chris
Unfortunately, I have not yet had the time to implement the solution with measure expressions yet (currently, we are using the MDX Script approach for solving the problem). I will, however, test it with measure expressions as well and report back my findings. I am sure that the solution you pointed out is correct wrt. using measure expressions (also after having reviewed the implementation in the AdventureWorks cube), which is why I marked your answer as correct.
I really appreciate your input!|||
Mosha's latest blog entry is also highly relevant to this problem:
http://sqljunkies.com/WebLog/mosha/archive/2005/12/06/multiplication_perf.aspx
Lesson learned... I guess.
Still has not tested the measure expression vs. the MDX Script approach, though...|||Hi,
I finally got around to test this... My results show that measure expressions performs marginally better compared to using MDX scripts. But the two approaches perform almost just as well (and they do perform remarkably good) - a statement Mosha has previously made as well. So which approach do I prefer? Well - since using measure expressions has the best peformance (not by much, though), I will be using these. Perhaps there are scenarios where you cannot model your UDM to fit this approach, and MDX scripts might be more appropriate then...
I hope that these findings are useful to others...|||Well... What do you know?! I just found a scenario myself in which using measure expressions for currency conversion is not possible. One of my measures used "LastNonEmpty" as the aggregation function and measure expressions do not support this... Will of course be using the MDX script approach for solving this...
Measure expressions and currency conversion
OK - here is my scenario... I have a fact table containing one measure: Sales. The values of the measure are stored in the same currency (DKK) for all records. In another fact table I have exchange rates for four different currencies per day for a 15 year period (approx. 22000 records). I have built a cube around these fact tables containing two measure groups.
Measure Group 1
Measure: Sales
Dimensions: Time, Product, Company
Measure Group 2
Measure: Exchange rate
Dimensions: Time, Currency
The only shared dimension between the two measure group is thus Time... The currency dimension contains one attribute hierarchy (CurrencyCode) with IsAggregatable set to False and DefaultMember set to DKK.
For the measure Sales, I have created a measure expression like this:
[Measures].[Sales] / [Measures].[Currency]
I thought this should work - i.e. give me the opportunity to select any of the four available currencies and have the value of Sales displayed accordingly. However - it does not. Instead my Sales measures is multiplied by 4, which corresponds to the number of currencies for which I have exchange rates in my exchange rate fact table. Why is this? Am I not modelling the scenario correctly?
I hope someone can help, since this really puzzles me!
I think I might know what's going on here: you need to give your Currency dimension a many-to-many relationship with Measure Group 1 using Measure Group 2 as the intermediate measure group. Does this work?
Regards,
Chris|||Hi Chris,
Sound very reasonable... I will give it a try. Do you think this approach will perform better than an MDX script like this (inspired by one of your own blog entries: http://spaces.msn.com/members/cwebbbi/Blog/cns!1pi7ETChsJ1un_2s41jm9Iyg!299.entry)
([Measures].[Sales],LEAVES([Time]),LEAVES([Currency])) = [Measures].[Sales] / [Measures].[Exchange Rate]
?
Chris, thanks for your input. It would be a shame to say that Microsoft has spent too much time documenting how to use measure expressions. |||Well, I think it might perform a little bit better, but to be honest I'm not sure. You'll have to try both!|||Michael,
Since you've marked my first reply as solving your problem, I don't suppose you can tell us what the performance on your cube is like now? Do your currency conversion queries run fast enough with measure expressions? I'd really be interested to know.
Chris|||Hi Chris
Unfortunately, I have not yet had the time to implement the solution with measure expressions yet (currently, we are using the MDX Script approach for solving the problem). I will, however, test it with measure expressions as well and report back my findings. I am sure that the solution you pointed out is correct wrt. using measure expressions (also after having reviewed the implementation in the AdventureWorks cube), which is why I marked your answer as correct.
I really appreciate your input!|||
Mosha's latest blog entry is also highly relevant to this problem:
http://sqljunkies.com/WebLog/mosha/archive/2005/12/06/multiplication_perf.aspx
Lesson learned... I guess.
Still has not tested the measure expression vs. the MDX Script approach, though...|||Hi,
I finally got around to test this... My results show that measure expressions performs marginally better compared to using MDX scripts. But the two approaches perform almost just as well (and they do perform remarkably good) - a statement Mosha has previously made as well. So which approach do I prefer? Well - since using measure expressions has the best peformance (not by much, though), I will be using these. Perhaps there are scenarios where you cannot model your UDM to fit this approach, and MDX scripts might be more appropriate then...
I hope that these findings are useful to others...|||Well... What do you know?! I just found a scenario myself in which using measure expressions for currency conversion is not possible. One of my measures used "LastNonEmpty" as the aggregation function and measure expressions do not support this... Will of course be using the MDX script approach for solving this...
MdxScript error using role security
The following error is displayed when trying to open a cube...
"An error occurred in the application: MdxScript(Quota Sales)(30,24) Then dimension '[Sales]' was not found in the cube when string, [Sales], was parsed"
I am using role security and am trying to hide a base measure [Measures].[Sales] along with two calculated measures that use the base measure. The cube does not have a [Sales] dimension.
I am using the Dimension Data tab within the role designer and denying all measures then selecting only the ones I want to appear in the cube. Those are written automatically into the "allowed member set" panel on the advanced tab. I have also tried allowing all members and then unchecking the Sales measure and specifying the [Measures].[Sales] measure and the two calculated measures in the "denied member set" panel on the advanced tab. I get the error either way.
I am using SSAS 2k5 and I receive the error when trying to open the cube using the ProClarity Professional client, although I don't think ProClarity is the issue.
Thanks in advance for any help.
Jay Hotchkiss
I've heard of some problems with security and MDX Scripts, although I can't find the relevant link at the moment...
...but, can you post the section of your MDX Script that's causing the error, ie everything around line 30? Is it by any chance something (like a calculated member definition, or named set) which refers to the Sales measure as
[Sales]
rather than using the full unique name of
[Measures].[Sales]
? If so, can you try using the full unique name in the MDX Script and seeing if you still get the same error?
Chris
|||So that's what MDXScript is referring to, the calculated measure script! Yes, I have a number of calculations (time intellligence stuff) that are checking the Sales measure for NonEmpty behavior. I missed those. I'll do some work on the script and reply back here with the results. I think I can check another measure for NonEmpty instead of having to list all those calculated measure in the "denied set" for the role's security. Thanks very much. Jay
|||Thanks Christopher, I had a few calculated sets that were referring to my Sales measure. I appreciate the help. Jay|||For each dimension, you can set the MdxMissingMemberMode property in SSAS 2005. Perhaps this could solve your problem.
MdxScript error using role security
The following error is displayed when trying to open a cube...
"An error occurred in the application: MdxScript(Quota Sales)(30,24) Then dimension '[Sales]' was not found in the cube when string, [Sales], was parsed"
I am using role security and am trying to hide a base measure [Measures].[Sales] along with two calculated measures that use the base measure. The cube does not have a [Sales] dimension.
I am using the Dimension Data tab within the role designer and denying all measures then selecting only the ones I want to appear in the cube. Those are written automatically into the "allowed member set" panel on the advanced tab. I have also tried allowing all members and then unchecking the Sales measure and specifying the [Measures].[Sales] measure and the two calculated measures in the "denied member set" panel on the advanced tab. I get the error either way.
I am using SSAS 2k5 and I receive the error when trying to open the cube using the ProClarity Professional client, although I don't think ProClarity is the issue.
Thanks in advance for any help.
Jay Hotchkiss
I've heard of some problems with security and MDX Scripts, although I can't find the relevant link at the moment...
...but, can you post the section of your MDX Script that's causing the error, ie everything around line 30? Is it by any chance something (like a calculated member definition, or named set) which refers to the Sales measure as
[Sales]
rather than using the full unique name of
[Measures].[Sales]
? If so, can you try using the full unique name in the MDX Script and seeing if you still get the same error?
Chris
|||So that's what MDXScript is referring to, the calculated measure script! Yes, I have a number of calculations (time intellligence stuff) that are checking the Sales measure for NonEmpty behavior. I missed those. I'll do some work on the script and reply back here with the results. I think I can check another measure for NonEmpty instead of having to list all those calculated measure in the "denied set" for the role's security. Thanks very much. Jay
|||Thanks Christopher, I had a few calculated sets that were referring to my Sales measure. I appreciate the help. Jay|||For each dimension, you can set the MdxMissingMemberMode property in SSAS 2005. Perhaps this could solve your problem.
Monday, March 12, 2012
MDX to dynamically insert calculated members into row?
I need some help writing the MDX for selecting Row members in a particular
query. Here's the background:
The query shows sales results by region and office. The row members should
be like this:
OfficeA
OfficeB
OfficeD
Region1
Region1 (SOC)
OfficeE
OfficeF
OfficeG
OfficeJ
Region2
Region2 (SOC)
World
World (SOC)
There are two things to note about this:
1. The set of offices is being filtered to only show the "active" ones (i.e.
offices which are operational - offices C, H & I have closed down)
2. The Region1, Region2 and World members are straightforward sums of their
(active) children. However, the Region1 (SOC), Region2 (SOC) and World (SOC)
members are calculated - the idea is that they represent a "Same Office
Comparison", in other words only sum the offices that were active both last
year and this year.
The dimensions used to generate the row members are:
1. Region with levels Region and Office
2. Active Office with levels Active (True/False) and SOC (True/False)
This is the MDX I used to generate the members EXCEPT for the "SOC" members:
Member [Region].[World] as
' [Region].[All Region] ' , solve_order = 1
Non Empty
{
Filter(
Hierarchize( Except( [Region].Members , [Region].[All Region] } )
, POST ) ,
[Active Office].[All Active Office].[True] > 0) ,
[Region].[World]
}
ON ROWS
Where I am stuck is generating the "SOC" members and including them in the
set of row members in the correct order. I need to do this without hard
coding any references to specific regions or offices (regions and offices
change over time).
Any ideas?
Many thanks
HI Laurence,
As you have no doubt found, it is hard to insert a calculated member in a specific location in a hierarchy. What I would suggest is to create "real" members in your dimension tables for the "(SOC)" members, but don't have any data associated with them. Then use an assignment in your MDX script to calcuate the value for these members
You could hard code a calculation something like the following:
([Region 1 (SOC)]) = Aggregate([Region].[Region].&[Region 1].Chidren,[Region].[SOC].[True])
But if it were me I think I would probably want to make the calc more generic. You could possibly add an attribute or add a "Total" member to the SOC attribute so that you could pull out just the SOC total members and then grab the children of the previous member and filter them by [SOC].[True] - or something like that
I do need to make the solution as generic as possible as the regions do change .... can you explain a bit more what you mean by '.... add an attribute or add a "Total" member to the SOC attribute ....'
|||Sure. When I said "add an attribute" I was thinking that you could either change your existing [SOC] column to contain another value, so it would have "True", "False" & "Total" or you could add another column to your dimension table. Then what I would do is to add another row into your dimension table at the same level as the "Region 1" member, but called "Region 1 SOC" and tagged with a specific attribute value so that you could filter down to just these members .You would not have any facts associated with these members, they would simply be placeholders at a specific positions in the dimension. You would then use an assignment in the MDX Script to set the values for these members.
You might then be able use an assignment similar to the following (which is more psuedo code, but hopefully it will help illustrate my thought process)
Code Snippet
SCOPE [Region].[SOC].[SOC].[SOC Total] -- grab just the members tagged as "SOC Total"
-- i'm not clear on your exact structures and logic, but assuming
-- that the "SOC total" members are ordered after the real region totals,
-- the code below grabs the real region's children and adds them up,
-- filtering on only those members where [SOC] is true.
this = aggregate({[Region].[Region].[Region].Prevmember.children
, [Region].[SOC].[SOC].&{True]})
END SCOPE;
|||Ah-ha, I think I got it! I will try it out, thanks very much.
MDX select with no aggregation
Dear All,
I tryed to do the following
OpenQuery([ssas DS], select {[Measures].[Weekday], [Measures].[Weekday], [Measures].[sales count]} on columns,
{[Dim Time].[Weekday].members} on rows
from CubeName)
It returns me the followings:
Weekday Weekday Sales Count
All 5 5 20402295
1 1 1 3445560
2 2 2 3950970
3 3 3 4532115
4 4 4 4498965
5 5 5 3974685
But I don't want the row "All 5 5 20402295" to be included in the row how can I remove it from the rowset?
Thanks
Tony Chun Tung Siu
Try replacing {[Dim Time].[Weekday].members} with {[Dim Time].[Weekday].[Weekday].members}Friday, March 9, 2012
MDX Ranking Question
Hi
We are trying to create a Ranking of a dimension based on multiple different measures. An example would be to rank the Sales Consultant by Num Sales, Total Sales and Num Customers.
In SQL, I would create three different temp tables, with the Sales Consultant, Measure and Ranking for each different measure. Then you could join these results together and generate the final ranking based on this result set.
Now the problem we are having is doing this in MDX. You can only order by one of the measures in any result set. Any way I look at this, it looks to be a multi step process to create this data. The only way I can think that this might work is to create a stored proc, but then this will limit the users ability to slice data by all the dimensions.
Are there any ideas on how we can do this that I have missed?
Thanks
Michael
Hi Michael,
Here's a sample Adventure Works ranking query - could you give more details, with examples, of your scenario?
With
Set [OrderedProducts] as
Order([Product].[Category].[Category]
* {[Measures].[Order Quantity]},
[Measures].[Order Quantity], BDESC)
+ Order([Product].[Category].[Category]
* {[Measures].[Sales Amount]},
[Measures].[Sales Amount], BDESC)
+ Order([Product].[Category].[Category]
* {[Measures].[Total Product Cost]},
[Measures].[Total Product Cost], BDESC)
Member [Measures].[QuantityRank] as
Rank(([Product].[Category].CurrentMember,
[Measures].[Order Quantity]),
Exists([OrderedProducts],
{[Measures].[Order Quantity]}))
Member [Measures].[SalesRank] as
Rank(([Product].[Category].CurrentMember,
[Measures].[Sales Amount]),
Exists([OrderedProducts],
{[Measures].[Sales Amount]}))
Member [Measures].[CostRank] as
Rank(([Product].[Category].CurrentMember,
[Measures].[Total Product Cost]),
Exists([OrderedProducts],
{[Measures].[Total Product Cost]}))
select
{[Measures].[Order Quantity],
[Measures].[QuantityRank],
[Measures].[Sales Amount],
[Measures].[SalesRank],
[Measures].[Total Product Cost],
[Measures].[CostRank]} on 0,
Extract(Exists([OrderedProducts],
{[Measures].[Order Quantity]}),
[Product].[Category]) on 1
from [Adventure Works]
--
Order Quantity QuantityRank Sales Amount SalesRank Total Product Cost CostRank
Bikes 90,220 1 $94,620,526.21 1 $84,105,429.60 1
Clothing 73,598 2 $2,117,613.45 3 $1,748,777.45 3
Accessories 61,931 3 $1,272,057.89 4 $637,590.73 4
Components 49,027 4 $11,799,076.66 2 $10,766,110.18 2
Hi
Thanks very much!!!! This just shows how far I have to go to understand MDX...
I took your query and expanded it to give what we were looking for:
With
Set [OrderedProducts] as
Order([Product].[Category].[Category]
* {[Measures].[Order Quantity]},
[Measures].[Order Quantity], BDESC)
+ Order([Product].[Category].[Category]
* {[Measures].[Sales Amount]},
[Measures].[Sales Amount], BDESC)
+ Order([Product].[Category].[Category]
* {[Measures].[Total Product Cost]},
[Measures].[Total Product Cost], BDESC)
Member [Measures].[QuantityRank] as
Rank(([Product].[Category].CurrentMember,
[Measures].[Order Quantity]),
Exists([OrderedProducts],
{[Measures].[Order Quantity]}))
Member [Measures].[SalesRank] as
Rank(([Product].[Category].CurrentMember,
[Measures].[Sales Amount]),
Exists([OrderedProducts],
{[Measures].[Sales Amount]}))
Member [Measures].[CostRank] as
Rank(([Product].[Category].CurrentMember,
[Measures].[Total Product Cost]),
Exists([OrderedProducts],
{[Measures].[Total Product Cost]}))
Member [Measures].[OverallActivity] as
1/(
(1/[Measures].[QuantityRank])^2 +
(1/[Measures].[SalesRank])^2 +
(1/[Measures].[CostRank])^2
)
Set [OrderedActivity] as
Order([Product].[Category].[Category]
* {[Measures].[OverallActivity]},
[Measures].[OverallActivity], BASC)
Member [Measures].[ActivityRank] as
Rank(([Product].[Category].CurrentMember,
[Measures].[OverallActivity]),
Exists([OrderedActivity],
{[Measures].[OverallActivity]}))
select
{[Measures].[Order Quantity],
[Measures].[QuantityRank],
[Measures].[Sales Amount],
[Measures].[SalesRank],
[Measures].[Total Product Cost],
[Measures].[CostRank],
[Measures].[OverallActivity],
[Measures].[ActivityRank]} on 0,
Extract(Exists([OrderedProducts],
{[Measures].[Order Quantity]}),
[Product].[Category]) on 1
from [Adventure Works]
Which gives us the Overall ranking of the Product by these three rankings:
Order Quantity QuantityRank Sales Amount SalesRank Total Product Cost CostRank OverallActivity ActivityRank
Bikes 90,220 1 $94,620,526.21 1 $84,105,429.60 1 0.333333333333333 1
Clothing 73,598 2 $2,117,613.45 3 $1,748,777.45 3 2.11764705882353 3
Accessories 61,931 3 $1,272,057.89 4 $637,590.73 4 4.23529411764706 4
Components 49,027 4 $11,799,076.66 2 $10,766,110.18 2 1.77777777777778 2
Once again, Thanks for the help!!!!
|||Hi
A further question to this. We are also wanting to make this ranking Time aware. Currently, you can only run the quesry for a specific time period (eg [dimDate].[Year].[2006]). If you try to create a crossjoin in the report to have all the above measures by 2005 and 2006, then the ranking does not work anymore. As far as I understand it, this will be the case as the Order Set does not know about the time dimension, and hasn't ordered the Products by Time.
So, how I see this is that I will need to add the time Dim to the order Set, but at what level. We would like to make this dynamic within the cube and would like the ranking to work in a year on year, quarter on quarter, month on month ...etc level. I would think that ordering by the lowest level of time (day in my case) might work, but would create a set that is extremely large.
Would this work, or is there a better way to do this?
Michael
|||Hi Michael,
This is a common requirement, but it's beyond the ordered SQL temp table approach. You could use Rank() dynamically, as shown in the query below. But there might be a performance penalty - see this 2005 thread from the OLAP newsgroup:
With
Member [Measures].[QuantityRank] as
Rank([Product].[Category].CurrentMember,
[Product].[Category].[Category],
[Measures].[Order Quantity])
Member [Measures].[SalesRank] as
Rank([Product].[Category].CurrentMember,
[Product].[Category].[Category],
[Measures].[Sales Amount])
Member [Measures].[CostRank] as
Rank([Product].[Category].CurrentMember,
[Product].[Category].[Category],
[Measures].[Total Product Cost])
Member [Measures].[OverallActivity] as
1/(
(1/[Measures].[QuantityRank])^2 +
(1/[Measures].[SalesRank])^2 +
(1/[Measures].[CostRank])^2
)
Member [Measures].[ActivityRank] as
Rank([Product].[Category].CurrentMember,
[Product].[Category].[Category],
-[Measures].[OverallActivity])
select
{[Measures].[Order Quantity],
[Measures].[QuantityRank],
[Measures].[Sales Amount],
[Measures].[SalesRank],
[Measures].[Total Product Cost],
[Measures].[CostRank],
[Measures].[OverallActivity],
[Measures].[ActivityRank]} on 0,
[Date].[Calendar Year].[Calendar Year]
* [Product].[Category].[Category] on 1
from [Adventure Works]
|||Thanks for all the help! That is working fine, and is not too slow. It is not a commonly used calc so shouldn't pose a problem.MDX question
Hi,
Please help with this MDX.
I need a report with situation of the sales( there are in currency $) and I need to be in EURO.
How could I achieve this?
Thanks,
You can create a calculated member to convert USD to Euro at specified rate (in the example below I assumed $1 = € 0.81) and define its format string accordingly. Like such:
WITH MEMBER [Measures].[SalesInEuro] AS
[Measures].[Sales Amount] * 0.81,
FORMAT_STRING = '€ #,##0.00'
SELECT { [Measures].[Sales Amount], [Measures].[SalesInEuro] } ON 0 FROM Finance
MDX question
Hi,
Here is my question.
For the last year special store sales amount, we only calculate sales amount based on special store lists in current selected date.
If a store in current date is not a special store then we do not include this store for last year sales amount (even if this store was special store in last year). This means we don't care about last year store status and we only identify the special store status of all stores according to the date we selected.
In the fact, I join the flag ( which represents a special store) and added one colum for special sales calcualtion. (flag * net sales). In the cube, Currnet speical sales are correct but I couldn't get correct last year specail sales amount.
Below is the calculation for last year specail store sales amount but the numbers do not match.
IIF(([Measures].[flag], [Date].[Fiscal Hierarchy].CurrentMember)=False,0,
(parallelperiod([Date].[Fiscal Hierarchy].[fiscal year],1),[Measures].[Special Sales]))
Please give me some comments.
Thanks in advance.
Boolean values as measures just don't work well. There probably would be a way to write the MDX calculation, but the performance would be really bad probably to the point of being unusable.
You could implement the flag as it's own dimension, but probably the best approach is to implement the store dimension as a slowly changing dimension. What you would be after is a type 2 changing dimension. If you are not aware of what this is, you are basically adding a new record to the store dimension when the value of the flag changes
Below is a simplified example, notice that "Store 1" has StoreID = 1 and StoreID = 3. It is often common to store effective dates against the records for slowly changing dimensions to make it easier to figure out which dimension record applies to a given fact record.
StoreID StoreName SpecialFlag
1 Store 1 False
2 Store 2 False
3 Store 1 True
Then when you insert facts you insert the StoreID that was effective for the date of the respective facts.
StoreID DateID SalesAmount
1 20060101 100
3 20070601 100
Using this technique you would be able to create a "Special Store" attribute in your store dimension and filter using that.
|||Thanks for the reply. I think I didn't give you a right explanation.
I understand what you suggested but could you take a look at this one more time?
Below is the details how it looks like :
Selected date : 20070101
Last year date : 20060101
Sales Fact
ID Store_key Date Sales
1 store 1 20070101 100.00
2 store 2 20070101 150.00
3 store 3 20070101 100.00
4 store 1 20060101 30.00
5 store 2 20060101 30.00
6 store 3 20060101 30.00
There is an anther fact table which has a flag.
Special_Store Fact
Special_Store_key Store_key Date Key SS_flag
1 store 1 20070101 True
2 store 1 20060101 False
3 store 2 20070101 False
4 store 2 20060101 True
5 store 3 20070101 False
6 store 3 20060101 True
Now I joined this 2 Fact to add Specail Store Sales into the Sales Fact
ID Store_key Date Sales SS_flag SS_Sales (SS flag * Sales)
1 store 1 20070101 100.00 True 100.00
2 store 2 20070101 150.00 False 0.00
3 store 3 20070101 100.00 True 100.00
4 store 1 20060101 30.00 False 0.00
5 store 2 20060101 30.00 True 30.00
6 store 3 20060101 30.00 True 30.00
From this data,
we calulate SS sales amount and the total is 130.00 which is not a problem to calculate in the cube.
My problem is that for the last year SS sales amount with time calculation we developed inside the cube,
time calculation sums up all SS_Sales, so last year total is 60.00.
But from the selected date , we do not consider last year store status. We only care current Special Store and From this list we calculate last year value. So, in this data, we need to get 30 (excluding store 2 since store 2 is not special store in the selected date 20070101).
As you mentioned, if boolean value does not work properly and affects performance then how can I approach in this situation? Any Suggestions?
Please let me know.
Thanks.
|||I am pretty sure that my original suggestion of not adding this flag to the fact table and implementing a slowly changing dimension would work. But possibly a simpler solution might be to insert NULL insead of 0.00 into the SS_Sales measure and then do something like the following, which basically checks if there is a value for [Special Sales] in the current period.
IIF(IsEmpty({([Measures].[Special Sales], [Date].[Fiscal Hierarchy].CurrentMember)}),NULL,
(parallelperiod([Date].[Fiscal Hierarchy].[fiscal year],1),[Measures].[Special Sales]))
The only problem with this is that, if you have a hierarchy over the stores so that they roll up into groups of some sort then you will get incorrect results at the higher levels. If any store in the group was "sepcial" last year the value for the entire group would be included
Which would mean that you might have to do something like the following to force this expression to be evaluated over individual store members.
SUM( EXISTING [Store].[Store].[Store].Members ,
IIF(IsEmpty({([Measures].[Special Sales], [Date].[Fiscal Hierarchy].CurrentMember)}),NULL,
(parallelperiod([Date].[Fiscal Hierarchy].[fiscal year],1),[Measures].[Special Sales]))
)|||There seems to be some inconsistency in the sample data: Store 3, 20070101 is shown as a special sale in the joined fact table, but not in Special_Store_Fact - I assume that is just a typo?
Anyway, since SS_Flag is already a field in the joined fact table, another approach would to create a simple true/false dimension like [Special Flag]. Then the MDX expressions could be:
For current year [Special Sales]:
([Special Flag].[SL_Flag].[True], [Measures].[Sales])
And for previous year [Special Sales]:
Sum(NonEmpty([Store].[Store].[Store].Members,
([Special Flag].[SL_Flag].[True], [Measures].[Sales])),
(ParallelPeriod([Date].[Fiscal Hierarchy].[Fiscal Year], 1),
[Measures].[Sales]))
|||That's probably a reasonable compromise and would be relatively easy to implement.
The only comment I would make is that modelling changing attributes in this matter should be the exception, not the rule. You would not want to take this to the extreme and end up with a cube that has lots of small dimensions as it increases the size of the aggregations and indexes which will reduce performance.
|||
Hi,
It's been a quite long time since you posted this.
Now, we have some changes regarding this special store's calculation.
Previously, we only select the current special store list, and for the last year value, we use current lists of special stores and if any store which was not special store in parallel period then we excluded it from the calculation.
I've tried this MDX into the cube and current year value works perfectly, but last year value doesn't work properly.
MDX for the last year :
Sum(NonEmpty([Store].[Store Hierarchy].currentmember,
([Special Flag].[SL_Flag].[True], [Measures].[Sales])),
(ParallelPeriod([Date].[Fiscal Hierarchy].[Fiscal Year], 1),
[Measures].[Sales]))
> This mdx calculate all the last year value that store became a special in last year. it can't get the current special store lists.
But now, whether store was not a spcial store in last year, if store became a special store in selected date ( current) then we sum up all store's sale amount into the last year value.
below is the MDX for the last year:
([Special Flag].[SL_Flag].[True],
(ParallelPeriod([Date].[Fiscal Hierarchy].[Fiscal Year], 1),
[Measures].[Sales]))
But when i browse the cube, it seems last year special flag is used for last year calculation. So, I can see all special store sales amount in last year. (not exclude store which is not a special store in current year)
I've tried to do different ways but still couldn't get any solutions yet.
It seems the MDX is something wrong.
If I want to use current special flag for the parallelperiod calculation how should I write MDX for that?
I think this logic makes sense to me but I don't know why it doesn't work.
I would appreciate if anybody can give me some comments.
Thanks.
MDX question
Hi,
Here is my question.
For the last year special store sales amount, we only calculate sales amount based on special store lists in current selected date.
If a store in current date is not a special store then we do not include this store for last year sales amount (even if this store was special store in last year). This means we don't care about last year store status and we only identify the special store status of all stores according to the date we selected.
In the fact, I join the flag ( which represents a special store) and added one colum for special sales calcualtion. (flag * net sales). In the cube, Currnet speical sales are correct but I couldn't get correct last year specail sales amount.
Below is the calculation for last year specail store sales amount but the numbers do not match.
IIF(([Measures].[flag], [Date].[Fiscal Hierarchy].CurrentMember)=False,0,
(parallelperiod([Date].[Fiscal Hierarchy].[fiscal year],1),[Measures].[Special Sales]))
Please give me some comments.
Thanks in advance.
Boolean values as measures just don't work well. There probably would be a way to write the MDX calculation, but the performance would be really bad probably to the point of being unusable.
You could implement the flag as it's own dimension, but probably the best approach is to implement the store dimension as a slowly changing dimension. What you would be after is a type 2 changing dimension. If you are not aware of what this is, you are basically adding a new record to the store dimension when the value of the flag changes
Below is a simplified example, notice that "Store 1" has StoreID = 1 and StoreID = 3. It is often common to store effective dates against the records for slowly changing dimensions to make it easier to figure out which dimension record applies to a given fact record.
StoreID StoreName SpecialFlag
1 Store 1 False
2 Store 2 False
3 Store 1 True
Then when you insert facts you insert the StoreID that was effective for the date of the respective facts.
StoreID DateID SalesAmount
1 20060101 100
3 20070601 100
Using this technique you would be able to create a "Special Store" attribute in your store dimension and filter using that.
|||Thanks for the reply. I think I didn't give you a right explanation.
I understand what you suggested but could you take a look at this one more time?
Below is the details how it looks like :
Selected date : 20070101
Last year date : 20060101
Sales Fact
ID Store_key Date Sales
1 store 1 20070101 100.00
2 store 2 20070101 150.00
3 store 3 20070101 100.00
4 store 1 20060101 30.00
5 store 2 20060101 30.00
6 store 3 20060101 30.00
There is an anther fact table which has a flag.
Special_Store Fact
Special_Store_key Store_key Date Key SS_flag
1 store 1 20070101 True
2 store 1 20060101 False
3 store 2 20070101 False
4 store 2 20060101 True
5 store 3 20070101 False
6 store 3 20060101 True
Now I joined this 2 Fact to add Specail Store Sales into the Sales Fact
ID Store_key Date Sales SS_flag SS_Sales (SS flag * Sales)
1 store 1 20070101 100.00 True 100.00
2 store 2 20070101 150.00 False 0.00
3 store 3 20070101 100.00 True 100.00
4 store 1 20060101 30.00 False 0.00
5 store 2 20060101 30.00 True 30.00
6 store 3 20060101 30.00 True 30.00
From this data,
we calulate SS sales amount and the total is 130.00 which is not a problem to calculate in the cube.
My problem is that for the last year SS sales amount with time calculation we developed inside the cube,
time calculation sums up all SS_Sales, so last year total is 60.00.
But from the selected date , we do not consider last year store status. We only care current Special Store and From this list we calculate last year value. So, in this data, we need to get 30 (excluding store 2 since store 2 is not special store in the selected date 20070101).
As you mentioned, if boolean value does not work properly and affects performance then how can I approach in this situation? Any Suggestions?
Please let me know.
Thanks.
|||I am pretty sure that my original suggestion of not adding this flag to the fact table and implementing a slowly changing dimension would work. But possibly a simpler solution might be to insert NULL insead of 0.00 into the SS_Sales measure and then do something like the following, which basically checks if there is a value for [Special Sales] in the current period.
IIF(IsEmpty({([Measures].[Special Sales], [Date].[Fiscal Hierarchy].CurrentMember)}),NULL,
(parallelperiod([Date].[Fiscal Hierarchy].[fiscal year],1),[Measures].[Special Sales]))
The only problem with this is that, if you have a hierarchy over the stores so that they roll up into groups of some sort then you will get incorrect results at the higher levels. If any store in the group was "sepcial" last year the value for the entire group would be included
Which would mean that you might have to do something like the following to force this expression to be evaluated over individual store members.
SUM( EXISTING [Store].[Store].[Store].Members ,
IIF(IsEmpty({([Measures].[Special Sales], [Date].[Fiscal Hierarchy].CurrentMember)}),NULL,
(parallelperiod([Date].[Fiscal Hierarchy].[fiscal year],1),[Measures].[Special Sales]))
)|||There seems to be some inconsistency in the sample data: Store 3, 20070101 is shown as a special sale in the joined fact table, but not in Special_Store_Fact - I assume that is just a typo?
Anyway, since SS_Flag is already a field in the joined fact table, another approach would to create a simple true/false dimension like [Special Flag]. Then the MDX expressions could be:
For current year [Special Sales]:
([Special Flag].[SL_Flag].[True], [Measures].[Sales])
And for previous year [Special Sales]:
Sum(NonEmpty([Store].[Store].[Store].Members,
([Special Flag].[SL_Flag].[True], [Measures].[Sales])),
(ParallelPeriod([Date].[Fiscal Hierarchy].[Fiscal Year], 1),
[Measures].[Sales]))
|||That's probably a reasonable compromise and would be relatively easy to implement.
The only comment I would make is that modelling changing attributes in this matter should be the exception, not the rule. You would not want to take this to the extreme and end up with a cube that has lots of small dimensions as it increases the size of the aggregations and indexes which will reduce performance.
|||
Hi,
It's been a quite long time since you posted this.
Now, we have some changes regarding this special store's calculation.
Previously, we only select the current special store list, and for the last year value, we use current lists of special stores and if any store which was not special store in parallel period then we excluded it from the calculation.
I've tried this MDX into the cube and current year value works perfectly, but last year value doesn't work properly.
MDX for the last year :
Sum(NonEmpty([Store].[Store Hierarchy].currentmember,
([Special Flag].[SL_Flag].[True], [Measures].[Sales])),
(ParallelPeriod([Date].[Fiscal Hierarchy].[Fiscal Year], 1),
[Measures].[Sales]))
> This mdx calculate all the last year value that store became a special in last year. it can't get the current special store lists.
But now, whether store was not a spcial store in last year, if store became a special store in selected date ( current) then we sum up all store's sale amount into the last year value.
below is the MDX for the last year:
([Special Flag].[SL_Flag].[True],
(ParallelPeriod([Date].[Fiscal Hierarchy].[Fiscal Year], 1),
[Measures].[Sales]))
But when i browse the cube, it seems last year special flag is used for last year calculation. So, I can see all special store sales amount in last year. (not exclude store which is not a special store in current year)
I've tried to do different ways but still couldn't get any solutions yet.
It seems the MDX is something wrong.
If I want to use current special flag for the parallelperiod calculation how should I write MDX for that?
I think this logic makes sense to me but I don't know why it doesn't work.
I would appreciate if anybody can give me some comments.
Thanks.
MDX question
Hi,
Here is my question.
For the last year special store sales amount, we only calculate sales amount based on special store lists in current selected date.
If a store in current date is not a special store then we do not include this store for last year sales amount (even if this store was special store in last year). This means we don't care about last year store status and we only identify the special store status of all stores according to the date we selected.
In the fact, I join the flag ( which represents a special store) and added one colum for special sales calcualtion. (flag * net sales). In the cube, Currnet speical sales are correct but I couldn't get correct last year specail sales amount.
Below is the calculation for last year specail store sales amount but the numbers do not match.
IIF(([Measures].[flag], [Date].[Fiscal Hierarchy].CurrentMember)=False,0,
(parallelperiod([Date].[Fiscal Hierarchy].[fiscal year],1),[Measures].[Special Sales]))
Please give me some comments.
Thanks in advance.
Boolean values as measures just don't work well. There probably would be a way to write the MDX calculation, but the performance would be really bad probably to the point of being unusable.
You could implement the flag as it's own dimension, but probably the best approach is to implement the store dimension as a slowly changing dimension. What you would be after is a type 2 changing dimension. If you are not aware of what this is, you are basically adding a new record to the store dimension when the value of the flag changes
Below is a simplified example, notice that "Store 1" has StoreID = 1 and StoreID = 3. It is often common to store effective dates against the records for slowly changing dimensions to make it easier to figure out which dimension record applies to a given fact record.
StoreID StoreName SpecialFlag
1 Store 1 False
2 Store 2 False
3 Store 1 True
Then when you insert facts you insert the StoreID that was effective for the date of the respective facts.
StoreID DateID SalesAmount
1 20060101 100
3 20070601 100
Using this technique you would be able to create a "Special Store" attribute in your store dimension and filter using that.
|||Thanks for the reply. I think I didn't give you a right explanation.
I understand what you suggested but could you take a look at this one more time?
Below is the details how it looks like :
Selected date : 20070101
Last year date : 20060101
Sales Fact
ID Store_key Date Sales
1 store 1 20070101 100.00
2 store 2 20070101 150.00
3 store 3 20070101 100.00
4 store 1 20060101 30.00
5 store 2 20060101 30.00
6 store 3 20060101 30.00
There is an anther fact table which has a flag.
Special_Store Fact
Special_Store_key Store_key Date Key SS_flag
1 store 1 20070101 True
2 store 1 20060101 False
3 store 2 20070101 False
4 store 2 20060101 True
5 store 3 20070101 False
6 store 3 20060101 True
Now I joined this 2 Fact to add Specail Store Sales into the Sales Fact
ID Store_key Date Sales SS_flag SS_Sales (SS flag * Sales)
1 store 1 20070101 100.00 True 100.00
2 store 2 20070101 150.00 False 0.00
3 store 3 20070101 100.00 True 100.00
4 store 1 20060101 30.00 False 0.00
5 store 2 20060101 30.00 True 30.00
6 store 3 20060101 30.00 True 30.00
From this data,
we calulate SS sales amount and the total is 130.00 which is not a problem to calculate in the cube.
My problem is that for the last year SS sales amount with time calculation we developed inside the cube,
time calculation sums up all SS_Sales, so last year total is 60.00.
But from the selected date , we do not consider last year store status. We only care current Special Store and From this list we calculate last year value. So, in this data, we need to get 30 (excluding store 2 since store 2 is not special store in the selected date 20070101).
As you mentioned, if boolean value does not work properly and affects performance then how can I approach in this situation? Any Suggestions?
Please let me know.
Thanks.
|||I am pretty sure that my original suggestion of not adding this flag to the fact table and implementing a slowly changing dimension would work. But possibly a simpler solution might be to insert NULL insead of 0.00 into the SS_Sales measure and then do something like the following, which basically checks if there is a value for [Special Sales] in the current period.
IIF(IsEmpty({([Measures].[Special Sales], [Date].[Fiscal Hierarchy].CurrentMember)}),NULL,
(parallelperiod([Date].[Fiscal Hierarchy].[fiscal year],1),[Measures].[Special Sales]))
The only problem with this is that, if you have a hierarchy over the stores so that they roll up into groups of some sort then you will get incorrect results at the higher levels. If any store in the group was "sepcial" last year the value for the entire group would be included
Which would mean that you might have to do something like the following to force this expression to be evaluated over individual store members.
SUM( EXISTING [Store].[Store].[Store].Members ,
IIF(IsEmpty({([Measures].[Special Sales], [Date].[Fiscal Hierarchy].CurrentMember)}),NULL,
(parallelperiod([Date].[Fiscal Hierarchy].[fiscal year],1),[Measures].[Special Sales]))
)|||There seems to be some inconsistency in the sample data: Store 3, 20070101 is shown as a special sale in the joined fact table, but not in Special_Store_Fact - I assume that is just a typo?
Anyway, since SS_Flag is already a field in the joined fact table, another approach would to create a simple true/false dimension like [Special Flag]. Then the MDX expressions could be:
For current year [Special Sales]:
([Special Flag].[SL_Flag].[True], [Measures].[Sales])
And for previous year [Special Sales]:
Sum(NonEmpty([Store].[Store].[Store].Members,
([Special Flag].[SL_Flag].[True], [Measures].[Sales])),
(ParallelPeriod([Date].[Fiscal Hierarchy].[Fiscal Year], 1),
[Measures].[Sales]))
|||That's probably a reasonable compromise and would be relatively easy to implement.
The only comment I would make is that modelling changing attributes in this matter should be the exception, not the rule. You would not want to take this to the extreme and end up with a cube that has lots of small dimensions as it increases the size of the aggregations and indexes which will reduce performance.
|||
Hi,
It's been a quite long time since you posted this.
Now, we have some changes regarding this special store's calculation.
Previously, we only select the current special store list, and for the last year value, we use current lists of special stores and if any store which was not special store in parallel period then we excluded it from the calculation.
I've tried this MDX into the cube and current year value works perfectly, but last year value doesn't work properly.
MDX for the last year :
Sum(NonEmpty([Store].[Store Hierarchy].currentmember,
([Special Flag].[SL_Flag].[True], [Measures].[Sales])),
(ParallelPeriod([Date].[Fiscal Hierarchy].[Fiscal Year], 1),
[Measures].[Sales]))
> This mdx calculate all the last year value that store became a special in last year. it can't get the current special store lists.
But now, whether store was not a spcial store in last year, if store became a special store in selected date ( current) then we sum up all store's sale amount into the last year value.
below is the MDX for the last year:
([Special Flag].[SL_Flag].[True],
(ParallelPeriod([Date].[Fiscal Hierarchy].[Fiscal Year], 1),
[Measures].[Sales]))
But when i browse the cube, it seems last year special flag is used for last year calculation. So, I can see all special store sales amount in last year. (not exclude store which is not a special store in current year)
I've tried to do different ways but still couldn't get any solutions yet.
It seems the MDX is something wrong.
If I want to use current special flag for the parallelperiod calculation how should I write MDX for that?
I think this logic makes sense to me but I don't know why it doesn't work.
I would appreciate if anybody can give me some comments.
Thanks.
Wednesday, March 7, 2012
MDX query for SQL equivalent function IN
I need help in writing MDX query which will return the count of Sales for differnt SalesPerson. Something equivalent to SQL query
Select Sales_Count from Sales where SalesPersonId IN (6200, 4367, 87650, 2222, 18, 334, 9090).
There are hundred thousands of SalesPersons, so is there any way i can run the MDX Query for a batch of SalesPersonId s. One more problem is the SalesPersonId s are not consecutive..Please can anyone help me solve this problem.
How are your user's selecting which sales persons to return?
B.
|||
Just place your MDX strings for each ID you want to be returned in the ROWS() part of the MDX.
SELECT { [Measures].[SomeCount] } ON COLUMNS,
{ [Heir].[ID1], [Heir].[ID2], [Heir].[ID3], [Heir].[ID4] } ON ROWS
FROM [Cube]
Will return:
[Heir].[ID1] 1259
[Heir].[ID2] 51454
[Heir].[ID3] 65514
[Heir].[ID4] 124
|||That is one of many ways to handle this problem. It just depends on what exactly you are wanting to have returned and how you intend to assemble your list of Sales Reps.
B.
|||True, I just supplied the exact syntax for the SQL statement provided.|||If you have a (front-end) Sql client that doesn't expose value hierachies to the user, and hence rely on users typing in the literals. You may consider use the class of functions that convert strings to member/tuple/set; so it will not result into an invalid member error when your client translates sql to mdx.
select city, profit from sales where city in ('Redwood City', 'Fremont') group by city;
==>
select [Measures].[Profit] on columns,
StrToSet("{[Customers].[Redwood City], [Customers].[Fremont]}") on rows from sales
but if the user typed RedwoodCity instead or Redwood City is not a valid member, the query wouldn't fail, and would still return results for Fremont.
That may be why Brian was asking how the IN list is constructed.
MDX query for SQL equivalent function IN
I need help in writing MDX query which will return the count of Sales for differnt SalesPerson. Something equivalent to SQL query
Select Sales_Count from Sales where SalesPersonId IN (6200, 4367, 87650, 2222, 18, 334, 9090).
There are hundred thousands of SalesPersons, so is there any way i can run the MDX Query for a batch of SalesPersonId s. One more problem is the SalesPersonId s are not consecutive..Please can anyone help me solve this problem.
How are your user's selecting which sales persons to return?
B.
|||Just place your MDX strings for each ID you want to be returned in the ROWS() part of the MDX.
SELECT { [Measures].[SomeCount] } ON COLUMNS,
{ [Heir].[ID1], [Heir].[ID2], [Heir].[ID3], [Heir].[ID4] } ON ROWS
FROM [Cube]
Will return:
[Heir].[ID1] 1259
[Heir].[ID2] 51454
[Heir].[ID3] 65514
[Heir].[ID4] 124
|||That is one of many ways to handle this problem. It just depends on what exactly you are wanting to have returned and how you intend to assemble your list of Sales Reps.
B.
|||True, I just supplied the exact syntax for the SQL statement provided.|||If you have a (front-end) Sql client that doesn't expose value hierachies to the user, and hence rely on users typing in the literals. You may consider use the class of functions that convert strings to member/tuple/set; so it will not result into an invalid member error when your client translates sql to mdx.select city, profit from sales where city in ('Redwood City', 'Fremont') group by city;
==>
select [Measures].[Profit] on columns,
StrToSet("{[Customers].[Redwood City], [Customers].[Fremont]}") on rows from sales
but if the user typed RedwoodCity instead or Redwood City is not a valid member, the query wouldn't fail, and would still return results for Fremont.
That may be why Brian was asking how the IN list is constructed.
Monday, February 20, 2012
MDX Help - Should be easy
I want to have a calculation that rolls up monthly closed sales and displays the total month volume no matter what dimension levels have been added.
Example:
Month Store # Sales Total Monthly Sales
April 1 10,000 50,000
April 2 10,000 50,000
April 3 30,000 50,000
May 1 20,000 70,000
May 2 20,000 70,000
May 3 30,000 70,000
This could be across multiple dimensions (ie, I could add product type, business day number, etc) and I would still want the total monthly sales to be the sum of all sales for the month for the entire company.
I have tried several things, but I do not know enough about MDX to know where to look.
Thanks for your help!!
Bob
I think this is what you are looking for:
with member [Measures].[Total Month Sales] as '([Time].currentmember, [Store].[All Stores], [Measures].[Unit Sales])'
select
{[Measures].[Unit Sales],[Measures].[Total Month Sales]} on columns,
{nonemptycrossjoin({descendants([Time].[1997],[Time].[Month])},{[Store].[Store Name].members})} on rows
from Sales
The trick is to add all the 'All' members to [Measures].[Total Month Sales]. For instance, if you have a product dimension, it should be
'([Time].currentmember, [Store].[All Stores], [Product].[All Product], [Measures].[Unit Sales])'
Hope this helps,
Santi
|||AWESOME! Thanks for your quick answer!!
BobP