How to calculate Average based on a criteria on SSAS? - ssas

I've a Measure like below,
Year ProductCategory CompanyId TotalCustCnt SalesAmt Rank
2012 Prd1 1 20 100,000 1
2012 Prd2 1 10 75,000 2
2013 Prd1 2 18 80,000 2
2013 Prd2 2 15 50,000 1
Now I want to calculate three averages out of this data.
Average = SalesAmt / TotalCustCnt
Company average (Average for each company)
Leader average (Average for the leader for the Product category,year i.e company with Rank 1)
Industry average (Average for the whole industry for the product category,year)
The first one is straight forward, I've added a calculated field in the CUBE for adding an expression (SalesAmt / TotalCustCnt)
But how do I calculate the other two averages?
I've checked the AVG function and also tried SUM([Measures].[SalesAmt].ALLMEMBER) / SUM([Measures].[TotalCustCnt].ALLMEMBER) but no success.

Related

SQL - Calculate YTD number

Here is the table:
Name
Month
Num
Metric
1/1/22
10
Metric
1/2/22
10
Metric
1/3/22
10
Metric
1/4/22
10
Metric
1/5/22
10
Metric
1/6/22
10
Metric
1/7/22
10
Metric
1/8/22
10
Metric
1/9/22
10
Metric
1/10/22
10
Metric
1/11/22
10
Metric
1/12/22
10
Metric
YTD
How to calculate the YTD number which sums the numbers in the Num column based on the month of the year? For example, it should return 40 right now. In May, it should return 50.
Thanks!

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];

sql running total math current quarter

Im trying to figure out the total for the quarter when the only data shown is a running total for the year:
Id Amount Periods Year Type Date
-------------------------------------------------------------
1 65 2 2014 G 4-1-12
2 75 3 2014 G 7-1-12
3 25 1 2014 G 1-1-12
4 60 1 2014 H 1-1-12
5 75 1 2014 Y 1-1-12
6 120 3 2014 I 7-1-12
7 30 1 2014 I 1-1-12
8 90 2 2014 I 4-1-12
In the data shown above. The items in type G and I are running totals for the period (in qtrs). If my query returns period 3, is there a sql way to get the data for the qtr? The math would involve retrieving the data for the 3rd period - 2nd period.
Right now my sql is something like:
SELECT * FROM data WHERE Date='4-1-12';
In this query, it will return row #1, which is a total for 2 periods. I would like it to return just the total for the 2nd period. Im looking to make this happen with SQLite.
Any help would be appreciated.
Thank alot
You want to subtract the running total of the previous quarter:
SELECT Id,
Year,
Type,
Date,
Amount - IFNULL((SELECT Amount
FROM data AS previousQuarter
WHERE previousQuarter.Year = data.year
AND previousQuarter.Type = data.Type
AND previousQuarter.Periods = data.Periods - 1
), 0) AS Amount
FROM data
The IFNULL is needed to handle a quarter that has no previous quarter.

Oracle SQL Paired Data

I need to find the difference in averages between patient weights at different visits (time points), but I'm struggling with finding the "paired" averages:
I have 1 table (PHYS) containing patient weights at different visits:
PATIENT VISIT WEIGHT
1 Baseline 200
1 1 Month 190
1 2 Month 170
2 Baseline 300
2 1 Month 290
2 2 Month 280
3 Baseline 250
3 1 Month 230
My problem is that I only want to find the difference for paired data. For example, when calculating the amount of weight loss between the 2 month and Baseline visits, I would want to find the difference between the (average 2 Month weight) and the (average Baseline weight FOR ONLY THOSE PATIENTS WITH A 2 MONTH WEIGHT). In this example, the result should be AVG(170,280) - AVG(200,300) = -25 (since only patient 1 and 2 have 2 Month weights).
Here is what I have, but it calculates the difference based on all weights:
SELECT VISIT
AVG(WEIGHT)
-
(SELECT
AVG(WEIGHT)
FROM PHYS
WHERE VISIT = 'BASELINE')
FROM PHYS
GROUP BY VISIT
My desired output would be (I know I need to add an ORDER BY):
VISIT CHANGE FROM BASELINE
Baseline 0
1 Month -13.3
2 Month -25
Thank you and sorry for such a newb question.
You can do this with a join to the same table but only for the 'Baseline'. Then, the aggregation only aggregates the values that match, so you should get different baseline averages for the three groups (because the populations are different):
select p.visit, avg(p.weight) as avg_weight, avg(pbl.weight) as avg_blweight,
(avg(p.weight) - avg(pbl.weight)) as change
from phys p join
phys pbl
on p.patient = pbl.patient and
pbl.visit = 'Baseline'
group by p.visit;

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..