Distinct Count Calculation
Hello everybody,
I have a cube with appointments including dimensions for companies and dates.
One of my measures is "Appointments" which is the count of all appointments. Another calculated member is "Visits" which is the count of appointments with the type of "Visits".
This is my MDX epxression for "Visits":
AGGREGATE([Appointment].[Type].&[Visit], [Measures].[Appointments])
Now I would like to create a new calculated member "Company Visits" which should be the distinct count of "Visits" by company and date.
Example: 2 "Visits" in one company on the same day should be 1 "Company Visit"
In T-SQL this query works:
COUNT(
DISTINCT(
CASE [Type]
WHEN "Visit" THEN CAST([CompanyID] AS NVARCHAR) + CONVERT(NCHAR(8), CAST([Date] AS NVARCHAR))
ELSE NULL
END
) AS [Company Visits]
How can I do this with MDX in calculations?
Additionally I would like to have a calculated member "Days in Field" as distinct count of dates with "Visits".
Thanks in advance and best regards
Lars
Hi Lars,
Based on your description, you want to create a new calculated member to calculate the distinct count of "Visit" for each company, right? In this case, you can use DistinctCount to achieve your requirement, here is a sample query about DistinctCount function
for your reqerence.
with member [DistCount] as
distinctcount(exists(Employee.Employee.children,,"Reseller Orders"))
Select {[Measures].[Reseller Order Count],
[Measures].[DistCount]} on 0,
([Employee].[Gender].children) on 1
From [AdventureWorks]
Reference:DistinctCount (MDX)
Regards,
Charlie Liao
TechNet Community Support
Similar Messages
-
Running Total & Distinct Count Query
Crystal 10.0.0.533 CR Professional
Hope you can assist.
I have a report listing deals signed, each deal has a corresponding category i.e. industry type, Accountant, Chiropractor, Financial Services, etc.
I am undertaking a Distinct Count on these categories so that in the group footer it is showing number of different categories signed in a period (the report is grouped into different date periods). This is working OK.
However, I need to EXCLUDE the category from this distinct count if the deal value is zero.
I have tried using a formula to show a blank field if the value is zero, however it appears to be distinctly counting the blank as a category?
Any assistance would be much appreciated.
TracyDeffinetly it will be less than what you expect for grand total. It is because when you are calculating distinct count for each group suppose
Group A has categories A,B,C,D-->distinct count (4)
Group B has C,D,E,F-->distinct count (4)
but while calculating grand total then the distinct count will be
A,B,C,D,E,F --> grand total (6) but not (8).
In this case you need to use mannual running total like this
whileprintingrecords;
numbervar i;
i:=i+{running total};
place this in group footer and create another fomula like this
whileprintingrecords;
numbervar i;
place this in report footer to get the correct grand total.
Regards,
Raghavendra -
Distinct Count doesn't return the expected results
Hi All,
I was fighting a little trying to implement a Distinct Count measure over an account dimension in my cube. I read a couple of posts relateed to that and I followed the steps posted by the experts.
I could process the cube but the results I'm getting are not correct. The cube is returning a higher value compared to the correct one calculated directly from the fact table.
Here are the details:
Query of my fact table:
select distinct cxd_account_id,
contactable_email_flag,
case when recency_date>current_date-365 then '0-12' else '13-24' end RECENCY_DATE_ROLLUP,
1 QTY_ACCNT
from cx_bi_reporting.cxd_contacts
where cxd_account_id<>-1 and recency_date >current_date-730;
I have the following dimensions:
Account (with 3 different hierarchies)
Contactable Email Flag (Just 3 values, Y, N, Unknown)
Recency_date (Just dimension members)
All dimensions are sparse and the cube is a compressed one. I defined "MAXIMUM" as aggregate for Contactable Email flag and Recency date and at the end, SUM over Account.
I saw that there is a patch to fix an issue when different aggregation rules are implemented in a compressed cube and I asked the DBA folks to apply it. They told me that the patch cannot be applied because we have an advanced version already installed (Patch 11.2.0.1 ).
These are the details of what we have installed:
OLAP Analytic Workspace 11.2.0.3.0 VALID
Oracle OLAP API 11.2.0.3.0 VALID
OLAP Catalog 11.2.0.3.0 VALID
Is there any other patch that needs to be applied to fix this issue? Or it's already included in the version we have installed (11.2.0.3.0)?
Is there something wrong in the definition of my fact table and that's why I'm not getting the right results?
Any help will be really appreciated!
Thanks in advance,
MartínNot sure I would have designed the dimensions /cubes as you, but there is another method you can obtain distinct counts.
Basically relies on using basic OLAP DML Expression language and can be put in a Calculated Measure, or can create two Calculated measures
to contain each specific result. I use this method to calculate distinct counts when I want to calculate averages, etc ...
IF account_id ne -1 and (recency_date GT today-365) THEN -
CONVERT(NUMLINES(UNIQUELINES(CHARLIST(Recency_date))) INTEGER)-
ELSE IF account_id ne -1 and (recency_date GT today-730 and recency_date LE today-365) THEN -
CONVERT(NUMLINES(UNIQUELINES(CHARLIST(Recency_date))) INTEGER)-
ELSE NA
This exact code may not work in your case, but think you can get the gist of the process involved.
This assumes the aggregation operators are set to the default (Sum), but may work with how you have them set.
Regards,
Michael Cooper -
Distinct count inside a measure group with other measures
Hello,
I have 1 distinct count inside a measure group with other measures, sum, count etc. I know this is not recommended due to poor processing performance and query response time.
Processing performance I can live with if it means not having another measure group, which increases processing time anyway.
I have used the recommended approach before and it generated many questions about what this second measure group is for (visible via excel), even though I made the distinct count appear in the main measure group via a calculated measure.
(it would be nice if you could hide measure groups)
However my question is: is query response time only effected when the distinct count is used in the query? Or is query response time effected regardless if the distinct count is used or not??
Below is an extract from the 2005 distinct count optimizer white paper. It’s not completely clear but I assume if effects queries regardless if distinct count is used or not?
"By adding other measures to the measure group holding a distinct count measure, all of the other measures will be at the same granularity as the distinct count measure, resulting in inefficient data structures and suboptimal
queries."You might also be interested in reading this blog post, which deals with a similar scenario, to get a feeling for some of the things that might be going on behind the scenes:
http://cwebbbi.wordpress.com/2012/11/27/storage-engine-caching-measures-and-measure-groups/
Chris
Check out my MS BI blog I also do
SSAS, PowerPivot, MDX and DAX consultancy
and run public SQL Server and BI training courses in the UK -
Hi,
I have a question about performing a distinct count of number of clients in my cube OLAP.
My clients-dimension table is populated from 4 different databases, this means that I can find the same clients with different Clients_ID (Natural key), because each client has a different ID in every database.
I want to calculate a distinct count on client’s phone number because it’s the same record in all Databases, I always get a big number which is not the right result. I checked deeply the issue and I found that the query I set seems
to not perform the distinct count right on the phone number.
The small test I did with only filters (see screenshot below) let me believe that my query is performing the distinct count on each client ID instead of the phone number.
Here it is the query I used to perform my distinct count:
WITH SET MySet AS
Filter(
{[Dim Date].[Year].&[2014]} *
{[DIM FA Clients].[FaCuNumberX].[FaCuNumberX]},[Measures].[QQT - Fact Ventes]>1 or [Measures].[QQT - Fact Ventes]=1)
MEMBER MEASURES.SETDISTINCTCOUNT AS
DISTINCTCOUNT(MySet)
SELECT {MEASURES.SETDISTINCTCOUNT} ON 0
FROM [CubeAll]
I don’t know why the filter on this mdx query doesn’t work properly like an SQL group by.
Thanks for help!Hi Merouane,
According to your description, you want to calculated to the numbers of client by count distinct Phone Number, right?
In your scenario, you can add a distinct count measure in your project. Distinctcounts are one of the most demanding requests that can be placed on a MOLAP database, caused by the large number of dimensional attributes that typically exist in a cube. Here
is a blog which describe several solutions for distinctcount in a SSAS project, please refer to the link below to see the detais.
http://www.mssqltips.com/sqlservertip/3043/different-options-for-creating-a-distinct-count-measure-in-ssas/
Regards,
Charlie Liao
TechNet Community Support -
This is from BOB post, which was posted in 2008. Now it's 2013 and there is no solution by SAP? We are doing a POC on Explorer, this is probably time to scrap it. Very frustrating! Is there any
Hi,
We have 2 tables used in universe "orders" and
"order_line_product_item" from where we are pulling data in explorer indexes.
We have issues getting distinct count of
customers in explorer when we bring data from order line product item level
facets/dimension.
Just wondering if anyone else had solution of how to
handle this on explorer side?
At universe level we using count (distinct cust key), which is working at
webintelligence.
Looks like BO XI R3 further enhanced on handling this kind
of situation by implementing delegated measures, but don't find anything similar
to it in BO explorer.
Occurrences is always getting us count at the lowest grain level(basically row level
counts, but we are looking for distinct counts though !) .
Is getting
distinct counts limitation in explorer?
Any help will be greatly
appreciated.
Thanks,
~SK
Courtesy of SachinHello,
So if you have a calculation(Webi) function(Excel) built into the two tables you are using from the data source you might try 'configuring' the data as 'Occurance' from the workspace you created or after the file in index if you're using excel. If you are using a universe as a data source for the exploration view set you can attempt to drag the 'measure' needed (count) into the workspace option "measure" when creating the workspace.
I do hope this helps -
Regular measures(measures with SUM function) are not working along Distinct count measures
Hi All,
I am creating a cube that got to have a distinct count measure and a sum measure. if i have created only sum measure then it is working fine. if i create both measures and process the cube only distinct count measure is populated. the sum measure is showing
all blank values. i am using 2008 R2, and creating 2 different measure groups for both measures, after i include the distinct count measure the sum measure becoming null. can you please help me with this? i am breaking my head for last 2 days on this.. Thank
YouRamesh, measures are affected by the context of the queries that contain them, for example and in some cases, you can get a different total count of something by two different queries, this is because the context of the first query is different than
the second one ... keep this in mind.
Now, I've noticed that you are "creating 2 different measure
GROUPS for both measures", and i guess that you are trying to view those two measures _which are from different measure
groups_ at the same time and in the same report.
considering the info in the first point and as you are create the calculated measures in two different measure
groups, I'm not sure but i guess that this is the problem, and i suggest you create those two calculated measures
in the same measure group, then try to view them again and let's see.
if the previous point didn't solve it, please post the expressions you are using to create the calculated measures, maybe this will help in finding the problem. -
Distinct count of Group header
Hi Experts ,
I have a crystal report where i have grouped on Business area and group header carries YTD Actuals Vs Target
and in detail section it carries in different regions information ofthe business area.
Now my concern is i need to count distinct business area in report header based YTD Actuals <= or > Target.
Any Ideas!!!
Thanks,
KrishnaI am not sure if I am understanding what you are asking correctly, but here is my take on it.
You have a group that is identified as "Business Area"
You have done a calculation to determine the YTD Actual for each Business Area which you have put into the Group footer or header
Now you want at the report level a count of the business areas that have YTD actuals <= the Target, and a count of the business areas that have YTD actuals > the Target.
If this is correct, could you use a running total? In your field explorer pain, right click on Running Total and select New to open up the running total editor. In the summary section, select field to summarize on the field you used to define a business area; type of summary is "distinct count'; in the evaluate section, set up a formula to look for the YTD Actual <= to whatever the target was supposed to be; in the reset section, select never.
If this is not what you are trying to do, could you please reword your question? For example, are you trying to reshow each business areas YTD totals compared to the target in your report header instead of a count? -
Error: The sort order specified for distinct count records is incorrect
When processing a measure group with a distinct count measure in it, i get the following error:
"The sort order specified for distinct count records is incorrect."
I have no idea what this means - any ideas?I had the same problem and your fix worked. In more detail, the problematic field was contract_no. I added a named calculation to the table in the Data Source View with the formula CHECKSUM(contract_no). Then I created the distinct count measure on that named calculation. And, lo and behold, the errors disappeared!
Thank you to Frank.
- CindyCindy P Hoskey -
Maintaining distinct counts in summary tables - thoughts
Hi,
We have a 3bn row transaction fact table of sales by product_id and customer_id.
We have some very large summaries on this. e.g product/multiple.
Wish to add at the summary leveks a distinct count of customers whio have been supplied or returned at these aggregate levels.
At moment,code merges into summary tables latest batch of data on daily basis. Fine for sums, but when updating the summary table only want to read the latest batch of data, rather than having to scan the entire lower level table to get the count(distinct)
How can we efficiently increment the counts but ensuring only increemnt if a brand new customer being supplied.
Mat. views impractical at this stage due to volume of data.
Many ThanksHello,
So if you have a calculation(Webi) function(Excel) built into the two tables you are using from the data source you might try 'configuring' the data as 'Occurance' from the workspace you created or after the file in index if you're using excel. If you are using a universe as a data source for the exploration view set you can attempt to drag the 'measure' needed (count) into the workspace option "measure" when creating the workspace.
I do hope this helps -
Distinct count of GRN's in Query PLD
hi all,
We have developed a daily grn report and designed report
using Query PLD.
Report contains
(grn no,date,vendor name,vendor ref no,item code,item
description,quanity & line total)
We want to display no of grn's(distinct count)
@ repetitive area footer.
Is there any function available in formula field to display
distinct count of no. of grn's ? / It should done thru query.
Thanks,
with regards,
A.Jeyakanthanit will be hard from PLD, instead u can have ur query like,
SELECT 'Details', ' ',T0.Docnum, T0.CardName, .... FROM OPDN T0 WHERE Month( T0.DocDate ) = Month ('{%0]')
Union
SELECT 'Counts', count(T0.DocNum) as 'Doc(s) No', ' ', ' ', ... FROM OPDN T0 WHERE Month( T0.DocDate ) = Month ('{%0]')
(Replace { with [ )
Note ' ' is a gap in order to fill the other fields. in first query have given tht ' ' gap purposely so tht the display will not affect ur designing part
union is used b'coz Count() fn is like aggregate, so wont allow further individual fields coming along with it.
the 2nd query will occupy only one row, which by grouping u can separate and can hide from repetitive and take the value via formula field by writing that field's id to it into Footer Area.
Regards,
Dhana.
Edited by: Dhanalakshmi C on Mar 5, 2008 1:31 PM -
Report using Tabular Model and Measures based on Distinct Counts
Hello,
I am creating a report that should present something like this:
YEAR-1 | MONTH-1 | MONTH-2 | MONTH-3... | YEAR | MONTH-1 | MONTH-2 | MONTH-3...
My problem is that when designing the dataset to support this layout I drag the Year, Month and Distinct count Measure, but on the report when I want the value for the YEAR level I don't have it and I cannot sum the months value...
What is the best aproach to solve this? Do I really have to go to advanced mode and customize my MDX or DAX? Can't basic users do something like this that seems so trivial and needed?
Thank you
Luis SimõesHi Luis,
According to your description, you create a Reporting Services report using Analysis Service Tabular Model as the datasource, now what you want is sum the months value on year level, right?
In your scenario, you can add the Month field to column group, add a parent group using Year Field and then add a Total on Month group. In this case, Reporting Services will sum the months value on Year level. I have tested it on my local environment, the
screenshot below is for you reference.
Reference:Lesson 6: Adding Grouping and Totals (Reporting Services)
If this is not what you want, please describe your dataset structure, so that we can make further analysis.
Regards,
Charlie Liao
TechNet Community Support -
How to get Distinct Count of Products across two dimensions
Hi,
I have two dimensions, Item and Presentations. I need to get distinct count of products for IMD_Id + Merc_Pres_Id. IMD_Id is the lowest member in Item and Merc_Pres_Id is lowest in Presentation. My MDX query is given below but when I apply filters to
slice it, it does not work and does not give right count. It always gives the count for all.
/* Last Year Demand - Demand for 12 months back of selected months */
With
Member [Measures].[LYDemand]
as
Sum(
Generate(
EXISTING[All Date].[Fiscal Month Name].[Fiscal Month Name].Members,
{parallelperiod([All Date].[Fiscal Month Name].[Fiscal Month Name], 12
,[All Date].[Fiscal Month Name].CurrentMember)}
,[Measures].[Proj Demand]
/* Last to last Year Demand - Demand for 24 back of selected months */
Member [Measures].[LLYDemand]
as
Sum(
Generate(
EXISTING[All Date].[Fiscal Month Name].[Fiscal Month Name].Members,
{parallelperiod([All Date].[Fiscal Month Name].[Fiscal Month Name], 24
,[All Date].[Fiscal Month Name].CurrentMember)}
,[Measures].[Proj Demand]
/* Current Year Active Products */
Member [Measures].[CYCount]
as
CASE
WHEN
[Measures].[Proj Demand] > 0
THEN
DistinctCount(Existing(([All Items].[IMD Id].[IMD Id],[All Merchandise Presentations].[Merch
Pres Key].[Merch Pres Key])))
ELSE
NULL
END
/* Last year Active Products */
Member [Measures].[LYCount]
as
CASE
WHEN
[Measures].[LYDemand] > 0
THEN
DistinctCount(([All Items].[IMD Id].[IMD Id],[All Merchandise Presentations].[Merch Pres Key].[Merch Pres
Key], [All Items].[Style Name].CurrentMember, (StrToMember('[All Date].[Fiscal Month Name].&[201401]',CONSTRAINED).Lag(12)
: StrToMember('[All Date].[Fiscal Month Name].&[201411]',CONSTRAINED).Lag(12))))
ELSE
NULL
END
/* Last to last Year Active Products */
Member [Measures].[LLYCount]
as
CASE
WHEN
[Measures].[LLYDemand] > 0
THEN
DistinctCount(([All Items].[IMD Id].[IMD Id],[All Merchandise Presentations].[Merch Pres Key].[Merch Pres
Key], [All Items].[Style Name].CurrentMember, (StrToMember('[All Date].[Fiscal Month Name].&[201401]',CONSTRAINED).Lag(24)
: StrToMember('[All Date].[Fiscal Month Name].&[201411]',CONSTRAINED).Lag(24))))
ELSE NULL END
SELECT
[Measures].[CYCount], [Measures].[LYCount], [Measures].[LLYCount],
[Measures].[Proj Demand],[Measures].[LYDemand],[Measures].[LLYDemand]
ON
COLUMNS,
Non
Empty([All Items].[Demand Center Name].[Demand Center Name], [All Items].[Style Name].[Style Name])
ON ROWS
FROM
(SELECT (StrToSet('[All Items].[Style].[ALL]'))
ON COLUMNS
FROM
(SELECT (StrToSet('[All Items].[Demand Center].[ALL]'))
ON COLUMNS
FROM
(select (STRTOSET('[All Items].[Merch Group].&[MG-110]'))
on Columns
FROM
(SELECT (StrToSet('[All Merchandise Presentations].[Merch Pres Chnl Dkey].&[MPC-1]'))
ON COLUMNS
From
[FMI Forecasting]
WHERE {strToMember('[All Date].[Fiscal Month
Name].&[201401]',CONSTRAINED) :
StrToMember('[All Date].[Fiscal Month Name].&[201411]',CONSTRAINED)}
Requirements are as follows:
1. Distinct Count should not include products where Proj Demand is 0, when I am using Filter function to remove products with 0 demand, query is really slow and execution time goes up from 35- 40 secs to 8-9 Minutes.
2. When we apply filter (parameters) Distinct Count should be in the context of filters( which are mentioned in the select statement like Style, Demand Center and Merch Group). Currently after applying filters count does not change.
Thanks for help.Hi Skd78,
Thank you for your question.
I am trying to involve someone more familiar with this topic for a further look at this issue. Sometime delay might be expected from the job transferring. Your patience is greatly appreciated.
Thank you for your understanding and support.
Regards,
Charlie Liao
TechNet Community Support -
How do I solve this distinct count problem?
Hello experts,
So, I have an OBI report (table view). I needed to get the percentage difference btn 2 columns, I did. Then I had to summarize difference in 4 buckets (0-15, 16-30, 31-50, >50%); I did (case statement). NOW, I need to summarize(distinct count) the above buckets based on Store numbers for each day.
Basically, if the difference is btn(0-5%) and I have 5 stores then I need to see 5 stores separately. The problem I am having when I do the distinct count instead of having separate counts for each bucket I am getting the total. I see the buckets summarized, but the store column is showing the total number of all(we have about 700 stores) instead of breaking down the count for each bucket. In the stores column I am using the distinct count function, I don't know if the problem is here or the case statement for buckets. I don't know either OBIEE is able to do what I trying to do, since I have yet to do this kind of function. I have gotten few leads on my first post, so far none of them have worked.
As always, your insights are highly appreciated,Instead of using Distinct Count in Aggregation Rule,Try using in Column Formula.
Let me know if u need any help on this.
Thanks, -
Distinct count of dimension business key in fact table
In my cube I have a fact table which joins to a patient dimension. The patient dimension is a type 2. What I would like to do is get a distinct count of patients who have records in the fact table. The business key in the patient dimension
is the PrimaryMrn. So a SQL query would look like this.
SELECT count(distinct PrimaryMrn)
FROM EncounterFact e
INNER JOIN PatientDim p
on e.PatientKey = p.PatientKey
Is it possible to do this via MDX?
Thanks for the help.If you have to distinct count an attribute in a SCD 2, you might choose between:
Denormalizing that attribute in the fact table, and the create a classical DISTINCT COUNT measure
Use a many-to-many approach - see the "Distinct Count" scenario in the Many-to-Many White paper here:
http://www.sqlbi.com/articles/many2many (for both Multidimensional and Tabular
If you use Tabular, you might want to read also this pattern:
http://www.daxpatterns.com/distinct-count/
Marco Russo http://www.sqlbi.com http://www.powerpivotworkshop.com http://sqlblog.com/blogs/marco_russo
Maybe you are looking for
-
Need to sent alv output as html or as pdf attachment in mail
+Hello I want to send an ALV output as attachement in html or as pdf format. how to do that? line size is greater than 600(nearly 40 fields). +please help me in this query. Regards Guruvayurappan Moderator Message: Please search before posting your q
-
Photoshop not opening double clicked files anymore?
Hi all... All of a sudden my copy of Photoshop CS1 has stopped opening files when they are double clicked in the finder or passed any other way. The only way I can get PS to open a file is by opening them from it's File menu! Draging a file to it's d
-
Satellite L300 PSLB8A doesn't reconnect to net after log off or shut down
My son has the L300 which connects to the net via wireless to the router in another room. When he logs out or shuts down, the next time he starts up he loses connectivity. I am been able to get him reconnected by logging into the router and reconfirm
-
Hi Guys, I have a poor performance when execute the transaction CON2(Module CO), so i spent the run in background mode. And still having performance problems. How can i improve the performance of this transaction ? My environment SAP: 4.7 SAPKH47027
-
Hi! Installation of NW CE 7.3 trial exists with following error message: 2007-11-08 06:08:13.443 iaextract.c:257: extracting: jre/THIRDPARTYLICENSEREADME.txt 2007-11-08 06:08:13.463 iaextract.c:630: Extraction finished with 0 2007-11-08 06:08:13.463