Showing posts with label sum. Show all posts
Showing posts with label sum. Show all posts

Monday, March 26, 2012

question on using median to aggregate a measure

Hey all,

I'm still learning SSAS + MDX and am not sure how to go about using the MEDIAN function (instead of sum or avg) to aggregate my measure.

In my dataset, I have Geography, Time, and 'Use Type' dimensions that splice a Sale Value measure. I'd like to construct a measure that uses the Median instead of the traditional avg or sum aggregations.

I read the MSDN page on the median function (http://msdn2.microsoft.com/en-us/ms145570.aspx), but I'm still lost on what to do in the SSAS designer to add this measure. Any suggestions?

Thanks!

I'm not aware of a native "median" aggregation in SSAS - you could use the Median() function at the measure group fact dimension granularity, as in this Adventure Works example; but performance may be poor:

>>

With Member [Measures].[Median Sales Amount] as

Median(Exists([Sales Summary Order Details].[Sales Orders].[Order Line],,

"Sales Summary"), [Measures].[Sales Amount]),

NON_EMPTY_BEHAVIOR = [Measures].[Sales Amount],

FORMAT_STRING = "Currency"

select {[Measures].[Order Quantity], [Measures].[Average Sales Amount],

[Measures].[Median Sales Amount]} on 0,

Non Empty [Product].[Category].Members on 1

from [Adventure Works]

where [Date].[Calendar].[Calendar Year].&[2002]

--

Order Quantity Average Sales Amount Median Sales Amount
All Products 60,918 $8,308.44 $809.33
Accessories 5,207 $260.49 $60.56
Bikes 24,908 $7,509.60 $2,181.56
Clothing 16,927 $754.02 $91.18
Components 13,876 $5,142.58 $418.51

>>

|||Hi Deepak,

Yeah - I figured out how to use the Median function when creating arbitrary MDX expressions, but why can't you create preprocessed cubes using an arbitrary aggregation method? When I splice and dice my cube, I wanted the numbers to be the pre-processed Median at the level I was at, instead of the sum or average. Is this really not possible?|||Does anyone know if there's a way to create a custom aggregator? In this case, using Median instead of SUM/Avg?|||This is an interesting idea, but not possible in Analysis Services 2005.|||Hmm - actually, it sounds like I might be able to get what I want via MDX Scripting. Does anyone know if this is possible? Essentialy, I want a measure where every cell is computed as the median of whatever dimensional intersection is specified.

question on using median to aggregate a measure

Hey all,

I'm still learning SSAS + MDX and am not sure how to go about using the MEDIAN function (instead of sum or avg) to aggregate my measure.

In my dataset, I have Geography, Time, and 'Use Type' dimensions that splice a Sale Value measure. I'd like to construct a measure that uses the Median instead of the traditional avg or sum aggregations.

I read the MSDN page on the median function (http://msdn2.microsoft.com/en-us/ms145570.aspx), but I'm still lost on what to do in the SSAS designer to add this measure. Any suggestions?

Thanks!

I'm not aware of a native "median" aggregation in SSAS - you could use the Median() function at the measure group fact dimension granularity, as in this Adventure Works example; but performance may be poor:

>>

With Member [Measures].[Median Sales Amount] as

Median(Exists([Sales Summary Order Details].[Sales Orders].[Order Line],,

"Sales Summary"), [Measures].[Sales Amount]),

NON_EMPTY_BEHAVIOR = [Measures].[Sales Amount],

FORMAT_STRING = "Currency"

select {[Measures].[Order Quantity], [Measures].[Average Sales Amount],

[Measures].[Median Sales Amount]} on 0,

Non Empty [Product].[Category].Members on 1

from [Adventure Works]

where [Date].[Calendar].[Calendar Year].&[2002]

--

Order Quantity Average Sales Amount Median Sales Amount
All Products 60,918 $8,308.44 $809.33
Accessories 5,207 $260.49 $60.56
Bikes 24,908 $7,509.60 $2,181.56
Clothing 16,927 $754.02 $91.18
Components 13,876 $5,142.58 $418.51

>>

|||Hi Deepak,

Yeah - I figured out how to use the Median function when creating arbitrary MDX expressions, but why can't you create preprocessed cubes using an arbitrary aggregation method? When I splice and dice my cube, I wanted the numbers to be the pre-processed Median at the level I was at, instead of the sum or average. Is this really not possible?|||Does anyone know if there's a way to create a custom aggregator? In this case, using Median instead of SUM/Avg?|||This is an interesting idea, but not possible in Analysis Services 2005.|||Hmm - actually, it sounds like I might be able to get what I want via MDX Scripting. Does anyone know if this is possible? Essentialy, I want a measure where every cell is computed as the median of whatever dimensional intersection is specified.

Wednesday, March 21, 2012

Question on SQL

Hello,
I am new to SQL Server. I am writing a stored procedure with a sql like this
:
SELECT
sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnA)) ,
sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnB)) ,
sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnC)) ,
sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnD)) ,
sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnE))
FROM TableX
WHERE
ID = '1234'
When I run the query, all the five column return same value and I am sure
the result is incorrect. Here is the function of fn_currency. It simply
convert foreign curreny amount to base currency amount. Is there anyone who
can tell me what's wrong with it'
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
ALTER FUNCTION fn_Currency
(
@.Item_Curr Currency_Code,
@.AC_Curr Currency_Code,
@.In_Val Amount
)RETURNS Amount AS
BEGIN
DECLARE
@.Out_Val Amount,
@.Ex_Rate Ex_Rate,
@.Cal_Method Cal_Method
SELECT @.Out_Val = ISNULL(@.In_Val, 0)
IF @.Item_Curr <> @.AC_Curr
BEGIN
SELECT @.Cal_Method = Cal_Method FROM ExRate_CalMethod WHERE From_Currency = @.Item_Curr AND To_Currency = @.AC_Curr
SET @.Cal_Method = ISNULL(@.Cal_Method, 'M')
SELECT @.Ex_Rate = Rate FROM Exchange_Rate WHERE From_Currency = @.Item_Curr
AND To_Currency = @.AC_Curr
SELECT @.Ex_Rate = ISNULL(@.Ex_Rate, 0)
IF @.Cal_Method = 'M'
SELECT @.Out_Val = ISNULL(@.In_Val * @.Ex_Rate, 0)
ELSE
SELECT @.Out_Val = ISNULL(@.In_Val / @.Ex_Rate, 0)
END
Return @.Out_Val
END
Many thanks!
KennethHi
Without DDL for the UDT and tables it is hard to replicate your environment.
See http://www.aspfaq.com/etiquette.asp?id=5006 on how to get this. You may
also want to post what version/service pack you are using.
Your procedure should return different values, have you tried it without the
SUMs?
John
"Kenneth" wrote:
> Hello,
> I am new to SQL Server. I am writing a stored procedure with a sql like this
> :
> SELECT
> sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnA)) ,
> sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnB)) ,
> sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnC)) ,
> sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnD)) ,
> sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnE))
> FROM TableX
> WHERE
> ID = '1234'
> When I run the query, all the five column return same value and I am sure
> the result is incorrect. Here is the function of fn_currency. It simply
> convert foreign curreny amount to base currency amount. Is there anyone who
> can tell me what's wrong with it'
> SET QUOTED_IDENTIFIER OFF
> GO
> SET ANSI_NULLS OFF
> GO
> ALTER FUNCTION fn_Currency
> (
> @.Item_Curr Currency_Code,
> @.AC_Curr Currency_Code,
> @.In_Val Amount
> )RETURNS Amount AS
> BEGIN
> DECLARE
> @.Out_Val Amount,
> @.Ex_Rate Ex_Rate,
> @.Cal_Method Cal_Method
> SELECT @.Out_Val = ISNULL(@.In_Val, 0)
> IF @.Item_Curr <> @.AC_Curr
> BEGIN
> SELECT @.Cal_Method = Cal_Method FROM ExRate_CalMethod WHERE From_Currency => @.Item_Curr AND To_Currency = @.AC_Curr
> SET @.Cal_Method = ISNULL(@.Cal_Method, 'M')
> SELECT @.Ex_Rate = Rate FROM Exchange_Rate WHERE From_Currency = @.Item_Curr
> AND To_Currency = @.AC_Curr
> SELECT @.Ex_Rate = ISNULL(@.Ex_Rate, 0)
> IF @.Cal_Method = 'M'
> SELECT @.Out_Val = ISNULL(@.In_Val * @.Ex_Rate, 0)
> ELSE
> SELECT @.Out_Val = ISNULL(@.In_Val / @.Ex_Rate, 0)
> END
> Return @.Out_Val
> END
> Many thanks!
> Kenneth
>|||On Tue, 4 Apr 2006 00:24:01 -0700, Kenneth wrote:
>Hello,
>I am new to SQL Server. I am writing a stored procedure with a sql like this
>:
> SELECT
> sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnA)) ,
> sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnB)) ,
> sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnC)) ,
> sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnD)) ,
> sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnE))
> FROM TableX
> WHERE
> ID = '1234'
>When I run the query, all the five column return same value and I am sure
>the result is incorrect. Here is the function of fn_currency. It simply
>convert foreign curreny amount to base currency amount. Is there anyone who
>can tell me what's wrong with it'
Hi Kenneth,
It looks like your server has not been upgraded with any service pack
yet. What you describe looks exactly like this problem, which was fixed
in service pack 1: http://support.microsoft.com/kb/288957/EN-US/
--
Hugo Kornelis, SQL Server MVP|||I am using SQL server service pack 2 currently. However, I can still simulate
the error mentioned in the knowledge base. I am now trying to upgrade to
service pack 4 and test again. Thank you very much!
"Hugo Kornelis" wrote:
> On Tue, 4 Apr 2006 00:24:01 -0700, Kenneth wrote:
> >Hello,
> >
> >I am new to SQL Server. I am writing a stored procedure with a sql like this
> >:
> >
> > SELECT
> > sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnA)) ,
> > sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnB)) ,
> > sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnC)) ,
> > sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnD)) ,
> > sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnE))
> > FROM TableX
> > WHERE
> > ID = '1234'
> >
> >When I run the query, all the five column return same value and I am sure
> >the result is incorrect. Here is the function of fn_currency. It simply
> >convert foreign curreny amount to base currency amount. Is there anyone who
> >can tell me what's wrong with it'
> Hi Kenneth,
> It looks like your server has not been upgraded with any service pack
> yet. What you describe looks exactly like this problem, which was fixed
> in service pack 1: http://support.microsoft.com/kb/288957/EN-US/
> --
> Hugo Kornelis, SQL Server MVP
>

Friday, March 9, 2012

Question on MDX

I am trying to write a calculated field. I need to calculate the Sum of a Item Sold over a period of time where 2 measures called BV value and Unit Price are zero. Can someone suggest what functions I should use to get this done

Thanks

Ann

Hello Ann,

Did you mean to use a calculated member? If yes, please, try the following:

WITH MEMBER [Measures].SumOfSoldItems AS 'sum ( filter ( [Date].[Fiscal Year].members, [Measures].[BV Value] = 0 AND [Measures].[Unit Price] = 0 ) , [Measures].[Item Sold] ) '
SELECT [Measures].SumOfSoldItems on 0
FROM [Adventure Works]

In the first argument of the filter function you should put a set representing the period of time that you are interested in.

I hope this helps

Greg

|||

Hi Greg

Thanks for your post. Thats exactly what I am trying to achieve. I tried using the above method, but it gives me a syntax error . It says, Syntax for "WITH" is incorrect.

Thanks

Ann

|||

Could you tell me what client application you are using? I wrote 2 more MDX queries for you so you can use them as a small tutorial. They both work with Adventure Works.

1. This one doesn't use calculated member but should give you the right results. After you try it, you can change the query to work with your own cube. Change the time dimension members ( [Date].[Fiscal Year].members ) to a set representing the time period you need. The two other measures in the filter you can change to [Measures].[BV value]=0 and [Measures].[Unit Price] = 0. The measure in the WHERE clause you may want to change to [Measures].[Item Sold]

SELECT filter ( [Date].[Fiscal Year].members, [Measures].[Amount] > 3500000 AND [Measures].[Order Quantity] > 90000 ) on 0
FROM [Adventure Works]
WHERE [Measures].[Sales Amount]

2. This is the query that I showed you yesterday but I changed it back slightly so now it works with Adventure Works. Try it first against AW and then change the time period and measures for your own needs as I explained in the first paragraph:

WITH MEMBER [Measures].x AS 'sum ( filter ( [Date].[Fiscal Year].members, [Measures].[Amount] > 0 AND [Measures].[Order Quantity] > 0 ) , [Measures].[Sales Amount] ) '
SELECT [Measures].x on 0
FROM [Adventure Works]


I hope this works for you. If it doesn't or you have more questions, feel free to ask :)

- Greg

Saturday, February 25, 2012

Question on cube write back

Hi, all experts here,

I encountered a problem with cube writing back which contains measures with count aggregation rather than sum which said write back can not be allowed on measure groups with other aggregations rather than sum? Is it saying we can only enable write back on cubes with measure groups all based on sum aggregations?

But in my case, the measures needed to be counted rather than sum. What can I try to enable write back on this cube?

I am looking forward to hearing from you for your advices and thank you very much in advance.

With best regards,

Yours sincerely,

I'm having trouble thinking of a situation where you would need to writeback to a count measure, but that is beside the point.

One work around might be to create a new column in your fact table for this measure and pre-populate it with a value of 1 during ETL. Then you would be able to use a sum aggregation and get the same value as you would with a count measure. It would then also be possible to write back to this measure.

|||

Hi,

In my data, the facts are bit data, which only with values of 1 and 0 meaning different things respectively. Therefore I need to count these different situations happened in the fact table across different levels. There are no numeric data types at the moment which are for sum.

Thanks for your advices and I am looking forward to hearing from any of you here more ideas in this situation. Maybe you would give me some better ideas on how to deal with it.

With best regards,

Yours sincerely,

|||

Helen,

Explain me better what is the goal, I didnt understood... Explain me as I a dummy!

:-)

Regards!

|||

HI, PedroCGD,

The facts in the facts tables are with 0 or 1 values only. (0 and 1 therefore represent different meanings), therefore I dont see any point to sum these facts in any cube. Instead, count aggregation makes more sense which can count the numbers for the fact being 0 or 1.

Hope my explanation is clear for your help.

With best regards,

Yours sincerely,

|||It sounds like you have some flags which should modelled as separate attributes, not as measures. You would have one count measure and set either a single dimension or a combined dimension (also known as a 'junk' dimension). In this way you should be able to get the count by any combination of attributes.|||

Hi, Darren,

Thanks for your kind advices.

As you kindly advised, I will then need to update the underlying relational database (as the underlying data warehouse is designed by others)?Since at the moment, the data residing in the data warehouse does not have any junk dimensions at all. All those flags attributes are residing in the fact tables.

I would need to create single dimension for each flag attribute or combine some of the flag attributes together into one single dimension? But only one count measure for all these dimension attributes?

Will it be any good to complete these in data source view of the cube? Thanks again. And hope you can give me more further advices on it.

With best regards,

Yours sincerely,

Monday, February 20, 2012

Question of SQL

Hello,
I am new to SQL Server. I am writing a stored procedure with a sql like this
:
SELECT
sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnA)) ,
sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnB)) ,
sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnC)) ,
sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnD)) ,
sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnE))
FROM TableX
WHERE
ID = '1234'
When I run the query, all the five column return same value and I am sure
the result is incorrect. Is there anyone who can tell me what's wrong with
it'
Many thanks!
KennethKenneth wrote:
> Hello,
> I am new to SQL Server. I am writing a stored procedure with a sql like this
> :
> SELECT
> sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnA)) ,
> sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnB)) ,
> sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnC)) ,
> sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnD)) ,
> sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnE))
> FROM TableX
> WHERE
> ID = '1234'
> When I run the query, all the five column return same value and I am sure
> the result is incorrect. Is there anyone who can tell me what's wrong with
> it'
> Many thanks!
> Kenneth
Please post enough code to reproduce the problem and show us what is
wrong with it. Without even seeing what fn_currency does it's hard to
help you.
--
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--|||Here is the function of fn_currency. It simply convert foreign curreny amount
to base currency amount.
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
ALTER FUNCTION fn_Currency
(
@.Item_Curr Currency_Code,
@.AC_Curr Currency_Code,
@.In_Val Amount
)RETURNS Amount AS
BEGIN
DECLARE
@.Out_Val Amount,
@.Ex_Rate Ex_Rate,
@.Cal_Method Cal_Method
SELECT @.Out_Val = ISNULL(@.In_Val, 0)
IF @.Item_Curr <> @.AC_Curr
BEGIN
SELECT @.Cal_Method = Cal_Method FROM ExRate_CalMethod WHERE From_Currency =@.Item_Curr AND To_Currency = @.AC_Curr
SET @.Cal_Method = ISNULL(@.Cal_Method, 'M')
SELECT @.Ex_Rate = Rate FROM Exchange_Rate WHERE From_Currency = @.Item_Curr
AND To_Currency = @.AC_Curr
SELECT @.Ex_Rate = ISNULL(@.Ex_Rate, 0)
IF @.Cal_Method = 'M'
SELECT @.Out_Val = ISNULL(@.In_Val * @.Ex_Rate, 0)
ELSE
SELECT @.Out_Val = ISNULL(@.In_Val / @.Ex_Rate, 0)
END
Return @.Out_Val
END
"David Portas" wrote:
> Kenneth wrote:
> > Hello,
> >
> > I am new to SQL Server. I am writing a stored procedure with a sql like this
> > :
> >
> > SELECT
> > sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnA)) ,
> > sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnB)) ,
> > sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnC)) ,
> > sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnD)) ,
> > sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnE))
> > FROM TableX
> > WHERE
> > ID = '1234'
> >
> > When I run the query, all the five column return same value and I am sure
> > the result is incorrect. Is there anyone who can tell me what's wrong with
> > it'
> >
> > Many thanks!
> > Kenneth
> Please post enough code to reproduce the problem and show us what is
> wrong with it. Without even seeing what fn_currency does it's hard to
> help you.
> --
> David Portas, SQL Server MVP
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
> SQL Server Books Online:
> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
> --
>|||This will not solve your former problem, but IMHO this is not a good
approach.
You should use a simple joined table and avoid the choice of the method at
the execution (multiply or divide) in the function.
Have a column with a coef and always multiply. This coef can be a computed
column or a column maintained by a trigger. ;-)
This could look like this :
select t1.amount, t1.amount * er.rate as converted_amount
from table t1
inner join exrate er
on t1.currency = er.source_currency
and er.destination_currency = 'USD'
This is a *much* faster.
"Kenneth" <Kenneth@.discussions.microsoft.com> a écrit dans le message de
news: B8BD30D8-1100-45A7-8615-8EDFCB7A494E@.microsoft.com...
> Here is the function of fn_currency. It simply convert foreign curreny
> amount
> to base currency amount.
> SET QUOTED_IDENTIFIER OFF
> GO
> SET ANSI_NULLS OFF
> GO
> ALTER FUNCTION fn_Currency
> (
> @.Item_Curr Currency_Code,
> @.AC_Curr Currency_Code,
> @.In_Val Amount
> )RETURNS Amount AS
> BEGIN
> DECLARE
> @.Out_Val Amount,
> @.Ex_Rate Ex_Rate,
> @.Cal_Method Cal_Method
> SELECT @.Out_Val = ISNULL(@.In_Val, 0)
> IF @.Item_Curr <> @.AC_Curr
> BEGIN
> SELECT @.Cal_Method = Cal_Method FROM ExRate_CalMethod WHERE From_Currency
> => @.Item_Curr AND To_Currency = @.AC_Curr
> SET @.Cal_Method = ISNULL(@.Cal_Method, 'M')
> SELECT @.Ex_Rate = Rate FROM Exchange_Rate WHERE From_Currency = @.Item_Curr
> AND To_Currency = @.AC_Curr
> SELECT @.Ex_Rate = ISNULL(@.Ex_Rate, 0)
> IF @.Cal_Method = 'M'
> SELECT @.Out_Val = ISNULL(@.In_Val * @.Ex_Rate, 0)
> ELSE
> SELECT @.Out_Val = ISNULL(@.In_Val / @.Ex_Rate, 0)
> END
> Return @.Out_Val
> END
> "David Portas" wrote:
>> Kenneth wrote:
>> > Hello,
>> >
>> > I am new to SQL Server. I am writing a stored procedure with a sql like
>> > this
>> > :
>> >
>> > SELECT
>> > sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnA)) ,
>> > sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnB)) ,
>> > sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnC)) ,
>> > sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnD)) ,
>> > sum(dbo.fn_Currency(Currency_Code, 'USD', ColumnE))
>> > FROM TableX
>> > WHERE
>> > ID = '1234'
>> >
>> > When I run the query, all the five column return same value and I am
>> > sure
>> > the result is incorrect. Is there anyone who can tell me what's wrong
>> > with
>> > it'
>> >
>> > Many thanks!
>> > Kenneth
>> Please post enough code to reproduce the problem and show us what is
>> wrong with it. Without even seeing what fn_currency does it's hard to
>> help you.
>> --
>> David Portas, SQL Server MVP
>> Whenever possible please post enough code to reproduce your problem.
>> Including CREATE TABLE and INSERT statements usually helps.
>> State what version of SQL Server you are using and specify the content
>> of any error messages.
>> SQL Server Books Online:
>> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
>> --
>>