How do I reformat a measure in a mdx query - sql

I have an MDX query running on SSAS instance. The amount that gets displayed as a currency with spaces and , in the column. eg R 1 200,10.
I do not want to change anything on the data warehouse but want to change the format in the MDX query so that the results are displayed in the column as 1200.10 (a straight numeric)
I have included the following code on top of the exiting MDX query that give the wrong format.
with
member [Measures].[Gross Amt Exc Base] as [Measures].[AMT] , FORMAT_STRING="###0.00"
This is the code i have created:
with
member [Measures].[Gross Amt Exc Base] as [Measures].[AMT] , FORMAT_STRING="###0.00"
SELECT NON EMPTY { [Measures].[Gross Amt Exc Base] } ON COLUMNS,
NON EMPTY {(StrToMember('[Tran Date].[Actual Date].&[' + Format(Now()-1,'yyyy-MM-dd') + 'T00:00:00]')
:StrToMember('[Tran Date].[Actual Date].&[' + Format(Now()-3,'yyyy-MM-dd') + 'T00:00:00]') *
[Dim Store].[Store Number].[Store Number].ALLMEMBERS *
[Dim SKU].[SKU Code].[SKU Code].ALLMEMBERS *
[Dim SKU].[Product Code].[Product Code].ALLMEMBERS ) } DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS
FROM [Dolfin Dwh FTS] CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS
I want to see 1200.10 but get the following error message
"Executing the query ...
The 'Gross Amt Exc Base' calculated member cannot be created because a member with the same name already exists.
Run complete"
Please can some one help me.

Related

Number or Results and WITH MEMBER Calculation interacting weird

In our MDX Queries, we use a template like the following WITH MEMBER . SELECT SUBSET . FROM (SELECT . FROM .) a lot.
What this particular query should do (and does!) is list all product variants matching a filter, and give the sales amount for it for a month in time. On the same row, we have the sales total of all variants of this product.
We need only a subset of the result (for paging), and the results need to ordered to allow paging of course. Then there is a non empty, so only product variants that were sold in the time period qualify.
This works just fine. Paging also works: putting 30, 60, 90 for Start in SUBSET is gives the correct results.
However, if I put a 1 as Count (SUBSET(..., x, 1)) the result of the "Price all" goes to (null) for each and any row I select with x. If I return two or more rows it works just fine, and all rows have their correct values.
Two results and the correct value in the rightmost column (in case you are wondering how the total of all variants, 67, can be lower than the sales of one particular variant (280): I picked a bad example - there was a return (so a negative sale) for another variant of this product, for 213. The 67 is correct and desired!)
One Row and a (null) result
I do not understand how or why the SUBSET count would interfere with the Tuple I select in the WITH MEMBER. If it does it for 30 rows at a time, why not for one?
*edit
I just tried, what would happen if I filter in a way that only one result matches (and SUBSET paging is set to "working values" of 0, 30): The same as in the 2nd screenshot, for one result row the WITH MEMBER evaluates to (null).
So it is not that SUBSET is the problem, but more general: When there is only one result row!
Could this be a bug in our SQL Server Version? It is SQL 2012 with SP4 and all latest security patches.
WITH
MEMBER [DIM Date].[HI Year_Month_Day].[CURRENT_MONTH] AS
[DIM Date].[HI Year_Month_Day].&[20201215].Parent /* Select Month of December */
MEMBER [MEASURES].[Price all] AS (
(
[DIM Products].[HI Products].Currentmember.Parent
, [DIM Products].[HI Subproduct].[Total]
)
, [MEASURES].[Price]
), FORMAT_STRING = '#,0' /* Get the parent product total next to every subproduct */
SELECT {
([DIM Datum].[HI Year_Month_Day].[CURRENT_MONTH], [MEASURES].[Price])
, ([DIM Datum].[HI Year_Month_Day].[CURRENT_MONTH], [MEASURES].[Price All])
} ON COLUMNS
, {
SUBSET(
ORDER(
NONEMPTY(
([DIM Products].[HI Produt Subproduct].[Subproduct])
, {([DIM Date].[HI Year_Month_Day].[CURRENT_MONTH], [MEASURES].[Price])}
)
, ([DIM Date].[HI Year_Month_Day].[CURRENT_MONTH], [MEASURES].[Price])
, BDESC
)
, 0
, 30
)
} ON ROWS
FROM (
SELECT
{[DIM StoreLocation].[HI Country].[Country].&[US]}
* {[DIM Productgroup].[HI Productgroup].[Productgroup].&[Bikes]}
ON COLUMNS
FROM [Cube]
)

Using WITH in mdx to retrieve row names

I am executing the following MDX query in SSMS, which I have got from Profiler:
SELECT {
[Measures].[Dollar Amount],
[Measures].[Transaction Count]}
DIMENSION PROPERTIES PARENT_UNIQUE_NAME,HIERARCHY_UNIQUE_NAME ON COLUMNS,
NON EMPTY Hierarchize({DrilldownLevel({[Retail Sales Date].[Month].[All]},,,INCLUDE_CALC_MEMBERS)})
DIMENSION PROPERTIES PARENT_UNIQUE_NAME,HIERARCHY_UNIQUE_NAME ON ROWS
FROM [Retail Sales Cube]
WHERE ([Geography].[Retail Sales Location].[Country Name].&[Australia],[Retail Category].[Retail Category].[All])
CELL PROPERTIES VALUE, FORMAT_STRING, LANGUAGE, BACK_COLOR, FORE_COLOR, FONT_FLAGS
This produces the following results (snippet only):
The rows are months, and because of the method of retrieval, I need the months to also be displayed in a column.
So I tried this:
WITH
MEMBER [TheDate] AS [Retail Sales Date].[Month].CurrentMember.Name
SELECT {
[TheDate],
[Measures].[Dollar Amount],
[Measures].[Transaction Count]}
DIMENSION PROPERTIES PARENT_UNIQUE_NAME,HIERARCHY_UNIQUE_NAME ON COLUMNS,
NON EMPTY Hierarchize({DrilldownLevel({[Retail Sales Date].[Month].[All]},,,INCLUDE_CALC_MEMBERS)})
DIMENSION PROPERTIES PARENT_UNIQUE_NAME,HIERARCHY_UNIQUE_NAME ON ROWS
FROM [Retail Sales Cube]
WHERE ([Geography].[Retail Sales Location].[Country Name].&[Australia],[Retail Category].[Retail Category].[All])
CELL PROPERTIES VALUE, FORMAT_STRING, LANGUAGE, BACK_COLOR, FORE_COLOR, FONT_FLAGS
which would be fine, except that it also shows rows where there are null values, as follows:
I know there is NONEMPTY, but I don't know where I would put it. How can I get rid of the empty rows?
Perhaps the following expression would do the trick:
WITH
MEMBER [TheDate] AS
IIF( !isEmpty( [Dollar Amount] )
[Retail Sales Date].[Month].CurrentMember.Name,
null
)

Calculated measure hurting performance and returning many more rows

I have the following query which should be returning income split by country and date. It should also be including a count of the number of dates in the grouping. This would be useful for a calculation looking at an entire month of income/days. I'm finding that the calculated measure is causing all countries in the dimension to be returned regardless of if there's any income data for it. Is there any way to limit this so only countries with income are returned?
with
MEMBER [Measures].[group_day_count] as
COUNT(
Descendants(
[Date].[Date].currentmember, [Date].[Date].[Date]
)
)
SELECT NON EMPTY Hierarchize({DrilldownLevel({[Date].[Date].[All]},,,INCLUDE_CALC_MEMBERS)})
DIMENSION PROPERTIES PARENT_UNIQUE_NAME,HIERARCHY_UNIQUE_NAME,
[Date].[Date].[Date].[Date Sort] ON COLUMNS ,
NON EMPTY CrossJoin(Hierarchize({DrilldownLevel({[Source Location].[Country Code].[All]},,,INCLUDE_CALC_MEMBERS)}),
{[Measures].[group_day_count],[Measures].[income]})
DIMENSION PROPERTIES PARENT_UNIQUE_NAME,HIERARCHY_UNIQUE_NAME ON ROWS
FROM (SELECT ({[Date].[Date].&[2017-10-04], [Date].[Date].&[2017-10-05], [Date].[Date].&[2017-10-06], [Date].[Date].&[2017-10-07], [Date].[Date].&[2017-10-08], [Date].[Date].&[2017-10-09], [Date].[Date].&[2017-10-10]})
ON COLUMNS FROM [Placeholder])
CELL PROPERTIES VALUE, FORMAT_STRING, LANGUAGE, BACK_COLOR, FORE_COLOR, FONT_FLAGS
Try:
IIF([Measures].[Income],[Measures].[Days],NULL)

Query to only display matching elements in two dimensions

Short question:
Any idea how I can pull the name from one dimension entry and apply it to another as filter?
Long description: My cube has has dimensions for manufacturer and seller, and I only need sales for "themselves". The dimensions are loaded from distinct tables in the source but have entries with the exact same name.
Manufacturer Seller
A A *
A D
B B *
C C *
C A
C D
A, B and C sell their products themselves, A and C also sell through reseller D. C also sells through A. I'd like to filter for the marked rows only (names matching).
In SQL this would be simple, or the cube might be easily enhanced to flag "own sales", but both options are not available. I need to query the cube "as is".
I tried using STRTOMEMBER and CURRENTMEMBER like so (with NAME and MEMBER_NAME):
STRTOMEMBER("[Dim Seller].[Seller].[" + [Dim Producer].[Producer].CURRENTMEMBER.NAME + "]
That actually works, syntactically, but CURRENTMEMBER seems to always evaluate to ALL and delivers the (correct) measure value for the ALL element yet not the one for matching name.
I also tried to create a WHERE setting the two names equal
[Dim Seller].[Seller].CURRENTMEMBER.NAME = [Dim Producer].[Producer].CURRENTMEMBER.NAME
but that is very SQL and not valid in MDX.
The whole query looks like this:
SELECT
NON EMPTY {
[Measures].[Value]
} ON COLUMNS
, NON EMPTY { (
{
[Dim Producer].[Producer].[A]
, [Dim Producer].[Producer].[B]
, [Dim Producer].[Producer].[C]
}
* [Dim Seller].[Seller].[Seller].ALLMEMBERS
// This line needs to be trimmed to same name as producer
) } ON ROWS
FROM
[Cube]
The producer list is coming from a config file and string-concatenated to the query and subject to change. Having a second seller list to be kept in sync would be a burden but also (when C sells through A) deliver wrong results.
Any idea how I can pull the name from one dimension entry and apply it to another?
The HAVING clause might help you:
SELECT
NON EMPTY {
[Measures].[Value]
} ON COLUMNS
, NON EMPTY {
{
[Dim Producer].[Producer].[A]
, [Dim Producer].[Producer].[B]
, [Dim Producer].[Producer].[C]
}
* [Dim Seller].[Seller].[Seller].ALLMEMBERS
}
HAVING
[Dim Seller].[Seller].CURRENTMEMBER.MEMBER_NAME =
[Dim Producer].[Producer].CURRENTMEMBER.MEMBER_NAME
ON ROWS
FROM
[Cube]
Use the GENERATE function to do the job for you.
with set Producers as
{[Dim Producer].[Producer].[A]
,[Dim Producer].[Producer].[B]
,[Dim Producer].[Producer].[C]}
set Sellers as
[Dim Seller].[Seller].members
//For every producer, get the seller who shares the same name.
set ProducersHavingSameNameAsSellers as
GENERATE(Producers, GENERATE(Sellers, filter
(Sellers,
[Dim Seller].[Seller].CURRENTMEMBER.MEMBER_NAME =
[Dim Producer].[Producer].CURRENTMEMBER.MEMBER_NAME
)
)
)
SELECT
NON EMPTY [Measures].[Value] ON COLUMNS,
ProducersHavingSameNameAsSellers ON ROWS
FROM [Cube]

SSAS: Percent of Total not working in hierarchy

I have a simple olap cube - one set of measures and some unexciting dimensions.
I've add one calculation to get the "percent of total" sales against the gross sales measure. The code for this calculation is:
([Dim Stores].[Store Name].CurrentMember, [Measures].[Gross Sales])
/
([Dim Stores].[Store Name].Parent, [Measures].[Gross Sales])
This works.
Within the store dimension, there is a hierarchy called 'By State' where the stores are contained within.
Two questions please:
1. Any idea why the calculation would not work when I use the the 'By state' hierarchy i.e. the same calculation grouped by the next level up?
The state problem aside, any idea why my grand total shows an error even when I just use the Store Name?
TIA!
In poking around, I found a template within the "calculation tools" called "Percentage of Total". Using it, I translated my calculation to this:
Case
// Test to avoid division by zero.
When IsEmpty
(
[Measures].[Gross Sales]
)
Then Null
Else ( [Dim Stores].[By State].CurrentMember, [Measures].[Gross Sales] )
/
(
// The Root function returns the (All) value for the target dimension.
Root
(
[Dim Stores]
),
[Measures].[Gross Sales]
)
End
It worked!