Adding a Total column to MDX - sql

I have the following query that gives me an extract (I am putting this into a CSV from my cube).
I want to show the Total of all NRx in a column as the first column NRx and the rest of the NRx columns should come up as they do now. I tried to add a SUM (Measures.NRx) to this, but it did not work
How do I get a Total NRx column?
SELECT
NON EMPTY
CrossJoin
(
[Time Period].[Calendar].[Cycle Dt],
{
[Measures].[NRx]
}
)
ON COLUMNS,
NON EMPTY
{(
[Prescriber].[Prescriber Name].[Prescriber Name].Members
, [Prototype Alignment].[Tier Code].[Lilly Tier Code].Members
, [Prototype Alignment].[Territory Name].[Territory Name].Members
, [Prototype Alignment].[District Name].[District Name].Members
, [Prototype Alignment].[Division Name].[Division Name].Members
, [Prototype Alignment].[Area Name].[Area Name].Members
, [Market Product].[Product Group Nbr].[Product Group Nbr].Members
, [Market Product].[Product G Name Name].[Product G Name Name].Members
)} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS
FROM
(
SELECT
({
[Prescriber].[Ims Prescriber Id].&[011111]
}
,
{
[Time Period].[Cycle Dt].&[2008-10-03T00:00:00],
[Time Period].[Cycle Dt].&[2008-10-10T00:00:00],
[Time Period].[Cycle Dt].&[2008-10-17T00:00:00],
[Time Period].[Cycle Dt].&[2008-10-24T00:00:00],
[Time Period].[Cycle Dt].&[2008-10-31T00:00:00],
[Time Period].[Cycle Dt].&[2008-11-07T00:00:00],
[Time Period].[Cycle Dt].&[2008-11-14T00:00:00],
[Time Period].[Cycle Dt].&[2008-11-21T00:00:00],
[Time Period].[Cycle Dt].&[2008-11-28T00:00:00],
[Time Period].[Cycle Dt].&[2008-12-05T00:00:00],
[Time Period].[Cycle Dt].&[2008-12-12T00:00:00],
[Time Period].[Cycle Dt].&[2008-12-12T00:00:00],
[Time Period].[Cycle Dt].&[2008-12-19T00:00:00],
[Time Period].[Cycle Dt].&[2008-12-26T00:00:00],
[Time Period].[Cycle Dt].&[2008-12-26T00:00:00]
}
) ON COLUMNS
FROM [Mart]
) CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE,
FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS

What if you made a calculated member before the SELECT query with something like this:
SUM ([Measures].[NRx], [Time Period].[Calendar].[Cycle Dt])?
Then just do:
SELECT
NON EMPTY [MYCALCMEMBER_SUM] * CrossJoin([Time Period].[Calendar].[Cycle Dt], {[Measures].[NRx]}) ON COLUMNS,

Related

concatenate columns based on blank columns

I am having three columns,- and / should be used as separator result should come in new column(File Index-sub Index/Year),if sub index is blank then result should be (File Index/Year).
SELECT
[ File Index].[File Index]
, [ File Index].[Sub Index]
, [ File Index].[Financial Year]
, [ File Index].[File Index] & [Sub Index] & [Financial Year] AS [Composite Index]
FROM [File Index];
Since you have both MySQL and MS Access listed, here are solutions for both.
For MySQL, you can use a CASE statement for this:
SELECT [File Index], [Sub Index], [Financial Year],
CASE WHEN [Sub Index] IS NOT NULL
THEN Concat([File Index], '-', [Sub Index], '/', [Financial Year])
ELSE Concat([File Index], '/', [Financial Year])
END as [Composite Index]
FROM [File Index];
For MS Access, you will use Switch(...):
SELECT [File Index], [Sub Index], [Financial Year],
Switch(Not IsNull([Sub Index]),
[File Index] & '-' & [Sub Index] & '/' & [Financial Year],
IsNull([Sub Index]),
[File Index] & '/' & [Financial Year]
) as [Composite Index]
FROM [File Index];
You can use IIF function: IIF(Condition;ConcatenationIfTrue;ConcatenationIfFalse)
SELECT
[ File Index].[File Index]
, [ File Index].[Sub Index]
, [ File Index].[Financial Year]
, IIF(ISNULL([Sub Index];[ File Index].[File Index] & "/" & [Financial Year];[ File Index].[File Index] & [Sub Index] & [Financial Year]) AS [Composite Index]
FROM [File Index];

wrong Total using case-when in MDX

If I use the following Negative total instead of previous one i am getting the same output but still not my expected output. Is there anyone to help me out?
MEMBER [Measures].[Negative Total] AS
CASE
WHEN ([Measures].[Total],[Dim Account].[Account Marketing].&[Income]&[Marketing]) < 0
THEN [Measures].[Total]*-1
WHEN ([Measures].[Total],[Dim Account].[Account Marketing].&[Income]&[Marketing]) > 0
THEN 0
END
Let me explain what I am trying to achieve. I have a Dimension of [Dim Account] which has the attributes like [Account Type],[Account Activity], [Account Marketing] etc. [Account Type] is income, expense,.. [Account Activity] is like Advertising,Discounts, Sale of sponsorship,etc [Account Marketing] is the combination of those things I mean advertising is the expense whereas discounts is stored as the negative income, so ultimately discounts will be treated as marketing expense That's why my member [Negative Total] is converting the income multiplied by -1 and i want to exclude those income are positive as they are income. I want to display only those converted income and finally add up them. In my MDX, the data are displayed perfectly but added up the original value -43380.4 +18 - 9181.32= -52542.81 that was supposed to be 43380.4 +0 +9181.32= 52561.72. For comparing the original total with expected total I displayed [Measure].[Total] and [Measure].[Negative Total] concurrently. Help me please if you know the trick
WITH
MEMBER [Measures].[Negative Total] AS
CASE
WHEN
[Dim Account].[Account Marketing].CurrentMember.MEMBER_CAPTION="Marketing" AND
[Dim Account].[Account Marketing].Properties("Account Type") ="Income" AND
[Measures].[Total]<1
THEN [Measures].[Total]*-1
WHEN
[Dim Account].[Account Marketing].CurrentMember.MEMBER_CAPTION="Marketing" AND
[Dim Account].[Account Marketing].Properties("Account Type") ="Income" AND
[Measures].[Total]>1
THEN 0
END
SELECT
{
[Measures].[Total],[Measures].[Negative Total]} on COLUMNS
,
NON EMPTY
(
{
(
[Dim Account].[HierarchyMarketing].[Account Marketing].&[Income]&[Marketing]
*
{
[Dim Account].[Account Activity].[Account Activity].MEMBERS,
VISUALTOTALS([Dim Account].[Account Activity].[All])
}
)
}
,
[Dim Branch].[Trading As].&[BAR]&[Barlens Event Hire] ,
[Dim Scenario].[Scenario Name].&[Actual] ,
[Dim Fiscal Year].[HierarchyFiscal].[E Month].&[2016]&[December]
)
on ROWS
FROM [CubeProfitLoss]
It is working as expected since [All] too is a member.
To achieve what you want, add a base measure that gets the negative total and then use it in the final calculation.
MEMBER [Measures].[Negative Total Test] AS
CASE
WHEN ([Measures].[Total],[Dim Account].[Account Marketing].&[Income]&[Marketing]) < 0
THEN [Measures].[Total]*-1
WHEN ([Measures].[Total],[Dim Account].[Account Marketing].&[Income]&[Marketing]) > 0
THEN 0
END
MEMBER [Measures].[Negative Total] AS
CASE
WHEN [Dim Account].[Account Activity].CURRENTMEMBER IS [Dim Account].[Account Activity].[All]
SUM(
[Dim Account].[Account Activity].[Account Activity].MEMBERS,
[Measures].[Negative Total Test]
)
ELSE [Measures].[Negative Total Test]
END
(Just to add to Sourav's nice solution)
CASE and IIF are both slow - IIF can be faster in some circumstances, usually it is faster choice if one of its branches can be NULL:
MEMBER [Measures].[Negative Total Test] AS
IIF(
(
[Measures].[Total]
,[Dim Account].[Account Marketing].&[Income]&[Marketing]
) >= 0
,NULL
,[Measures].[Total] * -1
)
MEMBER [Measures].[Negative Total] AS
IIF(
[Dim Account].[Account Activity].CURRENTMEMBER
IS [Dim Account].[Account Activity].[All]
,SUM(
[Dim Account].[Account Activity].[Account Activity].MEMBERS,
[Measures].[Negative Total Test]
)
,[Measures].[Negative Total Test]
)
According to the screenshot there is no need to include Marketing in the first calc so a further simplification could be:
MEMBER [Measures].[Negative Total Test] AS
IIF(
[Measures].[Total] >= 0
,NULL
,[Measures].[Total] * -1
)
MEMBER [Measures].[Negative Total] AS
IIF(
[Dim Account].[Account Activity].CURRENTMEMBER
IS [Dim Account].[Account Activity].[All]
,SUM(
[Dim Account].[Account Activity].[Account Activity].MEMBERS,
[Measures].[Negative Total Test]
)
,[Measures].[Negative Total Test]
)
WOW ! I got my answers, that's the thing I was looking for. It was nothing but only changes is the display of [Negative Total Test]. I again thanks to Saurav because he gave me life of my MDX code.
WITH
MEMBER [Measures].[Negative Total Test] AS
CASE
WHEN
[Dim Account].[Account Marketing].CurrentMember.MEMBER_CAPTION="Marketing" AND
[Dim Account].[Account Marketing].Properties("Account Type") ="Income" AND
[Measures].[Total]<1
THEN [Measures].[Total]*-1
WHEN
[Dim Account].[Account Marketing].CurrentMember.MEMBER_CAPTION="Marketing" AND
[Dim Account].[Account Marketing].Properties("Account Type") ="Income" AND
[Measures].[Total]>1
THEN NULL
ELSE
[Measures].[Total]
END
MEMBER [Measures].[Negative Total] AS
CASE
WHEN [Dim Account].[Account Activity].CURRENTMEMBER IS [Dim Account].[Account Activity].[All]
THEN
SUM(
[Dim Account].[Account Activity].[Account Activity].MEMBERS,
[Measures].[Negative Total Test]
)
ELSE [Measures].[Negative Total Test]
END
MEMBER [Dim Account].[HierarchyMarketing].[Income] AS
([Dim Account].[HierarchyMarketing].[Account Type].&[Income], [Measures].[Negative Total])
MEMBER [Dim Account].[HierarchyMarketing].[Total Marketing Expense] AS
AGGREGATE
(
{
[Dim Account].[Account Marketing].&[Income]&[Marketing]
,
[Dim Account].[Account Marketing].&[Expenses]&[Marketing]
}
,
[Measures].[Negative Total]
)
SELECT
{
//[Measures].[Total], [Measures].[Negative Total],
[Measures].[Negative Total Test]
} on COLUMNS
,
NON EMPTY
(
{
(
[Dim Account].[HierarchyMarketing].[Account Type].&[Income]
*
{
[Dim Account].[Account Activity].[All]
}
)
,
(
{
[Dim Account].[HierarchyMarketing].[Account Marketing].&[Expenses]&[Marketing]
,
[Dim Account].[HierarchyMarketing].[Account Marketing].&[Income]&[Marketing]
}
*
[Dim Account].[Account Activity].[Account Activity].MEMBERS
)
,
(
[Dim Account].[HierarchyMarketing].[Total Marketing Expense]
,
[Dim Account].[Account Activity].[All]
)
}
,
// Traders,
[Dim Branch].[Trading As].&[BAR]&[Barlens Event Hire] ,
[Dim Scenario].[Scenario Name].&[Actual] ,
[Dim Fiscal Year].[HierarchyFiscal].[E Month].&[2016]&[December]
)
on ROWS
FROM [CubeProfitLoss]
;
I should have asked as an another question however I have reached my question limit, sorry for that.
The above question was the part of my problem. The final output should be like my edited picture. According to Sourav's solution, he checked the condition of [Dim Account].[Account Activity].CURRENTMEMBER IS [Dim Account].[Account Activity].[All] that worked perfectly that tiny problem but when I wanted to accumulate with original income (650,920.88) then it's coming up with big figure that's not expected. Another solution I need I want to add up marketing expense(24010.44) with negative marketing income which are already converted (43380.4+9181.32) so finally the result should be 76572.16. Please modify my MDX. Thanks a lot
WITH
//MEMBER [Measures].[Negative Total Test] AS -- (1)
//CASE
// WHEN ([Measures].[Total],[Dim Account].[Account Marketing].&[Income]&[Marketing]) < 0
// THEN [Measures].[Total]*-1
// WHEN ([Measures].[Total],[Dim Account].[Account Marketing].&[Income]&[Marketing]) > 0
// THEN NULL
// ELSE
// [Measures].[Total]
//END
MEMBER [Measures].[Negative Total Test] AS --(2)
CASE
WHEN
[Dim Account].[Account Marketing].CurrentMember.MEMBER_CAPTION="Marketing" AND
[Dim Account].[Account Marketing].Properties("Account Type") ="Income" AND
[Measures].[Total]<1
THEN [Measures].[Total]*-1
WHEN
[Dim Account].[Account Marketing].CurrentMember.MEMBER_CAPTION="Marketing" AND
[Dim Account].[Account Marketing].Properties("Account Type") ="Income" AND
[Measures].[Total]>1
THEN NULL
ELSE
[Measures].[Total]
END
MEMBER [Measures].[Negative Total] AS
CASE
WHEN [Dim Account].[Account Activity].CURRENTMEMBER IS [Dim Account].[Account Activity].[All]
THEN
SUM(
[Dim Account].[Account Activity].[Account Activity].MEMBERS,
[Measures].[Negative Total Test]
)
ELSE [Measures].[Negative Total Test]
END
//MEMBER [Dim Account].[Account Activity].[TOTAL] AS
// [Dim Account].[Account Activity].[All]
MEMBER [Dim Account].[HierarchyMarketing].[Total Marketing Expense] AS
AGGREGATE
(
{
[Dim Account].[Account Marketing].&[Income]&[Marketing]
,
[Dim Account].[Account Marketing].&[Expenses]&[Marketing]
}
,
[Measures].[Negative Total Test]
)
SELECT
{
[Measures].[Total], [Measures].[Negative Total]
} on COLUMNS
,
NON EMPTY
(
{
(
[Dim Account].[HierarchyMarketing].[Account Type].&[Income]
*
{
VisualTotals([Dim Account].[Account Activity].[All])
}
)
,
(
{
[Dim Account].[HierarchyMarketing].[Account Marketing].&[Expenses]&[Marketing]
,
[Dim Account].[HierarchyMarketing].[Account Marketing].&[Income]&[Marketing]
}
*
[Dim Account].[Account Activity].[Account Activity].MEMBERS
)
,
(
[Dim Account].[HierarchyMarketing].[Total Marketing Expense]
,
VISUALTOTALS([Dim Account].[Account Activity].[All])
)
}
,
[Dim Branch].[Trading As].&[BAR]&[Barlens Event Hire] ,
[Dim Scenario].[Scenario Name].&[Actual] ,
[Dim Fiscal Year].[HierarchyFiscal].[E Month].&[2016]&[December]
)
on ROWS
FROM [CubeProfitLoss]

Set of Days in past 6 equivalent MTDs

Imagine today it is the 22nd June 2010 (I've used this date as AdvWrks cube is old)
I would like to find the set of days that make up this month i.e. 1st June - 22nd June plus the days in the previous 5 equivalent months to date.
So these days would make up the set:
(1st Jan 2010 - 22nd Jan 2010) +
(1st Feb 2010 - 22nd Feb 2010) +
(1st Mar 2010 - 22nd Mar 2010) +
(1st Apr 2010 - 22nd Apr 2010) +
(1st May 2010 - 22nd May 2010) +
(1st Jun 2010 - 22nd Jun 2010)
The following gives me this set of 132 days:
WITH
SET [Days in Current Month] AS
[Date].[Calendar].[Date].&[20100601]:[Date].[Calendar].[Date].&[20100622]
SET [Mths in Past 6 Mths] AS
Tail
(
[Date].[Calendar].[Month].MEMBERS
,6
)
SET [Prev Equiv MTDs] AS
Generate
(
[Mths in Past 6 Mths]
,Head
(
Descendants
(
[Mths in Past 6 Mths].CurrentMember
,[Date].[Calendar].[Date]
,SELF
)
,[Days in Current Month].Count
)
)
SELECT
{} ON 0
,[Prev Equiv MTDs] ON 1
FROM [Adventure Works];
Is it possible to find this set of days without using the Generate function?
Here is one way:
WITH SET FirstDateOfMonth AS [Date].[Calendar].[Date].&[20100622].Parent.FirstChild
SET LastDayOfMonth AS [Date].[Calendar].[Date].&[20100622]
SELECT {} ON 0,
//CurrentMonth
{
FirstDateOfMonth.ITEM(0)
:
LastDayOfMonth.ITEM(0)
}
+
//-1 month
{
ParallelPeriod(
[Date].[Calendar].[Month],
1,
FirstDateOfMonth.ITEM(0)
)
:
ParallelPeriod(
[Date].[Calendar].[Month],
1,
LastDayOfMonth.ITEM(0)
)
}
+
//-2 month
{
ParallelPeriod(
[Date].[Calendar].[Month],
2,
FirstDateOfMonth.ITEM(0)
)
:
ParallelPeriod(
[Date].[Calendar].[Month],
2,
LastDayOfMonth.ITEM(0)
)
}
+
//-3 month
{
ParallelPeriod(
[Date].[Calendar].[Month],
3,
FirstDateOfMonth.ITEM(0)
)
:
ParallelPeriod(
[Date].[Calendar].[Month],
3,
LastDayOfMonth.ITEM(0)
)
}
+
//-4 month
{
ParallelPeriod(
[Date].[Calendar].[Month],
4,
FirstDateOfMonth.ITEM(0)
)
:
ParallelPeriod(
[Date].[Calendar].[Month],
4,
LastDayOfMonth.ITEM(0)
)
}
+
//-5 month
{
ParallelPeriod(
[Date].[Calendar].[Month],
5,
FirstDateOfMonth.ITEM(0)
)
:
ParallelPeriod(
[Date].[Calendar].[Month],
5,
LastDayOfMonth.ITEM(0)
)
}
ON 1
FROM [Adventure Works]
And here is one more:
SELECT
{
[Date].[Calendar].[Date].&[20100622].PARENT.FirstChild
:
COUSIN
(
[Date].[Calendar].[Date].&[20100622],
[Date].[Calendar].[Date].&[20100622].PARENT
)
}
+
{
[Date].[Calendar].[Date].&[20100622].PARENT.LAG(1).FirstChild
:
COUSIN
(
[Date].[Calendar].[Date].&[20100622],
[Date].[Calendar].[Date].&[20100622].PARENT.LAG(1)
)
}
+
{
[Date].[Calendar].[Date].&[20100622].PARENT.LAG(2).FirstChild
:
COUSIN
(
[Date].[Calendar].[Date].&[20100622],
[Date].[Calendar].[Date].&[20100622].PARENT.LAG(2)
)
}
+
{
[Date].[Calendar].[Date].&[20100622].PARENT.LAG(3).FirstChild
:
COUSIN
(
[Date].[Calendar].[Date].&[20100622],
[Date].[Calendar].[Date].&[20100622].PARENT.LAG(3)
)
}
+
{
[Date].[Calendar].[Date].&[20100622].PARENT.LAG(4).FirstChild
:
COUSIN
(
[Date].[Calendar].[Date].&[20100622],
[Date].[Calendar].[Date].&[20100622].PARENT.LAG(4)
)
}
+
{
[Date].[Calendar].[Date].&[20100622].PARENT.LAG(5).FirstChild
:
COUSIN
(
[Date].[Calendar].[Date].&[20100622],
[Date].[Calendar].[Date].&[20100622].PARENT.LAG(5)
)
}
ON 1,
{} ON 0
FROM [Adventure Works]
And one more...
WITH SET FirstDateOfMonth AS [Date].[Calendar].[Date].&[20100622].Parent.FirstChild
SET LastDayOfMonth AS [Date].[Calendar].[Date].&[20100622]
MEMBER Measures.PositionOfDate AS {FirstDateOfMonth.ITEM(0):LastDayOfMonth.ITEM(0)}.COUNT
SET Dates AS
HEAD(
[Date].[Calendar].[Date].&[20100622].Parent.ITEM(0).CHILDREN,
Measures.PositionOfDate
)
+
HEAD(
[Date].[Calendar].[Date].&[20100622].Parent.LAG(1).ITEM(0).CHILDREN,
Measures.PositionOfDate
)
+
HEAD(
[Date].[Calendar].[Date].&[20100622].Parent.LAG(2).ITEM(0).CHILDREN,
Measures.PositionOfDate
)
+
HEAD(
[Date].[Calendar].[Date].&[20100622].Parent.LAG(3).ITEM(0).CHILDREN,
Measures.PositionOfDate
)
+
HEAD(
[Date].[Calendar].[Date].&[20100622].Parent.LAG(4).ITEM(0).CHILDREN,
Measures.PositionOfDate
)
+
HEAD(
[Date].[Calendar].[Date].&[20100622].Parent.LAG(5).ITEM(0).CHILDREN,
Measures.PositionOfDate
)
SELECT Dates ON 0,
{} ON 1
FROM [Adventure Works]
And this way too:
WITH SET FirstDateOfSelectedMonth AS [Date].[Calendar].[Date].&[20100622].Parent.FirstChild
SET SelectedDate AS [Date].[Calendar].[Date].&[20100622]
MEMBER Measures.PositionOfDate AS {FirstDateOfSelectedMonth.ITEM(0):SelectedDate.ITEM(0)}.COUNT
SELECT
PeriodsToDate
(
[Date].[Calendar].[Month],
SelectedDate.ITEM(0).PARENT.lag(5).FirstChild.LEAD(Measures.PositionOfDate - 1)
)
+
PeriodsToDate
(
[Date].[Calendar].[Month],
SelectedDate.ITEM(0).PARENT.lag(4).FirstChild.LEAD(Measures.PositionOfDate - 1)
)
+
PeriodsToDate
(
[Date].[Calendar].[Month],
SelectedDate.ITEM(0).PARENT.lag(3).FirstChild.LEAD(Measures.PositionOfDate - 1)
)
+
PeriodsToDate
(
[Date].[Calendar].[Month],
SelectedDate.ITEM(0).PARENT.lag(2).FirstChild.LEAD(Measures.PositionOfDate - 1)
)
+
PeriodsToDate
(
[Date].[Calendar].[Month],
SelectedDate.ITEM(0).PARENT.lag(1).FirstChild.LEAD(Measures.PositionOfDate - 1)
)
+
PeriodsToDate
(
[Date].[Calendar].[Month],
SelectedDate.ITEM(0)
)
ON 1,
{} ON 0
FROM [Adventure Works]
EDIT
Slow code with non-repeating blocks:
WITH SET FirstDateOfSelectedMonth AS [Date].[Calendar].[Date].&[20100601].Parent.FirstChild
SET SelectedDate AS [Date].[Calendar].[Date].&[20100601]
MEMBER Measures.PositionOfDate AS {FirstDateOfSelectedMonth.ITEM(0):SelectedDate.ITEM(0)}.COUNT
MEMBER Measures.PositionOfCurrentDate AS
([Date].[Calendar].currentmember.Parent.FirstChild:[Date].[Calendar].currentmember).COUNT
SELECT
{
[Date].[Calendar].[Date].&[20100601].Parent.LAG(5).FirstChild.ITEM(0)
:
[Date].[Calendar].[Date].&[20100601]
}
HAVING Measures.PositionOfCurrentDate <= Measures.PositionOfDate
ON 1,
{} ON 0
FROM [Adventure Works]

Grouping ignoring blank fields

I am having a hard time to identify (and fix) the grouping issue I have with the following query. My data has some Country fields blank and the query is ignoring them, only those with non blank values are converted to rows. I tried removing the country grouping but I get the error "You tried to execute a query that does not include the specified expression "Country" as part of an aggregate function". This is the query I have in Access:
TRANSFORM Count([passed courses].[Course Number])
SELECT [domain].[Account ID], [domain].[Account Name], [passed courses].[Learner Email Address], [passed courses].Category, [passed courses].Country, [passed courses].[Earliest Start_Date], [passed courses].[Latest End_Date]
FROM [Domain Master] AS [domain] INNER JOIN (SELECT d3.ID, d3.[Learner Email Address], d3.[Course Number], d3.[Transcript Status], d3.Domain, c2.Category, [passed levels].Country, [passed levels].[Earliest Start_Date], [passed levels].[Latest End_Date]
FROM
(SELECT [completed courses].[Learner Email Address], [completed courses].[Level], [completed courses].Category, [completed courses].[Completed Count], [completed courses].Country, [learner dates].[Earliest Start_Date], [learner dates].[Latest End_Date]
FROM
(SELECT [courses taken].[Learner Email Address], [courses taken].[Level], [courses taken].Category, Count([courses taken].ID) AS [Completed Count], [courses taken].Country
FROM
(SELECT d1.ID, d1.[Learner Email Address], d1.[Course Number], c1.[Level], c1.Category, d1.[Transcript Status], d1.Country
FROM [Data] AS d1
INNER JOIN [Courses] AS c1 ON d1.[Course Number] = c1.[Course Number]
)
AS [courses taken]
WHERE [courses taken].[Transcript Status] = "Completed"
GROUP BY [courses taken].[Learner Email Address], [courses taken].Level, [courses taken].Category, d1.Country
)
AS [completed courses]
INNER JOIN
(SELECT d2.[Learner Email Address], d2.Country, Min(d2.Start_Date) AS [Earliest Start_Date], Max(d2.End_Date) AS [Latest End_Date]
FROM [Data] AS d2
GROUP BY d2.[Learner Email Address], d2.Country
) AS [learner dates]
ON [completed courses].[Learner Email Address] = [learner dates].[Learner Email Address] AND
[completed courses].Country = [learner dates].Country
WHERE
([courses taken].Category = 'Sales') AND (
(([completed courses].Level = 1) AND ([completed courses].[Completed Count] >=5)) OR
(([completed courses].Level = 2) AND ([completed courses].[Completed Count] >=5)) OR
(([completed courses].Level = 3) AND ([completed courses].[Completed Count] >=1))
) OR
([courses taken].Category = 'Purchase') AND (
(([completed courses].Level = 1) AND ([completed courses].[Completed Count] >=5)) OR
(([completed courses].Level = 2) AND ([completed courses].[Completed Count] >=4)) OR
(([completed courses].Level = 3) AND ([completed courses].[Completed Count] >=1))
)
)
AS [passed levels]
INNER JOIN ([Data] AS d3 INNER JOIN Courses AS c2 ON ((d3.[Course Number] = c2.[Course Number])))
ON ([passed levels].Level = c2.Level) AND ([passed levels].[Learner Email Address] = d3.[Learner Email Address]) AND ([passed levels].Category = c2.Category)
) AS [passed courses] ON [domain].[Partner Domain] = [passed courses].Domain
GROUP BY [domain].[Account ID], [domain].[Account Name], [passed courses].[Learner Email Address], [passed courses].Category, [passed courses].Country, [passed courses].[Earliest Start_Date], [passed courses].[Latest End_Date]
PIVOT [passed courses].[Course Number];
Any help is appreciated!
What if you added a case statement into your query where if country = '' then return a unique GUID else return the Country column value and alias the case as Country
GUID function for access
You may need to wrap the whole query as a sub query to group by the Country aliased column though. This isn't tested, just a concept idea as I can't see any other way of grouping a country and ignoring blanks within the same select, or a less you did 2 selects and did a union instead.
Regards
Liam

Need a Totals column in MDX

I have the following query that gives me an extract (I am putting this into a CSV from my cube).
I want to show the Total of all NRx in a column as the first column NRx and the rest of the NRx columns should come up as they do now. I tried to add a SUM (Measures.NRx) to this, but it did not work
How do I get a total NRx column?
SELECT
NON EMPTY
CrossJoin
(
[Time Period].[Calendar].[Cycle Dt],
{
[Measures].[NRx]
}
)
ON COLUMNS,
NON EMPTY
{(
[Prescriber].[Prescriber Name].[Prescriber Name].Members
, [Prototype Alignment].[Tier Code].[Lilly Tier Code].Members
, [Prototype Alignment].[Territory Name].[Territory Name].Members
, [Prototype Alignment].[District Name].[District Name].Members
, [Prototype Alignment].[Division Name].[Division Name].Members
, [Prototype Alignment].[Area Name].[Area Name].Members
, [Market Product].[Product Group Nbr].[Product Group Nbr].Members
, [Market Product].[Product G Name Name].[Product G Name Name].Members
)} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS
FROM
(
SELECT
({
[Prescriber].[Ims Prescriber Id].&[011111]
}
,
{
[Time Period].[Cycle Dt].&[2008-10-03T00:00:00],
[Time Period].[Cycle Dt].&[2008-10-10T00:00:00],
[Time Period].[Cycle Dt].&[2008-10-17T00:00:00],
[Time Period].[Cycle Dt].&[2008-10-24T00:00:00],
[Time Period].[Cycle Dt].&[2008-10-31T00:00:00],
[Time Period].[Cycle Dt].&[2008-11-07T00:00:00],
[Time Period].[Cycle Dt].&[2008-11-14T00:00:00],
[Time Period].[Cycle Dt].&[2008-11-21T00:00:00],
[Time Period].[Cycle Dt].&[2008-11-28T00:00:00],
[Time Period].[Cycle Dt].&[2008-12-05T00:00:00],
[Time Period].[Cycle Dt].&[2008-12-12T00:00:00],
[Time Period].[Cycle Dt].&[2008-12-12T00:00:00],
[Time Period].[Cycle Dt].&[2008-12-19T00:00:00],
[Time Period].[Cycle Dt].&[2008-12-26T00:00:00],
[Time Period].[Cycle Dt].&[2008-12-26T00:00:00]
}
) ON COLUMNS
FROM [Mart]
) CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE,
FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS
In the first Crossjoin, where you have
[Time Period].[Calendar].[Cycle Dt],
Try adding a reference to the All member of the Calendar hierarchy
{[Time Period].[Calendar].[Cycle Dt], [Time Period].[Calendar].[All] },