Showing posts with label distinct. Show all posts
Showing posts with label distinct. Show all posts

Wednesday, March 21, 2012

Measure with "Distinct Count" Aggregate Function and Null value

I Have a cube with many measures. One of them use as "Aggregate
Function" the function "Distinct Count". But some records of fact table
has null value in the field of this measure. In SQL Server, when I
execute a "SELECT COUNT (DISTINCT field)" command, it eliminates the
Null value, as showed in the next exemple:
Table X
field A
1122
2345
4567
1122
1122
2345
null
null
When a execute the command:
SELECT COUNT (DISTINCT A) FROM X
I recieve "3" as result.
But when I create a measure over the field A in a cube and I choose the
aggregate funtion "Distinct Count", I recieve "4" as result. I think
that Analysis Services is considering "null" as one of distinct values
of the field A, but I dont want that, because it makes no sense for me.
Is there a way to eliminate the null value in Analysis Services, to show
"3" as the result in the same way I receive in SQL Server?
Thanks,
Paulo
*** Sent via Developersdex http://www.codecomments.com ***
I am new to SQL server, but experieced in Oracle.
My top of mind test would be:
select count(distinct *) from x => 4
select count(distinct a) from x => 3
It's standard SQL behaviour, I think.
pls verify, tks
|||When a execute the command "select count(distinct *) from x" , I recieve
"4" as result and when a execute the command "select count(distinct a)
from x", I recieve "3" as result.
But my problem is not with SQL Server. My problem is in Analysis
Services. I created a measure over the field "a", with the aggregate
function "Distinct Count". But the result in Analysis Services is "4"
and not "3", as I expected.
Do you have a clue?
Thanks,
Paulo
*** Sent via Developersdex http://www.codecomments.com ***
|||try to create a new cube (copy/paste the current one)
then create a view on the database like:
select * from mytable where mycolumntodcount IS NOT NULL
use this view in the new cube.
this cube will contain only the dcount measure and the null values are
ignored.
"Paulo Andre Ortega Ribeiro" <paulo.andre.66@.terra.com.br> wrote in message
news:ehCLkEZAGHA.2272@.TK2MSFTNGP11.phx.gbl...
> When a execute the command "select count(distinct *) from x" , I recieve
> "4" as result and when a execute the command "select count(distinct a)
> from x", I recieve "3" as result.
> But my problem is not with SQL Server. My problem is in Analysis
> Services. I created a measure over the field "a", with the aggregate
> function "Distinct Count". But the result in Analysis Services is "4"
> and not "3", as I expected.
> Do you have a clue?
> Thanks,
> Paulo
>
> *** Sent via Developersdex http://www.codecomments.com ***
|||I tried this solution and It works. But I dont want to create a new
cube. That will be my last option. Is not possible to ignore the null
vules with "distinct count" as the aggregate function? Is not there a
propriety that I can configure?
Thanks,
Paulo
*** Sent via Developersdex http://www.codecomments.com ***
|||the only other option I can propose is:
add a dimension in the cube, add a column in the fact table called "ToCount"
which contains a Y/N or 0/1 value linked to this new dimension.
case when MyColumn is null then 'N' else 'Y' end as ToCount
hide the dimension.
Rename the dcount measure to HiddenDcount, hide this measure
create a calculated measure which is:
(measures.HiddenDCount, MyDummyDimension.&[Y])
this ignore the null values.
"Paulo Andre Ortega Ribeiro" <paulo.andre.66@.terra.com.br> wrote in message
news:u9ciH0kAGHA.2040@.TK2MSFTNGP14.phx.gbl...
> I tried this solution and It works. But I dont want to create a new
> cube. That will be my last option. Is not possible to ignore the null
> vules with "distinct count" as the aggregate function? Is not there a
> propriety that I can configure?
> Thanks,
> Paulo
>
> *** Sent via Developersdex http://www.codecomments.com ***

Measure with "Distinct Count" Aggregate Function and Null value

I Have a cube with many measures. One of them use as "Aggregate
Function" the function "Distinct Count". But some records of fact table
has null value in the field of this measure. In SQL Server, when I
execute a "SELECT COUNT (DISTINCT field)" command, it eliminates the
Null value, as showed in the next exemple:
Table X
field A
1122
2345
4567
1122
1122
2345
null
null
When a execute the command:
SELECT COUNT (DISTINCT A) FROM X
I recieve "3" as result.
But when I create a measure over the field A in a cube and I choose the
aggregate funtion "Distinct Count", I recieve "4" as result. I think
that Analysis Services is considering "null" as one of distinct values
of the field A, but I dont want that, because it makes no sense for me.
Is there a way to eliminate the null value in Analysis Services, to show
"3" as the result in the same way I receive in SQL Server?
Thanks,
Paulo
*** Sent via Developersdex http://www.codecomments.com ***I am new to SQL server, but experieced in Oracle.
My top of mind test would be:
select count(distinct *) from x => 4
select count(distinct a) from x => 3
It's standard SQL behaviour, I think.
pls verify, tks|||When a execute the command "select count(distinct *) from x" , I recieve
"4" as result and when a execute the command "select count(distinct a)
from x", I recieve "3" as result.
But my problem is not with SQL Server. My problem is in Analysis
Services. I created a measure over the field "a", with the aggregate
function "Distinct Count". But the result in Analysis Services is "4"
and not "3", as I expected.
Do you have a clue?
Thanks,
Paulo
*** Sent via Developersdex http://www.codecomments.com ***|||try to create a new cube (copy/paste the current one)
then create a view on the database like:
select * from mytable where mycolumntodcount IS NOT NULL
use this view in the new cube.
this cube will contain only the dcount measure and the null values are
ignored.
"Paulo Andre Ortega Ribeiro" <paulo.andre.66@.terra.com.br> wrote in message
news:ehCLkEZAGHA.2272@.TK2MSFTNGP11.phx.gbl...
> When a execute the command "select count(distinct *) from x" , I recieve
> "4" as result and when a execute the command "select count(distinct a)
> from x", I recieve "3" as result.
> But my problem is not with SQL Server. My problem is in Analysis
> Services. I created a measure over the field "a", with the aggregate
> function "Distinct Count". But the result in Analysis Services is "4"
> and not "3", as I expected.
> Do you have a clue?
> Thanks,
> Paulo
>
> *** Sent via Developersdex http://www.codecomments.com ***|||I tried this solution and It works. But I dont want to create a new
cube. That will be my last option. Is not possible to ignore the null
vules with "distinct count" as the aggregate function? Is not there a
propriety that I can configure?
Thanks,
Paulo
*** Sent via Developersdex http://www.codecomments.com ***|||the only other option I can propose is:
add a dimension in the cube, add a column in the fact table called "ToCount"
which contains a Y/N or 0/1 value linked to this new dimension.
case when MyColumn is null then 'N' else 'Y' end as ToCount
hide the dimension.
Rename the dcount measure to HiddenDcount, hide this measure
create a calculated measure which is:
(measures.HiddenDCount, MyDummyDimension.&[Y])
this ignore the null values.
"Paulo Andre Ortega Ribeiro" <paulo.andre.66@.terra.com.br> wrote in message
news:u9ciH0kAGHA.2040@.TK2MSFTNGP14.phx.gbl...
> I tried this solution and It works. But I dont want to create a new
> cube. That will be my last option. Is not possible to ignore the null
> vules with "distinct count" as the aggregate function? Is not there a
> propriety that I can configure?
> Thanks,
> Paulo
>
> *** Sent via Developersdex http://www.codecomments.com ***

Monday, March 19, 2012

measure counting distinct values from a field

How do I count the amount of distinct values from a column in a fact table as a calculated measure

Assume the primary key of my fact table is a composite of three columns (a,b,c).

How can I code a calculated measure to count every distinct value of column a, not the amount of rows in my fact table.

Any advise help would be appreciated!

All you need to do is change the measure type from Count to Distinct Count ... make sure you select Column A as your key column....

Monday, March 12, 2012

MDX Syntax equivalent of SQL (NOT IN) Clause

I have a cube that I'm trying to separate into 2 distinct populations. After filtering on several combinations of dimensions, I get a set of unique people. I have the MDX syntax for this and it works well. However, I then want to turn around and get all of the other people not selected in the first group.

I can't merely take the "not" dimensions of the first group because I have both one-to-one and one-to-many dimensions and doing that will double count a lot of them. Oh yea, I've got a distinct count member hanging around that is needed.

In sql, its very rudimentry but using the "where x not in (select y from tablename .... )" will get the all the others not in the first group.

Any suggestions on getting the equivalent statement in MDX built?

Thanks! jmac

Hi,

I think you should be able to accomplish what you're trying to do using the 'Except' function. Google has lot's of examples of it..

C
|||I'll take a look. Not sure how to get both results into two sets for comparison.|||If you have some crossjoining to get to the set of members you are interested in, you might also need to use the Extract() function in combination with Except() to get just the members from one attribute.

MDX Syntax equivalent of SQL (NOT IN) Clause

I have a cube that I'm trying to separate into 2 distinct populations. After filtering on several combinations of dimensions, I get a set of unique people. I have the MDX syntax for this and it works well. However, I then want to turn around and get all of the other people not selected in the first group.

I can't merely take the "not" dimensions of the first group because I have both one-to-one and one-to-many dimensions and doing that will double count a lot of them. Oh yea, I've got a distinct count member hanging around that is needed.

In sql, its very rudimentry but using the "where x not in (select y from tablename .... )" will get the all the others not in the first group.

Any suggestions on getting the equivalent statement in MDX built?

Thanks! jmac

Hi,

I think you should be able to accomplish what you're trying to do using the 'Except' function. Google has lot's of examples of it..

C
|||I'll take a look. Not sure how to get both results into two sets for comparison.|||If you have some crossjoining to get to the set of members you are interested in, you might also need to use the Extract() function in combination with Except() to get just the members from one attribute.

MDX Sum distinct across specific dimensions

I have a measure group that has a few measures that are not addittive across all dimensions. I need to be able to get the sum distinct across specific dimensions and am having problems getting it to work. I tried specifying that the aggregate function should be semiaddittive, but it complains that it is missing a time dimension. I have tried many variations and combinations of MDX functions including sum, aggregate, crossjoin, filter, distinct, etc. but am struggling with the right combination.

Here is a simplified example of what I am trying to do:

GroupID Title Role ConditionCount

11682 Director Operations Licensing Manager 2
11683 Director Operations Licensing Manager 3
11683 Director Operations Product Manager 3

When I group on role I need to see this:

Licensing Manager 5

Production Manager 3

When I group on Title, I need to see this:

Director Operations 5

Basically I want to do a sum distinct for the GroupID. I only want to add the ConditionCount once for each distinct GroupID since the value will be the same for all instances of an individual groupID. Is there a way to do this in MDX?

Thanks in advance for your suggestions!

Clayton

Could you create another fact table and measure group for ConditionCount? For example, if the original fact table has the fields above, a named query could be created like:

select GroupID, Avg(ConditionCount) as ConditionCount from OriginalFact group by GroupID

Then, a "sum" [Measure].[ConditionCount] measure created on this measure group, which only relates to the Group dimension, would work as above.

|||I don't quite understand your suggestion. I do have a table where GroupID is the primary key, but I can't figure out how to get it to count values once for each distinct iteration of Group ID. The rollups and sum distincts across specific dimensions works with Oracle analytic functions, but I can't figure out how to get it to work with MDX. Thanks for your response.|||

Here's a more detailed description of the suggested model, which uses many-many dimensions:

Suppose this is the "TitleRole" fact table and measure group, with dimensions Group, Title and Role:

GroupID Title Role ConditionCount

11682 Director Operations Licensing Manager 2
11683 Director Operations Licensing Manager 3
11683 Director Operations Product Manager 3

There is a 2nd "Group" fact table and measure group (as suggested above), with [ConditionCount] "sum" measure - this could be the Group dimension table:

GroupID ConditionCount

11682 2
11683 3

This measure group relates directly to the "Group" dimension, but has a many-many relation with the Title and Role dimensions, via the intermediate "TitleRole" measure group. Now, if the Title: "Director Operations" is selected, [Measures].[ConditionCount] in the "Group" measure group should be 5. And if the Role: "Product Manager" is selected, [Measures].[ConditionCount] in the "Group" measure group should be 3.

|||Ah. I understand now. Let me give that I try when I get back to it in a few days and I'll respond. I already have that table set up in the data warehouse, but didn't think about trying a many-to-many relationship. Thanks again!

Wednesday, March 7, 2012

MDX query to aggregate distinct count

Hi there

I'm after some help with an mdx query, I'm using SQL 2005 analysis services and I want to create a query which compares the most recent three months with three months 1 month prior. The measure i want to compare is a distinct count so can't use sum to combine. i attempted to use (for the current 3 months);

aggregate([Dim Date].[Calendar Year].CurrentMember.Lag(3) :

[Dim Date].[Calendar Year].CurrentMember,

[Measures].[Active Player Count])

but got an error that I can't use aggregate on a calculated member, the measure Active Player Count is a standard measure that has distinct count measure as it's aggregation.

Hi,

It's not clear from the MDX fragment here what exactly you're trying to do, but here's an example using Adventure Works which does something similar:

WITH MEMBER MEASURES.LASTTHREEMONTHS AS

AGGREGATE([Date].[Calendar].CURRENTMEMBER : [Date].[Calendar].CURRENTMEMBER.LAG(2),[Measures].[Customer Count])

MEMBER MEASURES.LASTTHREEMONTHSONEPRIOR AS

AGGREGATE([Date].[Calendar].CURRENTMEMBER.PREVMEMBER : [Date].[Calendar].CURRENTMEMBER.LAG(3),[Measures].[Customer Count])

SELECT {[Measures].[Customer Count], MEASURES.LASTTHREEMONTHS,MEASURES.LASTTHREEMONTHSONEPRIOR} ON 0,

[Date].[Calendar].[Calendar Quarter].&[2003]&[4].CHILDREN

ON 1

FROM

[Adventure Works]

HTH,

Chris

|||

thanks Chris

Looks like I had the same query but had the lag around the wrong way , I had the lag (3) in the first part of the equation rather than the second.

Derek

Monday, February 20, 2012

MDX help

Hi All,

I have a calculated measure in my OLAP cube which is a distinct count of one of the keys in the Fact table. This is a semi-additive measure so I need Distinct count of the last child of this measure. How can I do this? Is there a built-in function to handle this.

I am using AS 2005.

Any help would be greatly appreciated.

Rohit

Hi Rohit. More detail here is helpful. You ask for LastChild of a measure. Unfortunately, measures don't have a hierarchy so they don't have children. This means you're probably asking for one of the following:

(1) The LastChild within a hierarchy (such as Product), and the corresponding value of your semi-additive measure (what's the name of the measure?)

OR

(2) The 'closing' value of your semi-additive measure across time.

Are you asking for one of the above options, or something else. An example would be very useful.

PGoldy

|||

Paul,

The calculated measure is MarketCount (distinct count of MarketKey). I am looking for the closing value of the measure across time.

The MarketCount is a monthly snapshot and should not be added across time.

For example, if the MarketCount is 110 in July and 120 in Aug, when the user browses the data in the cube for the months of July and August, the MarketCount should display as 120 and not as 230.

Thanks.

Rohit

|||

SSAS 2005 does have built-in support for semi-additive measures like last child, but only in the Enterprise Edition

http://msdn2.microsoft.com/en-us/library/ms189734.aspx

If you have the standard edition you could achieve the same results through calculations in the MDX script (although the performance would not be as good as the built-in feature)

|||

I am aware of the built in function for Last Child but I need to find the Distinct Count of the Last Child of one of the Fact keys. Please refer to my previous post for further details.

Paul, any ideas?

Rohit

|||

Hi Rohit:

I'm operating on a couple of assumptions here: (1) Calendar hirrearchy has structure: AllCalendar->Year->Quarter->Month (2) The lowest level in Calendar where MarketCount is valid is Month. (3) You want to see the MarketCount for the last month in the higher selected time period (Quarter, or Year). As an example, the MarketCount value for March 2006 is 130, then the Ending_MarketCount for Q1 2006 is also 130. Here's a calculated member definition for Ending_MarketCount based on my assumptions:

(TAIL(DESCENDANTS(Calendars.Calendars.CurrentMember, [Calendars].[Calendars].[Month]), 1).Item(0), Measures.MarketCount))

The use of TAIL(DESCENDANTS() finds the last month in the quarter if quarter is the current time level. It also finds the last month in the year if Year is the current time level, and defaults to the current month if a month is the current calendar selection.

Hope this helps.

PGoldy

|||

Sorry, I left an extra parentheses on the end of the calculated member definition. Here is the corrected definition:

(TAIL(DESCENDANTS(Calendars.Calendars.CurrentMember, [Calendars].[Calendars].[Month]), 1).Item(0), Measures.MarketCount)

PGoldy

|||

Have you thought about what you want the users to see when the data is incomplete?

For example, if we currently only have data up to August, but the users want to see the value as at calendar quarter Q3, should they see 0, because September currently has no value? Or should the calculation display the last non empty child (August)?

Wrapping the descendants function with NonEmpty() would give you "Last Non Empty Child" behaviour, but whether you need this or not depends what your business requirements are.

eg.

(TAIL(NONEMPTY(DESCENDANTS(Calendars.Calendars.CurrentMember, [Calendars].[Calendars].[Month])), 1).Item(0), Measures.MarketCount)

...just a thought

|||

Hi Paul,

I tried your suggestion but it did not work. I might be missing something. Let me explain my scenario in more detail.

I have the following dimensions -

DimTime - Hierarchy is Year - Quarter - Month (This dimension table has a granularity of date but I have set the granularity for this dimension in the cube to month level. Would this be a problem? I need the date for other Facts which are not part of the cube in question.)

DimMarket

DimInstrumentModel

DimTerritory (Geography)

I have an aggregate Fact Table 'FactInstallBase' which has a monthly snapshot of the instruments installed and active at various customer sites. Each customer can have one or more instruments. A few sample records in the Fact table look as follows

MarketKey InstrumentModelKey TimeKey TerritoryKey InstrumentCount

123 111 222 333 2

123 112 222 333 1

125 111 222 444 2

There is a requirement to display a count of the markets that own instruments belonging to a certain Instrument model. The users browse the cube using Excel pivot table.

I have created a calculated measure 'MarketCount' and set the aggregation for this measure to DIstinctCount of MarketKey. This works fine when the users browse only one month's data. But when the user's select multiple months like Jan-2006 and Feb-2006, this mesaure gets added for both the months. If the Distinct Market Count for Jan '06 is 110 and for Feb '06 is 120 the MarketCount is displayed as 230 (110+120). This measure is semi-additive and should not be added over time. So even if the user selects multiple months, the MarketCount measure should display as the last month's MarketCount (In this case 120). Am I doing something wrong?

Any help would be greatly appreciated.

Rohit

|||

Paul,

I changed the granularity in my Time Dimension table to Month. The hierarchy for this dimension is Year - Quarter - Month but the MDX code you sent for the calculated measure still does not work.

Rohit

MDX help

Hi All,

I have a calculated measure in my OLAP cube which is a distinct count of one of the keys in the Fact table. This is a semi-additive measure so I need Distinct count of the last child of this measure. How can I do this? Is there a built-in function to handle this.

I am using AS 2005.

Any help would be greatly appreciated.

Rohit

Hi Rohit. More detail here is helpful. You ask for LastChild of a measure. Unfortunately, measures don't have a hierarchy so they don't have children. This means you're probably asking for one of the following:

(1) The LastChild within a hierarchy (such as Product), and the corresponding value of your semi-additive measure (what's the name of the measure?)

OR

(2) The 'closing' value of your semi-additive measure across time.

Are you asking for one of the above options, or something else. An example would be very useful.

PGoldy

|||

Paul,

The calculated measure is MarketCount (distinct count of MarketKey). I am looking for the closing value of the measure across time.

The MarketCount is a monthly snapshot and should not be added across time.

For example, if the MarketCount is 110 in July and 120 in Aug, when the user browses the data in the cube for the months of July and August, the MarketCount should display as 120 and not as 230.

Thanks.

Rohit

|||

SSAS 2005 does have built-in support for semi-additive measures like last child, but only in the Enterprise Edition

http://msdn2.microsoft.com/en-us/library/ms189734.aspx

If you have the standard edition you could achieve the same results through calculations in the MDX script (although the performance would not be as good as the built-in feature)

|||

I am aware of the built in function for Last Child but I need to find the Distinct Count of the Last Child of one of the Fact keys. Please refer to my previous post for further details.

Paul, any ideas?

Rohit

|||

Hi Rohit:

I'm operating on a couple of assumptions here: (1) Calendar hirrearchy has structure: AllCalendar->Year->Quarter->Month (2) The lowest level in Calendar where MarketCount is valid is Month. (3) You want to see the MarketCount for the last month in the higher selected time period (Quarter, or Year). As an example, the MarketCount value for March 2006 is 130, then the Ending_MarketCount for Q1 2006 is also 130. Here's a calculated member definition for Ending_MarketCount based on my assumptions:

(TAIL(DESCENDANTS(Calendars.Calendars.CurrentMember, [Calendars].[Calendars].[Month]), 1).Item(0), Measures.MarketCount))

The use of TAIL(DESCENDANTS() finds the last month in the quarter if quarter is the current time level. It also finds the last month in the year if Year is the current time level, and defaults to the current month if a month is the current calendar selection.

Hope this helps.

PGoldy

|||

Sorry, I left an extra parentheses on the end of the calculated member definition. Here is the corrected definition:

(TAIL(DESCENDANTS(Calendars.Calendars.CurrentMember, [Calendars].[Calendars].[Month]), 1).Item(0), Measures.MarketCount)

PGoldy

|||

Have you thought about what you want the users to see when the data is incomplete?

For example, if we currently only have data up to August, but the users want to see the value as at calendar quarter Q3, should they see 0, because September currently has no value? Or should the calculation display the last non empty child (August)?

Wrapping the descendants function with NonEmpty() would give you "Last Non Empty Child" behaviour, but whether you need this or not depends what your business requirements are.

eg.

(TAIL(NONEMPTY(DESCENDANTS(Calendars.Calendars.CurrentMember, [Calendars].[Calendars].[Month])), 1).Item(0), Measures.MarketCount)

...just a thought

|||

Hi Paul,

I tried your suggestion but it did not work. I might be missing something. Let me explain my scenario in more detail.

I have the following dimensions -

DimTime - Hierarchy is Year - Quarter - Month (This dimension table has a granularity of date but I have set the granularity for this dimension in the cube to month level. Would this be a problem? I need the date for other Facts which are not part of the cube in question.)

DimMarket

DimInstrumentModel

DimTerritory (Geography)

I have an aggregate Fact Table 'FactInstallBase' which has a monthly snapshot of the instruments installed and active at various customer sites. Each customer can have one or more instruments. A few sample records in the Fact table look as follows

MarketKey InstrumentModelKey TimeKey TerritoryKey InstrumentCount

123 111 222 333 2

123 112 222 333 1

125 111 222 444 2

There is a requirement to display a count of the markets that own instruments belonging to a certain Instrument model. The users browse the cube using Excel pivot table.

I have created a calculated measure 'MarketCount' and set the aggregation for this measure to DIstinctCount of MarketKey. This works fine when the users browse only one month's data. But when the user's select multiple months like Jan-2006 and Feb-2006, this mesaure gets added for both the months. If the Distinct Market Count for Jan '06 is 110 and for Feb '06 is 120 the MarketCount is displayed as 230 (110+120). This measure is semi-additive and should not be added over time. So even if the user selects multiple months, the MarketCount measure should display as the last month's MarketCount (In this case 120). Am I doing something wrong?

Any help would be greatly appreciated.

Rohit

|||

Paul,

I changed the granularity in my Time Dimension table to Month. The hierarchy for this dimension is Year - Quarter - Month but the MDX code you sent for the calculated measure still does not work.

Rohit