Reg:Adding two measures from MDX into one field - sql

I have two measures in cube. I want to add two values from each measure and showing into one measure. basically adding two measure values into one.
How can I do.
thanks

To create a new measure which sums two other measures:
WITH MEMBER [Measures].[MyNewMeasure] AS [Measures].[MyFirstMeasure] + [Measures].[MySecondMeasure]
SELECT {[Measures].[MyNewMeasure]} ON COLUMNS,
{ whatever } ON ROWS
FROM [CubeName]

Related

How to Create a new Measure that is derived from a preexisting one in a Multidimensional cube

I need to create a new measure that is a derivative of a preexisting measure. The preexisting measure is just a straight count of all rows. The new measure is a count of all rows where EventType != 3 or 4 and then that count is divided by the preexisting measure count to produce a %. See data and questions below for context.
ID-------DimDateKey------EventType
1-------20220101-----------3
2-------20220101-----------2
3-------20220101-----------4
4-------20220101-----------1
5-------20220101-----------3
6-------20220101-----------4
Preexisting measure- straight count of all rows - 6
Needed Measure – count of all rows where Eventype not in (3,4) / Prexisting measure: 2/6=.333
(It needs to be filterable by all associated dimensions like DimDateKey)
Do I need to create a new Data source view to generate a count of these rows where EventType is not in(3,4), or can I define this as a temporary subset in the calculations tab of a Multidimensional Cube?
Once I have reached the step where new Measure/preexisting measure, what is that logic supposed to look like in Calculations? I see other measures are generated through being divided in Calculations, but if I try to test similar syntax in a MDX query I can’t get it to return anything but an error.
Lastly, once the new count is divided by the old count it should still be filterable by all of the dimensions that each count has a relationship with, correct?

Simple MDX Calculated Member

In my simple cube, I have a measure = \[Measure\].\[Salary\], I have also \[DimEmpployee\].\[EmployeeLastName\].\[Smith\]. I would like to create calculated measure, where I can display in Axis 0 two measures - \[Measure\].\[Salary\] and calculated measure \[Measure\].\[SmithsSalaries\], to compare difference between Smith's earnings vs Total Salary.
I would like to compare Measure.SmithSalaries with other measures accross all diemensions. Is it possible to create such a measure using SCOPE statement?
I was playing around SCOPE statements, but it was displaying results only if DimEmployee was selected. I am looking for something which is running in blocks to avoid performance issues.
I think you only need a simple calculated measure.
CREATE MEMBER CURRENTCUBE.[Measures].[SmithSalaries]
AS ([DimEmployee].[EmployeeLastName].[Smith], [Measures].[Salary]),
VISIBLE = 1 ;
After that you can combine that with you total salary for example to get a ratio.
CREATE MEMBER CURRENTCUBE.[Measures].[SmithSalaries Ratio]
AS DIVIDE(([DimEmployee].[EmployeeLastName].[Smith], [Measures].[Salary]),[Measures].[Salary])
VISIBLE = 1 ;
SCOPE allows you to have different behaviors when different combinations of Dimensions are into play, like returning a different calculation when the DimEmployee is selected but otherwise just return the normal calculation. Like a Very efficient IF condition to check what are in the Axis of this calculation.

Advice needed on weighted averaging in power query or power pivot

I need to create a weighted average that multiplies a column of volume manufactured for multiple manufacturing plants by a column containing the cost to manufacture at each plant, and returns one weighted average value for a specific product type for all plants.
I've tried adding this as a calculated column using:
=sumx('Plant','Plant'[Cost]*'Plant'[Tonnage])/sum('Plant'[Tonnage])
But this goes row by row, so it doesn't give me the full over riding average that I need for the company. I can aggregate the data, but really want to see the average lined up against individual plant for benchmarking
Any ideas how I can do this?
You can do this in multiple ways. You can either make a single more complex calculation, or you can make a few calculated columns to make the final calculation more transparent. I will pick the latter approach here, because it is more easy to show what is going on. I'm going to use the following DAX functions: CALCULATE, SUM, and ALLEXCEPT.
First, create three new calculated columns.
The first one should contain the [Volume] times [Cost] for each record:
VolumeTimesCost:=[Volume] * [Cost]
The second one should contain the sum of [VolumeTimesCost] for all plants within a given product type. It could look like this:
TotalProductTypeCost:=CALCULATE(SUM([VolumeTimesCost]),ALLEXCEPT([Product Type]))
Using the ALLEXCEPT([Product Type]) removes the filter from all other columns than the [Product Type] column.
The third calculated column should contain the SUM of [Volume] for all plants within a given product type. It could look like this:
TotalProductTypeVolume:=CALCULATE(SUM([Volume]),ALLEXCEPT([Product Type]))
You can then create your measure based on the two calculated columns [TotalProductTypeCost] and [TotalProductTypeVolume].
I hope that helps you solve the issue correctly. Otherwise feel free to let me know!

Using a Dimension Property as a Measure

In my product dimension, I have an attribute called CustomerRating which is a string. Possible values are "1", "2", "3", and "4".
I want to turn this attribute into a Measure that averages ratings for collections of products.
with member [Measures].[Product Rating] as
( [Product].[Project Name].CurrentMember.Properties("CustomerRating"))
select [Measures].[Product Rating] on columns
from [MyCube]
This query produces an error - I suspect because I'm dealing with a string.
How do I turn customer rating into a measure that is an average rather than a sum?
The best approach would be to add an int column to the source table of the dimension, maybe just as a named calculation in the DSV. Then you would add a measure group on the dimension table, and define a measure rating_sum that sums this column, and a count measure in this measure group.
Then define a calculated measure as rating_sum / count.
If everything works, make the two measures rating_sum and count invisible.
Not tested but I'm wondering if this errors?
WITH
MEMBER [Measures].[Product Rating] AS
CInt([Product].[Project Name].CurrentMember.Properties("CustomerRating"))
SELECT
{[Measures].[Product Rating]} ON COLUMNS
From [MyCube]

Can I Select a Calculated Member On the "Row" Dimension in MDX?

I know I can do this (quick example off the top of my head):
WITH
Member Measures.AnotherDataColumn AS [MyDimension].CurrentMember.Properties("MyProperty")
SELECT
{
Measures.DataColumn,
Measures.AnotherDataColumn
} ON COLUMNS
{
[MyDimension].Item
} ON ROWS
But is there a way to include that same calculated member Measures.AnotherDataColumn in the ROWS dimension?
Thanks in advance!!
You can create calculated members in any dimension, not just the measures dimension, but then you need to tell SSAS how you want it to aggregate the measures. Typically this is aggregating a set of other members from the same dimension and you see something like the following using the Aggregate function:
WITH
Member MyDimension.CalcMember AS Aggregate({[MyDimension].Item1:[MyDimension].Item3})
SELECT
{
Measures.DataColumn,
} ON COLUMNS
{
[MyDimension].Item
} ON ROWS