Creating buckets by comparing Dimension member against Measure using MDX - mdx

I have a Product dimension with hierarchies - product_id, initial_price, product_name and Measures - units_sold, cost_per_unit, revenue_per_unit. For each product_id I need to compare initial_price against revenue_per_unit and then assign it to a bucket based on the comparison.
Consider the following example:
Input:
product_id initial_price revenue_per_unit
1 10 12
2 20 18
3 30 30
Output:
product_id bucket_name
1 Profit
2 Loss
3 Same
How can I achieve this using MDX?

WITH Member [Measures].[Bucket] AS
CASE
WHEN [Measures].[initial_price] < [Measures].[revenue_per_unit] THEN
"Profit"
WHEN [Measures].[initial_price] > [Measures].[revenue_per_unit] THEN
"Loss"
ELSE
"Same"
END
SELECT {[Measures].[Bucket]}
ON COLUMNS,
[Product].[Product Name].Members
ON ROWS
FROM [yourcube]
should deliver what you want. Please note that calculated measures need not return numbers, they also can return strings in MDX (they even can return a string for some cells, and a number for others).
In case you do not have the price available as e. g. an average price on aggregated product level, and want to do the bucketing on product level, the best solution would be not do do that in MDX at all, but in the cube design: Just calculate the bucket on the relational side, and add it as an attribute to the product dimension.
You did not state anything about other dimensions, especially time. In case some other dimensions matter as well to determine the bucket, it might be necessary to add a separate dimension just for the buckets.

Related

How to calculate dynamic % of grand total as a measure on Power BI?

I have the below table connected into Power BI and I am looking for ways to create a formula calculating % of grand total of the Rating column and further subtracting with targets for each rating. For example, the % of grand total for Rating 1 is 3 divided by 7 (42.86%). The most important part of the formula is the denominator which has to remain at a total level and dynamic for any filters applied to either Grade or BU columns. For example, denominator at a total level would be 7 and when filtered down to Academy BU should be 3.
Sample Data Table:
Rating Target Table:
I want the end result to look like this,
I have used the following formula to achieve this,
Measure created: % of total calc = DIVIDE(COUNT('Table'[Rating]),CALCULATE(SUM('Table'[Count]),'Table'[Rating]))
To make the above formula work I had to add an extra column and include ones in it (see below)
I want to know if there are other ways of achieving this outcome?
ALLEXCEPT will produce such result to exclude used dimensions and include mandatory filters such as date with one condition, rating, date, any dimension must be in the same table.

Exclude one dimension from total count / sum in qlik

I want to have the %-Share of a value in Qlik. I have two dimension and on KPI which I am using in a distribution graph:
The data is:
Point Brand Business
A AA 1
B AA 1
C AA 1
Right now i am using the formula: Count([Business])/Count(total [Business]), on my Y-Achse I have the Brand and my point is Point, Right now he is calculating the Super total, but How can I only calculate the total for the Brand? Something like Count([Business])/Count(total [Business], if Brand == Brand)
You should add Brand field as aggregation scope
count([Business]) / Count(total <Brand> [Business])
From Qlik's documentation
By using TOTAL [], where the TOTAL qualifier is followed by a list of one or more field names as a subset of the chart dimension variables, you create a subset of the total possible values.
Adding the field to the total can be translated as: count all Business but aggregated by Brand. And since Brand is also a dimension to the chart then for each row Count Total will be the count for the current Brand value

TOTAL vs Aggr in QlikView

I'm trying to understand how TOTAL and Aggr work in QlikView. Could someone please explain the difference between the two examples below, and if possible please illustrate with a SQL query?
Example1:
Max({<Field1=>} Aggr(Sum({<Field2={'Value'}, Field1=>} StuffCount), Field1))
Example2:
Max({<Field1=>} TOTAL Aggr(Sum({<Field2={'Value'}, Field1=>} StuffCount), Field1))
Not sure what you mean with and SQL query in this example. Anyway, imagine you have this list of Customers (CustomerID) and Sales (Sales):
CustomerID/ Sales
Customer1 25
Customer2 20
Customer1 10
Customer1 5
Customer1 20
Customer3 30
Customer2 30
Then you want to show it on a pivot table with dimension CustomerID and two expressions:
Max(Aggr(Sum(Sales), CustomerID)) // this will show 60 for the first customer, 50 for the second and 30 for the third one
Max(TOTAL Aggr(Sum(Sales),CustomerID)) //this will show 60 in every row of your table (which is the maximum sum of sales among all customers)
So basically AGGR creates a temporal list of whatever you put in the first function input (in this case sum(Sales)) using the dimension of the second (CustomerID). Then you can perform operations on that list (such as Max, Min, Avg...). If you write TOTAL and use the expression in a pivot table, then you 'ignore' the dimensions that might be affecting the operations.
Hope it helps
TOTAL keyword is useful in charts/pivot tables. It applies the same calculation on every datapoint in the chart/pivot, with independence of dimentions.
Therefore - if you put your expression into pivot table - 1st option may display different values per cell (if the Aggr is rellevant) when the 2nd will result in same values.
Aggr function allows making double aggregations (avg of sum, max of count etc..) on different group by bases.

MDX Query Can't connect Fiscal Month/Quarter to my Measures

I've been building an MDX query using excel's powerpivot. I connect to my cube, drag and drop Measures /Dimensions and my query has been working just fine. Up until I try to pull different dimensions.
A simple version of my query:
SELECT
NON EMPTY { [Measures].[EP Projected Impressions] } ON COLUMNS,
NON EMPTY { ([EP Hierarchy].[EP Tactic ID].[EP Tactic ID].ALLMEMBERS ) } ON ROWS
FROM [MI_Cube]
This will return:
(EP Tactic ID) (EP Projected Impressions)
1 10
2 20
3 30
4 40
5 50
Now when I try to pull in date information for each tactic from the Time dimension it just gives me a copy of the above results with each time dimension member.
Example query:
SELECT
NON EMPTY { [Measures].[EP Projected Impressions] } ON COLUMNS,
NON EMPTY { ([EP Hierarchy].[EP Tactic ID].[EP Tactic ID].ALLMEMBERS * [Time].[Fiscal Year].[Fiscal Year].ALLMEMBERS ) } ON ROWS
FROM [MI_Cube]
Results:
(EP Tactic ID) (EP Projected Impressions) (Fiscal Year)
1 10 FY2015
1 10 FY2014
1 10 FY2013
1 10 FY2012
1 10 FY2011
2 20 FY2015
2 20 FY2014
2 20 FY2013
2 20 FY2012
2 20 FY2011
etc....
Does this mean that I cannot pull the Time.FiscalYear dimension for each TacticID? Or do I need to restructure my query? EP Hierarchy has lots of dimension members I can pull successfully, but when I try to pull anything from EP Hierarchy and Time my results get multiplied instead of combined.
Thanks for any advice, trying to wrap my head around cubes and mdx queries.
It seems that you are simply missing a relation between the fact table holding the [EP Projected Impressions] member, and the dimension table holding your [Time] dimension.
By adding a relation between a foreign key on the fact table and the primary key on the dimension table, your measures should get correctly filtered by any attributes you slice on the dimension.
Thank you for the responses, it turns out the measure I was using was not connected to the time dimension. Apparently that was an expected behavior, after trying different measures I am getting the results I was expecting.

sum group by in MDX

I want the equivalent SUM and Group By as in t-SQL. But I haven't found the answer on the web.
My MDX return has some records that have the same name. I want to show the distinct name with the measure summed up just like Group by feature in SQL.
It seems like it's a common feature. Thanks.
When you define a measure in AS you can set it several different ways including count and sum.
Let's assume you have a product dimension and a fact of sales. A simple query to get the total sales by product would look like the following.
SELECT {[Measures].[ItemCount], [Measures].[SalesDollars]} ON 0,
[Products].[Products].children ON 1
FROM [CUBE]
This would give you sample output like
Product Item Count Sales Dollars
Bike 10 1000
Tire 3 650