SSAS MDX calculation - ssas

I need a calculated measure(SSAS MD) to calculate the percentage of count values.
I have tried below expression but I did not get the desired output.Let me know if I missing anything and I want to calculate the percentage of the age for the group by the car total:
( [DimCar].[Car], [DimAge].[Age], [Measure].[Count])/
sum([DimCar].[Car].[All].children), ([DimAge].[Age].[All], [Meaures].[Count])}*100
Below are the sample date values in cube:
Car Age Count
----- ----- -----
Benz 1 2
Camry 37
Honda 1 18
Honda 6 10
Expected output:
Car Age Count Percent TotalCount
----- ----- ----- ------ ----------
Benz 1 2 100% 2
Camry 37 100% 37
Honda 1 18 64.28% 28
Honda 6 10 35.71% 28
Forumula to calculate percentage:
18/28*100 =64.28%
10/28*100 =35.71%
Honda 1 18 64.28% 28
Honda 6 10 35.71% 28

with Member [Measures].[Total Sales Count]
as iif (isempty([Measures].[Sales]),NUll, sum([Model].[Modelname].[All].children ,[Measures].[Sales]))
Member [Measures].[Total Sales%]
as ([Measures].[Sales]/[Measures].[Total Sales Count]),FORMAT_STRING = "Percent"
select {[Measures].[Sales],[Measures].[Total Sales Count],[Measures].[Total Sales%]
}on 0
,non empty{[Car].[Carname].[Carname]*[Model].[Modelname].[Modelname]} on 1
from [Cube]
Output :
Car Model Sales Total Sales Count Total Sales%
Benz New Model 2 2 100.00%
Camry Old Model 37 37 100.00%
Honda New Model 18 28 64.29%
Honda Top Model 10 28 35.71%
Instead of "Age" attribute I have added "Model" dimension.
Below code get exact output which is expected.
enter image description here

My understanding is that for a particular car example honda, you want to divide by the total honda's irrespective of their Age. In this case 28. So for Age:six honda you use 10/28. Where as for Benz, since all Benz are Age: 1 you use 2.
Use the following code
Round(
(
( [DimCar].[Car].currentmember, [DimAge].[Age].currentmember, [Measure].[Count])
/
([DimCar].[Car].currentmember,root([DimAge]),[Measure].[Count])
)*100
,2)
Below is a similar example on adventure works
with member
measures.t as
(
( [Product].[Category].currentmember, [Delivery Date].[Calendar Year].currentmember, [Measures].[Internet Order Quantity])
/
([Product].[Category].currentmember,root([Delivery Date]),[Measures].[Internet Order Quantity])
)*100
select {[Measures].[Internet Order Quantity],measures.t}
on columns ,
non empty
([Product].[Category].[Category],[Delivery Date].[Calendar Year].[Calendar Year])
on rows
from [Adventure Works]

Related

find sum of the count for the latest snapshot in pandas

i have a below df and want to calculate sum of a group by taking last snapshot:
product desc id month_year count
car ford 1 2019-01 20
car ford 1 2019-02 20
car ford 1 2019-04 40
car ford 2 2019-04 30
car ford 2 2019-04 30
car ford 2 2019-04 60
and find output as
df.groupby(["product", "desc"]. ?
product desc count_overall
car ford 100
which is for id 1 take last count order by desc month_year which is 40 and similarly for 2 it is 60 which makes the total as 100
IIUC you need the id as well to get the last value of count
s=df.groupby(["product", "desc","id"])['count'].last().sum(level=[0,1]).to_frame('count_overall').reset_index()
Out[171]:
product desc count_overall
0 car ford 100
You can also use drop_duplicates given the data is sorted by date already:
(df.drop_duplicates(['product','desc','id'], keep='last')
.groupby(['product','desc'])['count'].sum()
)
Output:
product desc
car ford 100
Name: count, dtype: int64
IIUC,
we can use groupby with agg with sort_values to get the last occurance of count.
first we transform your date into a proper datetime
df['month_year'] = pd.to_datetime(df['month_year'],format='%Y-%m')
new_df = df.sort_values("count").groupby(["product", "desc", "id"]).agg(
date_max=("month_year", max), count=("count", "last")
)
print(new_df)
date_max count
product desc id
car ford 1 2019-04-01 40
2 2019-04-01 60
from here you can just do a simple sum
print(new_df.groupby(level=[0,1]).sum())
count
product desc
car ford 100

pig script loop though calculate averages

I have data that will be run in pig using aws emr looks like. The columns are called model, year, units_sold, total_customers.
chevy 1900 1000 49
chevy 1901 73 92
chevy 1902 45 65
chevy 1903 300 75
ford 1900 35 12
ford 1901 777 32
ford 1902 932 484
ford 1903 33 15
What I am trying to do is calculate the average for every car type. the averages will be calculated by adding the sum of units_sold, divided by the sum of total_customers.
so the desired result would look like
chevy (1000+73+45+300) / (49+92+65+75) = 5.04
ford (35+777+932+33) / (12+32+484+15) = 3.27
in my script i have
A = *Step to load data*;
B = GROUP A by year;
C = results = FOREACH B GENERATE SUM(units_sold)/SUM(total_customers);
dump C;
This returns an incorrect result.How can I achieve results that look like
chevy 5.04
ford 3.27
Looks like you need to group by car type, not year. Also, you might need to cast to float if units_sold and total_customers are integers if you don't want a rounded result. Try:
B = GROUP A by model;
C = FOREACH B GENERATE (float)SUM(units_sold)/(float)SUM(total_customers);

MDX Measures on Rows and CurrentMember.UniqueName in Column

Need helping getting Member Full Path and Ordinality rotated on columns
I have cube with the following:
1 dimension with 4 levels (dim) in its hierarchy.
Example in table:
GCA = Group Customer A = Level 0,
EC = End Customer A = level 1
1 time dimension with Fiscal Year, Quarter and Month Level
2 measures (Revenue and Cost)
2 calculated measures (Gross Margin and Gross Margin%)
I use a LastPeriods statement on columns to display the last X quarters in column
I use a ToggleDrillState statement to drill on the dim and crossjoin this with the 4 measures.
To be able to style the table on the front end, I need the dim MemberFullPath and ordinality.
This gives me the following result:
DimensionWith4Lvl Measures 2017Q1 2017Q2
GCA Revenue 100 110
GCA Cost 60 70
GCA GM 40 40
GCA GM% 40% 36%
GCA MemberOrdinal 0 0
GCA MemberFullPath [Dim].[GCA] [Dim].[GCA]
EndCustA Revenue 100 110
EndCustA Cost 60 70
EndCustA GM 40 40
EndCustA GM% 40% 36%
EndCustA MemberOrdinal 1 1
EndCustA MemberFullPath [Dim].[GCA].[ECA] [Dim].[GCA].[ECA]
I need to get, however, the following output:
DimensionWith4Lvl Measures 2017Q1 2017Q2 MemberFullPath MemberOrdinal
GCA Revenue 100 110 [Dim].[GCA] 0
GCA Cost 60 70 [Dim].[GCA] 0
GCA GM 40 40 [Dim].[GCA] 0
GCA GM% 40% 36% [Dim].[GCA] 0
EndCustA Revenue 100 110 [Dim].[GCA].[ECA] 1
EndCustA Cost 60 70 [Dim].[GCA].[ECA] 1
EndCustA GM 40 40 [Dim].[GCA].[ECA] 1
EndCustA GM% 40% 36% [Dim].[GCA].[ECA] 1
This is my query.
with member [Measures].[Member Full Path] as '[Revenuedim].CurrentMember.UniqueName'
member [Measures].[Member Ordinal] as '[Revenuedim].CurrentMember.Ordinal'
member [Measures].[GM] as '([Measures].[Revenue_USD] - [Measures].[Cost_USD])'
member [Measures].[GM%] as 'IIF( [Measures].[Revenue_USD] > 2 , (([Measures].[GM] / [Measures].[Revenue_USD]) * 100), NULL)'
member [Measures].[Revenue] as '[Measures].[Revenue_USD]'
member [Measures].[Cost] as '[Measures].[Cost_USD]'
select LastPeriods(4, [Date.YQM].LastChild.LastChild) ON 0,
Crossjoin(
order(
Filter(
ToggleDrillState(
ToggleDrillState(
{[Revenuedim].[All GCs]}, {[Revenuedim].[All GCs] })
, {} )
, [Measures].[Revenue] <> 0 OR [Measures].[Cost] <> 0)
,[Measures].[Revenue], DESC)
, {[Measures].[Revenue],
[Measures].[Cost],
[Measures].[GM],
[Measures].[GM%],
[Measures].[Member Full Path],
[Measures].[Member Ordinal] }) ON 1
from [GM_CUST]

Running Total Measure for Project Milestones

I have a SSAS 2012 cube that has project name, milestone name and milestone date dimensions. The basic measure is dollars spent. I need an additional calculated measure of the cumulative dollars for a project spent through each milestone date.
Example of Data:
Project Milestone Milestone Date Spent Running Total
A 1 1/1/2015 10 10
A 2 2/23/2015 35 45
A 3 4/5/2015 4 49
B 1 6/7/2015 2 2
B 2 9/7/2015 49 51
The milestone date dimension is referenced through the Milestone dimension. Any suggestions on how to create the calculated measure would be appreciated.
Here is an example against AdvWrks:
WITH
MEMBER [Measures].[rnTotal] AS
Sum
(
(NULL : [Date].[Calendar].CurrentMember)
*
[Product].[Category].CurrentMember
,[Measures].[Internet Sales Amount]
)
SELECT
[Measures].[rnTotal] ON 0
,
[Product].[Category].[Category]
*
(
[Date].[Calendar].[Month].&[2006]&[6]
:
[Date].[Calendar].[Month].&[2007]&[6]
) ON 1
FROM [Adventure Works];

how to calculate % in PPS 2010

i have these columns in the table and made this table as the FACT table and also using time intelligence filter in the PPS2010..
i have measures , sum (materials), sum (sales) and sum (material_%)
in the PPS dashboard design i have included this cube and all the measures.. and using an analytic chart..
i have developed separate graphs for each columns (material, sales, material_%)..
for the sales and materials there is no problem , when i use the time filter
in the material_% graph i used the time filter current quarter in months (showing three months ) shows the correct value..
when i use the current quarter filter (sum of all the 3 months)
its showing 146% (83 +33 +30) --> for actual values
and 150 % ( 50+50+50) --> for target values
actually it showed show me 46% for actual and 50% for target ,
it should be sum of material in all the 3 months / sum of sales in all the 3 months but its just calculating sum of material_% column of all the 3 months
time filter : year :: Halfyear ::quarter:: Month::Day
DataBase Table:
Month Year Material sales Material_% [ material / sales]
Jan_Act 2011 500 600 83
Jan_target 2011 400 800 50
Feb_Act 2011 300 900 33
Feb_target 2011 300 600 50
Mar_Act 2011 300 900 30
Mar_target 2011 300 600 50
......
Jan_Act 2012 0 0 0
Jan_target 2012 600 1000 60
.............
Dec_Act 2012 0 0 0
Dec_target 2012 600 800 75
MDX Query:
SELECT
HIERARCHIZE( { [Time_dim].[Year - Half Year - Quarter - Month - Date].DEFAULTMEMBER } )ON COLUMNS,
HIERARCHIZE( { [Ven Bi Actfctmaster].[Act Fct].&[ACTUAL], [Ven Bi Actfctmaster].[Act Fct].&[TARGET] } )ON ROWS
FROM [Vin Finance]
WHERE ( [Measures].[Materials - Ven Bifullrptmaster] )
Please help me to sort out this issue.
i solved this issue by changing the measure of '%' columns from sum to averageofchild in the property tab..