MDX Balance to Totals - mdx

In the example below I have a products dimension with product id 1,2,3,4..etc. I want to create a calculated member called Balance To Totals that will sum over all the products they have chosen and subtract it from the grand total. This should happen over any measure they select. In the example below I have the products dimension on rows, and two measures on columns. If the user selects product 1,4,5 then the balance to totals will be the sum of those three products subtracted from the grand totals. Does anybody know how I can accomplish this through a calculated member?
Product Id Cost($) Profit($)
1 80 3
4 70 4
5 50 2
BalToTotal 125 25
Grand Total 325 34

I just set up a simple MDX query on AdventureWorks Cube, so you can play with it:
with set [SelectedProducts] as {[Product].[Subcategory].&[26], [Product].[Subcategory].&[1]}
member [Product].[Subcategory].[BalToTotal] as [Product].[Subcategory].defaultmember - Aggregate ([SelectedProducts])
select
{[Measures].[Internet Sales Amount], [Measures].[Reseller Order Quantity]} on 0,
{[SelectedProducts], [Product].[Subcategory].[BalToTotal], [Product].[Subcategory].defaultmember} on 1
from [Adventure Works]
The output will be:
Internet Sales Amount Reseller Order Quantity
Bike Racks €39.360,00 2.838
Mountain Bikes €9.952.759,56 23.351
BalToTotal €19.366.557,66 188.189
All Products €29.358.677,22 214.378
Hope this help

Related

MDX. Mixing measures with dimensions

Have multiple departments, which have monthly sales' measure and "Inception to Date" (ITD) and "Year to Date" (YTD) sales' data measures.
SELECT {[Dep1], [Dep1ITD], [Dep1YTD], [Dep2], [Dep2ITD], [Dep2YTD]} ON ROWS,
{[Calendar Date].[Calendar yyyy-MMM].[Calendar yyyy-MMM].ALLMEMBERS} ON COLUMNS
FROM (
SELECT ( { [Calendar Date].[Calendar yyyy-MMM].&[202206]:[Calendar Date].[Calendar yyyy-MMM].&[202209]}
) ON COLUMNS
FROM [MyCube])
Code above produces following result:
I do not have a need to show all YTD & ITD data, I need to show it only once - before the beginning of the period. Like this:
YTD & ITD are calculated from the May-2022 numbers minus May's sales.
In this example:
"Dep1 ITD": 538 = 543 - 5
"Dep2 ITD": 288 = 292 - 4
"Dep1 YTD": 23 = 28 - 5
"Dep2 YTD": 14 = 18 - 4
Is it possible to do with MDX?
If so, how complicated it would be?
Will it be faster than extract two separate data sets and link them in a front-end application?

SQL- How to get value from a different table without joining to use on a equation?

I have a table with total user count of a company by their divisions, and I have another table with total spend on multiple different products, I want to identify the per capita spend value for each product by dividing each total spend on a product by the total use count from all the divisions.
Division
User Count
A
10
B
20
C
20
D
50
Total
100
Product Table,
Product
Total Spend
Apple
670
Orange
580
Grapes
640
Tomato
1050
End result should be ,
Product
Total Spend
Apple
6.7
Orange
5.8
Grapes
6.4
Tomato
10.5
Since there is nothing common among these tables to join, I need a way to get the total of the column of User count and use it in an equation in the query. It has to be dynamic so that even if the user count changes it will reflect on the per capita spend.
I'm using Zoho Analytics to do my online queries.
You can just aggregate before joining:
select p.*, p.spend * 1.0 / d.user_count
from product p cross join
(select sum(user_count) as user_count
from divisions
) d;

Possible to keep fraction in a query?

I am looking for a way to add up averages in SQL. Here is an example of the data I have:
product avg_price
phone 104.28
car 1000.00
And I'm looking to build something like this:
product avg_price
[all] 544.27
phone 104.28
car 1000.00
The way I'm currently doing it is to store the count and sum in two different columns, such as:
product cnt total
phone 203 20,304.32
car 404 304,323.30
And from that get the average. However, I was wondering if it is possible in SQL to just 'keep the fraction' and be able to add them as needed. For example:
product avg_price
[all] [add the fractions]
phone 20,304.32 / 203
car 304,323.30 / 404
Or do I need to use two columns in order to get an average of multiple aggregated rows?
You don't need 2 columns to get the average, but if you want to display as a fraction then you will need both numbers. They don't need to be in 2 columns though.
select product, sum(total) ||'/'||sum(count)
from table a
join table b on a.product=b.product
union
select product, total ||'/'||count
from table a
join table b on a.product=b.product;

MDX formula for calculating All-Commodity Volume (%ACV)

I would like to create a measure for the All-Commodity Volume (ACV) indicator. The formula is the following :
Total Sales of Resellers Who Sold a Product / Total Sales of All Resellers
Let's take an example, for instance the product "LL Touring Frame - Blue, 44" from AdventureWorks2012DW Cube.
We can see it is sold by 14 resellers
These 14 resellers together make 2 706 909 $ of the sales
If I take all the resellers, they make 80 450 596 $ of the sales
My ACV measure would return 2 607 909 / 80 450 596 = 2% for the product "LL Touring Frame - Blue, 44", which means it present at the resellers who make 2% of my indirect sales.
What I'm looking for is the MDX expression which takes the "Reseller Sales Amount for all products" instead of the reseller sales amount for the selected product, but only if the "Reseller Sales Amount of the selected product is not null".
So far, thanks the help of the community I could make a query which gives me the correct result if I filter on some specific products :
WITH SET ActiveStores As NonEmpty(
[DimStore].[Store].[Store]
, { ([DimProduct].[Product].CurrentMember, [Measures].[Sales])}
)
MEMBER ACV AS SUM(ActiveStores, [Measures].[Store Sales]) / [Measures].[Store Sales]
SELECT NON EMPTY ACV ON COLUMNS,
NON EMPTY { ([DimProduct].[Product].[Product].ALLMEMBERS ) } ON ROWS
FROM ( SELECT ( { [DimProduct].[Product].&[ HAMAC DE VOYAGE] } ) ON COLUMNS FROM [Cube])
This query returns an ACV of 9% for the filtered product. However, if I remove the filter by putting only FROM [Cube], the ACV becomes 100% for all the products, even the one which was 9% with the previous query.
It is like the ActiveStores set is binded with the WHERE clause but not the ROWS Axis.
Regards
To find members where Sales is not null you can use the NonEmpty function:
NonEmpty(
[Stores].[Stores].members
,(
[Product].[Product].[ProductA]
, [Measures].[Sales]
)
)
What about:
Sum(
[Store].[Store].[Store].Members,
IIF(
IsEmpty([Measures].[Sales]),
Null,
(
ROOT([Product])
,ROOT([Supplier])
,ROOT([Promotion])
,ROOT([Misc])
,[Measures].[Sales]
)
)
)
I could find a solution this way :
I have added an aggregated fact (Store Sales) as an attribute of dimension Store. Created a Measure Store Sales out of it, then created a calculated measure with the following expression :
SUM(NonEmpty(
[DimStore].[Store].[Store]
,([Measures].[Sales])
), [Measures].[Store Sales]) / (ROOT([DimStore]), [Measures].[Store Sales])

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