Is there a way to calculate the MODE in SSAS? I see there are Microsoft provided functions for Median, AVG, Max, and Min but nothing for Mode. Would I need to code a user defined function in .net to make this happen or is there an easier alternative?
views:
311answers:
3This should do it:
create table #temp (value int)
insert into #temp (value) values (1)
insert into #temp (value) values (1)
insert into #temp (value) values (1)
insert into #temp (value) values (2)
insert into #temp (value) values (2)
insert into #temp (value) values (3)
insert into #temp (value) values (3)
insert into #temp (value) values (3)
insert into #temp (value) values (3)
insert into #temp (value) values (3)
insert into #temp (value) values (3)
insert into #temp (value) values (4)
select value from (select top 1 count(*) as counts, value from #temp group by value order by count(*) desc) as myTemp
I can see two possible alternatives. Creating a .Net stored proc is one, but there is a bit of overhead in calling those, so you would not want to run a Mode function over too many cells.
The other option, if there is not too large a number of distinct value that you need to operate across, is to create a dimension with an attribute based off the number. Then you could create a row count measure and get the mode by doing TOPCOUNT(... ,1) over the dimension with the numeric values.
How about creating a fact dimension (Degenerate dimension) of the numbers you are interested in the mode of, then using that against a count of rows, take the Top Count of 1?
Or are you after a more general MDX calculation that you could use for may scenarios?