Quantcast
Channel: SQL Server Analysis Services forum
Viewing all articles
Browse latest Browse all 14337

Create Measure for "Count of registered Users"

$
0
0

Hello

I am trying my hands on SSAS Cube. So bear with me here for a moment. Here’s my question

We have a very simple cube created in star schema. Just one Fact table and 5 Dimension table surrounding it.

Here’s what we want to achieve. We have a table in our relational SQL database called “user_registration” which we are going to integrate in SSAS Cube as a new Dimension. This table basically keeps track of registration status of a user. At any time a user can be either registered or unregistered. Let’s not focus on the design of the table and ways to improve it since that’s out of my scope as of now. This is how the table looks like.



So using this table we want to count unique users that are registered for a given time period. How can I create a measure for this?

Let me show you what we want to achieve by using T-SQL. I am sure there is a better way to write this but that’s not I wanted to focus on so pardon me if you see any obvious pitfalls in my script.

/**************************
 Script to generate table
**************************/
IF OBJECT_ID('tempdb..#tmpUserRegistration','U') IS NOT NULL
BEGIN
    DROP TABLE #tmpUserRegistration
END

CREATE TABLE #tmpUserRegistration(
	[id] [int] IDENTITY(1,1) NOT NULL,
	[user_id] [int] NOT NULL,
	[company_id] [int] NOT NULL,
	[registration_date] [datetime] NULL,
	[unregistration_date] [datetime] NULL,
	[creation_date] [datetime] NOT NULL)

INSERT INTO #tmpUserRegistration
SELECT 1200786, 120, '2013-11-11 08:29:37.960', NULL, '2013-11-11 08:29:37.960' UNION ALL
SELECT 1200786, 120, NULL, '2013-11-20 08:29:37.960', '2013-11-20 08:29:37.960' UNION ALL
SELECT 123546, 478, '2013-11-08 10:23:45.543', NULL, '2013-11-08 10:23:45.543' UNION ALL
SELECT 18365, 654, '2013-11-01 08:11:33.342', NULL, '2013-11-01 08:11:33.342' UNION ALL
SELECT 785932, 145, '2013-10-28 09:36:22.784', NULL, '2013-10-28 09:36:22.784' UNION ALL
SELECT 235445, 58, NULL, '2013-10-10 12:42:37.123', '2013-10-10 12:42:37.123' UNION ALL
SELECT 98574, 748, NULL, '2013-10-08 09:45:44.877', '2013-10-08 09:45:44.877' UNION ALL
SELECT 12379, 326, '2013-10-02 09:23:12.789', NULL, '2013-10-02 09:23:12.789' UNION ALL
SELECT 56485, 986, '2013-09-25 10:44:32.123', NULL, '2013-09-25 10:44:32.123' UNION ALL
SELECT 346844, 74, '2013-09-21 11:13:45.456', NULL, '2013-09-21 11:13:45.456'

SELECT * FROM #tmpUserRegistration tur

/**************************
	   T-SQL Approach
**************************/

DECLARE @start DATE = '01/01/2013'
	   ,@end DATE = '12/31/2013'

--self-joining the table to only include a user's latest(MAX(creation_date)) registration status
SELECT COUNT(her.user_id)
FROM #tmpUserRegistration her
    JOIN (SELECT user_id,MAX(her.creation_date) Date
		  FROM #tmpUserRegistration her
		  WHERE her.creation_date BETWEEN @start AND @end
		  GROUP BY her.user_id) MaxDate
    ON her.user_id = MaxDate.user_id
	   AND her.creation_date = MaxDate.[Date]
WHERE IIF(her.registration_date IS NOT NULL AND her.registration_date <> '01/01/1900',1,0) = 1 --filter to get count of registered users

So if you run this script you would get a count of 6. That is the count of registered users in the period of 2013. If you have noticed there are total 7 records with registration date in it but since user 1200786 registered and then unregistered in the same time period that user is considered unregistered.

Did this make sense? How can I create a measure to hold count of registered users? I am really new to SSAS world so I would appreciate if you can point me to any basic resources to achieve this.

Thanks a lot for taking time to read this

Nik




Viewing all articles
Browse latest Browse all 14337

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>