MDX Calculated member Sum of selected members rollup - ssas

I'm trying to write a calculated member that will roll up along a dimension based on the selected values provided on the axes.
Product -> subcategory -> category
lexus -> sedan -> car
lexus-coupe -> coupe -> car
bmw -> sedan -> car
bmw-coupe -> couple -> car
ford -> pickup -> truck
chev -> pickup -> truck
ford-suv -> suv -> truck
lincoln-nav -> suv -> truck
[Calculated measure] = [measures].[a]+[measures].[b]
suppose the user wants to see [Calculated measure] at the car level for just lexus-coupe and bmw. How do you create a calculated measure that will roll up at the car level based on what has been selected in the dimension?
Thank you in advance!

I have previously written MDX to check what dimensions are being used on rows (or columns) and change the way a measure is being calculated. It is a bit messy.
This example looks to see if axis 1 (rows?) is using any Measures, and can change the calculation accordingly:
IIF(InStr(1, SetToStr(StrToSet("Axis(1)")), "[Measures].") = 0, blah, blah)
Can't remember why I did StrToSet and then SetToStr, but it worked!
You may be able to use VBA string functions like InStr() to examine what the user has selected on rows and columns, and fiddle with your calculated members accordingly...

Related

Comparison of the queries

There are three queries A, B and C. I should compare the queries B and C to query A. And answer if in comparison to A: are the results of B and C respectively to a rollup, drill down or nothing of these.
Query A:
SELECT
Geography.Region, Time.Month, SUM (Sales.numberSold)
FROM
Sales, Time, Product, Geography
WHERE
Sales.ProductID = Product.ProductID
AND Sales.TimeID = Time.TimeID
AND Sales.GeoID = Geography.GeoID
AND Product.ProductFamily = "video"
AND Time.Year = 2000
AND Geography.Country = "Germany"
GROUP BY
Geography.Region, Time.Month;
Query B:
SELECT
Geography.Region, Time.Month, SUM (Sales.numberSold)
FROM
Sales, Time, Geography
WHERE
Sales.TimeID = Time.TimeID
AND Sales.GeoID = Geography.GeoID
AND Time.Year = 2000
AND Geography.Country = "Germany"
GROUP BY
Geography.Region, Time.Month;
Query C:
SELECT
Geography.City, Time.Month, SUM (Sales.numberSold)
FROM
Sales, Time, Product, Geography
WHERE
Sales.ProductID = Product.ProductID
AND Sales.TimeID = Time.TimeID
AND Sales.GeoID = Geography.GeoID
AND Product.ProductFamily = "video"
AND Time.Year = 2000
AND Geography.Country = "Germany"
GROUP BY
Geography.City, Time.Month;
Compare the queries B and C to query A.
In comparison to A: are the results of B and C respectively:
a rollup or
a drill down or
neither of these two?
The gaps are
The result of query ...... is .......................... in comparison to the result of query A".
The missing parts to be inserted are: "B", "C" and "a rollup", "a drill down", or "neither"
My words
The result of query B is a rollup in comparison to the result of query A".
I don't know if my answer is correct. What is the solution here for this point and why ?
Cube:
A cube consists of cells each of which is defined by the intersection
of all dimensions (axes) belonging to this cube. Each cell of the cube
can contain one or more measures.
Rollup:
The CUBE operator calculates aggregations by combining all possible
subsets of the attributes listed in the parentheses following the word
CUBE. Often, not all the combinations are necessary, however, but it
may be sufficient to aggregate only by taking first one attribute,
then two, then three, etc. until all are taken together. This is done
by the ROLLUP operator.
Drill Down:
In contrast to simple data access, OLAP requires a multidimensional
data model that is built according to analysis needs. => Not a
relational technique. OLAP allows data analysis with the goal of
discovering new information. Reports present consolidated values in
tables and images. The functionality allows for instance to "drill
down" to detailed data and "drill up" ("roll up") again.
Drill down and roll up functions are inverse and allow you to add and remove granularity in axes like a zoom.
a roll up - less granularity in the target table (for example years instead of months)
a drill down - more granularity in the target table (months instead of years)
neither of these two - there is other data in the tables
A three-dimensional cube is given, where the dimensions are:
Geography
Time
Product
Values are sales volumes (SUM (Sales.numberSold)).
Let's call this source cube Z.
In the end, A, B, and C show only two dimensions:
Geography
Time
The Product is always shrunk into one dimension.
Cube A:
The Product is shrunk into one dimension by the slice for Product.ProductFamily = "video"
Z
rollup on Product (from ArticleName to ProductFamily)
slice for Product.ProductFamily = "video"
rollup on Time (from Day to Year)
slice for Time.Year = 2000
rollup on Geography (from BranchName to Country)
slice for Geography.Country = "Germany"
drill down Geography from Country to Region
drill down Time from Year to Month
A
Cube B:
The Product is shrunk into one dimension by rollup on Product (from ArticleName to All).
The B is formed from the Z with the same granularity of rollups and drills down.
The result of query B is neither of these two in comparison to the result of query A.
Z
rollup on Product (from ArticleName to All)
rollup on Time (from Day to Year)
slice for Time.Year = 2000
rollup on Geography (from BranchName to Country)
slice for Geography.Country = "Germany"
drill down Geography from Country to Region
drill down Time from Year to Month
B
Cube C:
The Product is shrunk into one dimension the same way as in Cube A.
So the only difference is in granularity.
The C is more detailed.
The result of query C is a drill down in comparison to the result of query A.
A
drill down Geography (from Region to City)
C
Sources:
Data Warehousing - OLAP on tutorialspoint
Online analytical processing on wikipedia
OLAP Operations in the Multidimensional Data Model on javatpoint
Z
. . . . .
. . . . .
+---+---+---+---+ .
audio 123 / 2 / 9 / / /|.
+---+---+---+---+ + .
video 321 / 3 / 6 / / /|/|.
+---+---+---+---+ + + .
video 123 / 5 / 2 / / /|/|/|.
+---+---+---+---+ + + + .
LA | 5 | 2 | | |/|/|/|.
+---+---+---+---+ + + +
NY | 3 | 8 | | |/|/|/:
+---+---+---+---+ + + :
| | | | |/|/:
+---+---+---+---+ + :
| | | | |/:
+---+---+---+---+ :
: : : : :
: : : : :
01-01-22
02-01-22
Edit 1:
The disadvantage of the sources is that they say what a given function is and not what it isn't. It is important to thoroughly understand what they do to determine what they can't.
Even if you only have to decide between the Roll-Up and the Drill Down it is necessary to understand the Slice in your example. The Slice is pretty weak, so it's a good idea to find out that it's a case of the Dice.
The Roll-Up and the Drill Down aggregate all values - the Group by clause.
The Slice (Dice) filters - the Where clause.
Things achieved by the Slice can't be achieved by the Roll-Up.
The Roll-Up and the Drill Down in dimension Product can scale on All (remove the dimension), Category, Family, Group, and Name.
In select A, we get rid of the Product dimension using the Slice and in query B using the Roll-Up to All.
The Slice in query A leaves only Sales.numberSold for "video". This can't be achieved with Roll up.

Slice a measure based on only one attribute in SSAS Tabular

I am trying to create a measure which should slice the value based on only one dimension in SSAS tabular.
e.g the cube has a measure Population. and it has dimensions Country and time the data is something like:
Crty_A : 2018 :100
Crty_A : 2017 :200
Crty_B : 2018: 50
Crty_C : 2018: 25
The data in the measure should get aggreted only at country level. like when only country name is selected the data should be like:
Crty_A :300
Crty_B : 50
Crty_B : 25
But if time and the measure is selected the data should be:
2018 :100
2017 :200
2018: 50
2018: 25
is there any way this can be achieved?
You should be able to add simple SUM into your formula that sums the entire column. Save that as your measure and then when you bring the measure into the downstream application (Excel, PowerPivot, PowerBI, ETC) the slicing should already be applied to that measure based on the filters being applied. I'm assuming that you have a star schema and all the tables appropriate relationships.
Measure_Name:=SUM(TableName[Population])

With MDX is there a generic way to calculate the ratio of cells with regards to the selected members of a specific hierarchy?

I want to define a cube measure in a SSAS Analysis Services Cube (multidimensional model) that calculates ratios for the selection a user makes for a predefined hierarchy. The following example illustrates the desired behavior:
|-City----|---|
| Hamburg | 2 |
| Berlin | 1 |
| Munich | 3 |
This is my base table. What I want to achieve is a cube measure that calculates ratios based on a users' selection. E.g. when the user queries Hamburg (2) and Berlin (1) the measure should return the values 67% (for Hamburg) and 33% (for Berlin). However if Munich (3) is added to the same query, the return values would be 33% (Hamburg), 17% (Berlin) and 50% (Munich). The sum of the values should always equal to 100% no matter how many hierarchy members have been included into the MDX query.
So far I came up with different measures, but they all seem to suffer from the same problem that is it seems impossible to access the context of the whole MDX query from within a cell.
My first approach to this was the following measure:
[Measures].[Ratio] AS SUM([City].MEMBERS,[Measures].[Amount])/[Measures].[Amount]
This however sums up the amount of all cities regardless of the users selection and though always returns the ratio of a city with regards to the whole city hierarchy.
I also tried to restrict the members to the query context by adding the EXISTING keyword.
[Measures].[Ratio] AS SUM(EXISTING [City].MEMBERS,[Measures].[Amount])/[Measures].[Amount]
But this seems to restrict the context to the cell which means that I get 100% as a result for each cell (because EXISTING [City].MEMBERS is now restricted to a cell it only returns the city of the current cell).
I also googled to find out whether it is possible to add a column or row with totals but that also seems not possible within MDX.
The closest I got was with the following measure:
[Measures].[Ratio] AS SUM(Axis(1),[Measures].[Amount])/[Measures].[Amount]
Along with this MDX query
SELECT {[Measures].[Ratio]} ON 0, {[City].[Hamburg],[City].[Berlin]} ON 1 FROM [Cube]
it would yield the correct result. However, this requires the user to put the correct hierarchy for this specific measure onto a specific axis - very error prone, very unintuitive, I don't want to go this way.
Are there any other ideas or approaches that could help me to define this measure?
I would first define a set with the selected cities
[GeoSet] AS {[City].[Hamburg],[City].[Berlin]}
Then the Ratio
[Measures].[Ratio] AS [Measures].[Amount]/SUM([GeoSet],[Measures],[Amount])
To get the ratio of that city to the set of cities. Lastly
SELECT [Measures].[Ratio] ON COLUMNS,
[GeoSet] ON ROWS
FROM [Cube]
Whenever you select a list of cities, change the [GeoSet] to the list of cities, or other levels in the hierarchy, as long as you don't select 2 overlapping values ([City].[Hamburg] and [Region].[DE6], for example).

Get the most common item in a calculated column

I've figured out how to accomplish the equivalent of the following in a measure, but I need to use it as the legend in a Power View chart so it needs to be done in a calculated column. The change of context from calculated field to calculated column has completely screwed me up.
In my data model, I have a table of job applications. Each record has a single Specialty and an address for the company being applied to. Each specialty can show up multiple times in the table.
ApplicationTable:
ApplicationID | Name | Specialty | City | State
32911 |Joe Bob | Engineering | Miami | Florida
89547 |Ralph Kramden | Shouting | New York | New York
etc.
I also have a table of states. It just has columns for state name and postal abbreviation. I need to create a column with the most commonly occurring Specialty per state.
If I could do this as a calculated field, I would have been finished hours ago. I just used a pretty straightforward application of topn:
Top Specialty := FIRSTNONBLANK (TOPN (3, VALUES (ApplicationTable[Specialty]),[Count of ApplicationID], ApplicationTable[Specialty])
I used FIRSTNONBLANK and TOPN(3...) because some states only have a few applications, so each specialty only shows up once. In my application it's fine to just pick the first specialty in the list in those cases.
Anyway that formula is cool but it doesn't help here. So how do I do the equivalent in a calculated column, so I can use it as a key or a filter? Specifically, I think I need to do this in the StateTable, giving me the name of the specialty that occurs most per state in the ApplicationTable. Ideas?
First, create a basic measure to count specialties:
SpecialtyCount :=
COUNTA ( ApplicationTable[Specialty] )
Next, create a measure to figure out the highest single specialty (within a context):
MostSpecial :=
MAXX ( VALUES ( ApplicationTable[Specialty] ), [SpecialtyCount] )
Finally, add a calculated column to your States table:
=
FIRSTNONBLANK (
ApplicationTable[Specialty],
IF (
[SpecialtyCount]
= CALCULATE ( [MostSpecial], VALUES ( ApplicationTable[Speciality] ) ),
1,
BLANK ()
)
)
By placing this as a calculated column, our filter context is each state. So first PowerPivot will filter the ApplicationTable to just applications within the state, and then it will use FIRSTNONBLANK() to iterate through each ApplicationTable[Specialty], calculate its SpecialtyCount and see if that equals the MostSpecial count within that state. If so, it's not blank, and that's the specialty it returns.

MDX query to use a set but return a single row

I am new to MDX and have just started using Named sets to group several members of a dimension.
Whenever I use a SET in a query, the results returned are always detailed out for each individual member of the set. I am looking to get one one for the set.
For example: I have two Measures: Sales Dollars and Shipped Units. The then have a State dimension for each of the 50 states in the United States.
I want to see the Sales and Units measures for 3 specific states and then also for a group (Named Set) of 4 other states.
Example MDX:
With SET [My Favorite States] AS '{[States].[Illinois], [States].[Wisconsin]}'
select NON EMPTY {[Measures].[Sales], [Measures].[Shipped Units]} ON COLUMNS,
NON EMPTY {[States].[Alabama], [States].[New York], [My Favorite States]} ON ROWS
from [cubename]
This returns:
Measures
States Sales Shipped Units
Alabama $100 5
New York $500 20
Illinois $150 15
Wisconsin $900 25
What I want is for the Set to appear as a total on a single line. Similar to:
Measures
States Sales Shipped Units
Alabama $100 5
New York $500 20
My Favorite States $1,050 40
Is there an MDX function that will allow the set of specific members to be treated as a group?
You can use a calculated member to aggregate the separate states:
With Member [States].[My Favorite States] AS 'Aggregate({[States].[Illinois], [States].[Wisconsin]})'
select NON EMPTY {[Measures].[Sales], [Measures].[Shipped Units]} ON COLUMNS,
NON EMPTY {[States].[Alabama], [States].[New York], [States].[My Favorite States]} ON ROWS
from [cubename]