Showing posts with label functions. Show all posts
Showing posts with label functions. Show all posts

Friday, March 23, 2012

median function

I have a long term need for a median function, I was wondering if anyone has or knows of some code or user defined functions somewhere that would do this. Ideally you could use it just liket the rest of the aggregate functions like AVG, etc.Hi Bill

You would need some sort of CLR code to use it like AVG etc.

The SQL in the link below works (I translated it into Access SQL recently) however you will need to make minor modifications to account for nulls, not use the financial median etc.
http://www.oreilly.com/catalog/transqlcook/chapter/ch08.html

HTHsql

Monday, March 12, 2012

MDX Sumproduct and excel functions

Hi
Has anyone used SUMPRODUCT in a calculated member in analysis services?
Apparantly, according to MS documentation, its possible, but I can see
nothing in the books online - or on the web.
I am using the syntax below, which I would have thought would have worked.
SUMPRODUCT({[Accounts].&[123],[Accounts].&[456]},{
[Accounts].&[111],[Accounts].&[321]})
So does anyone know how to use SUMPRODUCT with AS?
Thank you for your help
JeremySince the arguments to SumProduct() are arrays, use the MDX SetToArray()
function to generate them. The second argument to SetToArray() is the
numerical value to use:
[vbcol=seagreen]
SUMPRODUCT(
SetToArray({[Accounts].&[123],[Accounts].&[456]},
[Measures].[Sales]),
SetToArray({[Accounts].&[111],[Accounts].&[321]},
[Measures].[Sales]))[vbcol=seagreen]
From SQL Server BOL>>
SetToArray
Converts one or more sets to an array for use in a user-defined
function.
Syntax
SetToArray(Set[, Set...][, Numeric Expression])
Remarks
This function converts one or more sets to an array for use in a
user-defined function. The number of dimensions in the resulting array
is the same as the number of sets specified.
The optional numeric expression can be used to provide the values in the
array cells. If omitted, the default value of the set member is used for
the array cell value.
..[vbcol=seagreen]
- Deepak
Deepak Puri
Microsoft MVP - SQL Server
*** Sent via Developersdex http://www.codecomments.com ***
Don't just participate in USENET...get rewarded for it!

Saturday, February 25, 2012

mdx query

I write the following MDX query that functions correctly:

WITH MEMBER [PERIOD CONTAB].[FY MONTH].[Totale] as

'AGGREGATE ( { [PERIOD CONTAB].[FY MONTH].&[2007-01-01T00:00:00] : [PERIOD CONTAB].[FY MONTH].&[2007-09-01T00:00:00] } )'

SELECT NON EMPTY { [Measures].[EUR] } ON COLUMNS ,

{

[Operating Cost]

,[COST ELEMENT].[MACRO COST].[Total Operating Cost]

,[COST ELEMENT].[MACRO COST].&[Capital Charge]

,[COST ELEMENT].[MACRO COST].[Total Contract Cost]

}

*

UNION (

{[PERIOD CONTAB].[FY MONTH].&[2007-01-01T00:00:00] :[PERIOD CONTAB].[FY MONTH].&[2007-09-01T00:00:00] }

, [PERIOD CONTAB].[FY MONTH].[Totale])

DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS

from [PMO_MDB]

If I try to insert the query inside Reporting Services substituting some data with query parameter in the following way I get an error:

WITH MEMBER [PERIOD CONTAB].[FY MONTH].[Totale] as

'AGGREGATE ( { STRTOMEMBER(@.FY_MONTH_START) : STRTOMEMBER(@.FY_MONTH_END) } )'

SELECT NON EMPTY { [Measures].[EUR] } ON COLUMNS ,

{

[Operating Cost]

,[COST ELEMENT].[MACRO COST].[Total Operating Cost]

,[COST ELEMENT].[MACRO COST].&[Capital Charge]

,[COST ELEMENT].[MACRO COST].[Total Contract Cost]

}

*

UNION (

{ STRTOMEMBER(@.FY_MONTH_START) : STRTOMEMBER(@.FY_MONTH_END) }

, [PERIOD CONTAB].[FY MONTH].[Totale])

DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS

FROM ( SELECT ( STRTOSET(@.PROJECTSTREAM, CONSTRAINED) ) ON COLUMNS FROM [PMO_MDB])

WHERE ( IIF( STRTOSET(@.PROJECTSTREAM, CONSTRAINED).Count = 1, STRTOSET(@.PROJECTSTREAM, CONSTRAINED), [PROJECT].[STREAM].currentmember ) )

CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS

The query seems sintaticcally correct !

So what happened !

Here is the error:

TITLE: Microsoft Visual Studio

Query preparation failed.


ADDITIONAL INFORMATION:

Parser: The FY_MONTH_START parameter could not be resolved because it was referenced in an inner subexpression. (msmgdsrv)


BUTTONS:

OK

Having the same problem in reporting services. Have you found anything?|||

Sounds like it's having a problem with the STRTOMEMBER function inside of the custom member definition. Since you know that your member will only be a single value, you could always write the query in dynamic mdx and pass the parameter values in. The top would look something like this:

Code Snippet

=" WITH MEMBER [PERIOD CONTAB].[FY MONTH].[Totale] as

'AGGREGATE ( { " + Parameters!FY_MONTH_START.Value + " : " + Parameters!FY_MONTH_END.Value + ") } )'

SELECT NON EMPTY { [Measures].[EUR] } ON COLUMNS , " +

It's a little tricky to get the designer to accept dynamic mdx. You have to write the original mdx query first. Then you have to click on the elipses for the dataset to bring up the dataset dialog. Then in the dialog, click the "f(x)" button to fill in the text. The dialog box that comes up will allow you to enter in the "=" sign and do the rest of your dynamic mdx.

|||

Thanks for the reply

I solved simply removing the character ' in the member definition !

Don't know why .... but now it functions -)

WITH MEMBER [PERIOD CONTAB].[FY MONTH].[Totale] as AGGREGATE ( { " + Parameters!FY_MONTH_START.Value + " : " + Parameters!FY_MONTH_END.Value + ") } )

.....

Cosimo

Monday, February 20, 2012

MDX issue with SUM function and + operator


Hi,

Facing a problem with the sum and aggregate functions when using the + operator
The simplified representation of the problem is as below

The following query returns null.

[Measures].[xyz] is a base measure, no mdx calculation

with member TestValue as
'[Measures].[xyz] + [Measures].[xyz]'

member SummedValue as
'sum (
{
DimensionA.Level2.Member1,
DimensionA.Level2.Member2,
DimensionA.Level2.Member3,
DimensionA.Level2.Member4
}
,TestValue
)

select
SummedValue on columns
from <cube>

The above query returns a valid integer value

when TestValue is just '[Measures].[xyz]' and not the sum(+) or subtraction(-),
though it works when it is multiplication or division (* or /).

Is there anything wrong in the usage or is there a workaround.

Regards

I ran the following query against the AdventureWorks cube, and it delivered the desired results.

Code Snippet

with member TestValue as

'[Measures].[Internet Order Count]+[Measures].[Internet Order Count]'

member SummedValue as

'sum (

{

[Customer].[Customer Geography].[State-Province].&[FL]&[US],

[Customer].[Customer Geography].[State-Province].&[AL]&[US],

[Customer].[Customer Geography].[State-Province].&[GA]&[US],

[Customer].[Customer Geography].[State-Province].&[MI]&[US]

}

,TestValue

)'

select

SummedValue on columns

from [Adventure Works]

Can you give some details on how your cube differs from this?

|||

we have this problem only in a particular environment.

Is this dependent on Service packs of SQL Server or 32/64 bit versions of SQL Server.

Regards

|||I tested in an XP SP2 32-bit / SQL Server 2005 SP2 environment. I can try a few others tomorrow. It would be helpful if you let the forum know what type of environment you are running in.|||

This might be a bit of a long shot, but try changing your expression to make it a bit more explicit. The '+' operator is overloaded to also act as a union operator, because you are passing in two members, maybe SSAS is choosing to make a set of two members rather than doing a numeric addition.

Try changing your calc expression from

with member TestValue as
'[Measures].[xyz] + [Measures].[xyz]'

To:

with member TestValue as ([Measures].[xyz]) + ([Measures].[xyz])

Note that the single quotes around inline calculated members are no longer required in SSAS 2005 and in fact it is better not to include them as SSAS can then report the location of any errors with more accuracy. (plus you get better syntax coloring support in SSMS)

|||

thanks for the quick replies...

this does not work in a 64-bit SQL Server SP1 ......SSAS version 9.00.2176.00 environment

but works in 32-bit SQL Server SP2......SSAS version 9.00.3042.00

this lists the fixes in SQL Server 2005 SP2

http://support.microsoft.com/default.aspx/kb/921896

is it linked to this fix

919957 (http://support.microsoft.com/kb/919957/) FIX: Some cells return the NULL value instead of returning the actual value when you query a dimension that contains a parent/child hierarchy in a SQL Server 2005 Analysis Services cube

Regards