MDX weighted values - mdx

I have a cube built on a fact which, amongst others, includes the Balance and Percentage columns. I have a calculation which multiplies the Balance by the Percentage to obtain an Adjusted Value. I now need to have this Adjusted Value divided by the sum of all balances, to get weighted values.
The problem is that this sum of all balances doesn't apply to the whole dataset. Rather, it should be calculated on a filtered subset of the whole data. This filtering is being done in Excel using a pivot table, so i do not know what conditions will be used to filter.
So, for example, this would be the pivot i'd like to see:
ID Balance Percentage Adjusted Value Weighted Adjusted Value
1 100 1.5 115 0.38 (ie 115/300)
2 50 2 51 0.17 (ie 51/300)
3 150 1 150 0.50 (ie 150/300)
300 is obtained by summing the balance of the rows that show in the filtered pivot.
Can this calculation be somehow done in OLAP? Or is it impossible to compute this sum with what i know?

Yes should be possible; e.g., assuming 1/2/3 are the children of a common parent, then the following calculated measure should do the trick :
WAV = AV / ( id.parent, Balance )
If not we would need more information about the actual data model and query.

Related

SQL Compound Growth Calculation based on previous rows (varying rate)

Given a column for 'Growth Factors' and a starting value I need to compute future values. For example, if a starting value of 1 is provided then the computed 'Value' column would be as shown below. Thus, Value(t2) = Value(t1) x Growth_Factor(t2). Base condition is Value(t1) = Starting_Value x Growth_Factor(t1). Example shown below.
How do I compute this in SQL (or Presto) where the computed value is dependent on previous computed values?
Growth Factor
Value
Time
1.2
1.2
1
1.1
1.32
2
1.5
1.98
3
1.7
3.366
4
You could sum the logarithms and invert when finished. This will work other than some possibility of small floating point error. But you're also going to introduce error once you multiply more than a few numbers with doubling decimal places at every iteration.
exp(
sum(ln(growth)) over (order by time)
)

Calculate a % of a field

Using quicksight, I'd like to create the % of my total revenues vs delta of loss.
Basically, I've a field, where inside there are all the economics (so not just revenues but also costs). I'm successfully filtering it to have 2 columns, one for Revenues and one for costs. Revenues came adding these filters to different categories field I've in the system:
Pl_cat --> filtered as "1-revenues" and "2-Expenses"
bu_F --> filtered as "1-work"
Report_super_category --> filters as "1-Coworking"
Act_Bud --> filtered as "Actual"
Applying all these filters to fy_total (field that held all my numbers), using a month field for the X axes, and multiplying costs *-1, I get the correct revenues and costs numbers in columns a table split by month.
Also, using fy_total withouth any calcs, I get the delta by month (leaving those filters applied of course).
What i'd to do now is, getting the % of that delta over those filter revenues.
Example: Total month revenues (all fields, no filters): 1M
Applying those filters and a calculated field where Expenses are multiplied by -1, I get the revenues and expenses for 1-Coworking under 1-Work in 2 columns, let's say 600K for Revenues and 400K for expenses.
Also Using fy_total withouth any calculated fyeld, I get its delta: 600K-400K = 200K
I want to transform those 200K in 200/600 = 33%
I've added an image of my current situation, I'm using quicksight which is also based on SQL presto language
Thank you guys!

DAX - Reference measure in calculated column?

I have data like this
EmployeeID Value
1 7
2 6
3 5
4 3
I would like to create a DAX calculated column (or do I need a measure?) that gives me for each row, Value - AVG() of selected rows.
So if the AVG() of the above 4 rows is 5.25, I would get results like this
EmployeeID Value Diff
1 7 1.75
2 6 0.75
3 5 -0.25
4 3 -1.75
Still learning DAX, I cannot figure out how to implement this?
Thanks
I figured this out with the help of some folks on MSDN forums.
This will only work as a measure because measures are selection aware while calculated columns are not.
The Average stored in a variable is critical. ALLSELECTED() gives you the current selection in a pivot table.
AVERAGEX does the row value - avg of selection.
Diff:=
Var ptAVG = CALCULATE(AVERAGE[Value],ALLSELECTED())
RETURN AVERAGEX(Employee, Value - ptAVG)
You can certainly do this with a calculated column. It's simply
Diff = TableName[Value] - AVERAGE(TableName[Value])
Note that this averages over all employees. If you want to average over only specific groups, then more work needs to be done.

Conditional Probability with Powerpivot

my major goal is to calculate Conditional Probability over a large number of rows. Hence the use of Powerpivot.
Attached is an excel file with 10 rows as an example of how I did it in Excel.
My challenge is the formula in column F which I will then be needing to calculate column G.
Tamir
Can you check the solution
Main Formulas:
Calculate a Total, without filtering BRAND and UPS (calculated measure)
=CALCULATE([Total],All(Brand),All(upc))
Sum IF UPC (calculated column):
=CALCULATE([Total],filter(ALL(Fact),Fact[UPC]= EARLIER(Fact[UPC]) ))

Ignore non-empty values when aggregating

I have a cube I've built with three separate measures: "TY Sales", "LY Sales", and "% Change", what I'm trying to do is have special behavior for the aggregate rows, basically not including any "LY Sales" values when summing the total if "TY Sales" is 0. So currently my cube works like below:
LYSales TYSales %Change
Year 1 450 300 -33%
Week 1 100 125 +25%
Week 2 150 175 +14%
Week 3 200 0 +0%
The aggregate column "Year 1" in this example, is summing all values for each sales measure. What I want it to do instead, is only include values in LYSales if TYSales also has a non-zero value. So my ideal state would be below:
LYSales TYSales %Change
Year 1 250 300 +20%
Week 1 100 125 +25%
Week 2 150 175 +14%
Week 3 200 0 +0%
I'm new to SSAS, so any guidance is appreciated. Thanks
An easy and reliable way to achieve that would be to change the source column of LYSales to be zero if TYSales is zero. This would be done in the fact table on which the measure is based. You could implement that
either in the ETL process, changing the LYSales column values to be zero when TYSales is zero,
or in a view based on the fact table that is then used in the Data Source View instead of the original table,
or as a Calculated Calculation of the fact table in the Data Source View.
In the latter two cases, the calculation formula would be SQL like this:
case when TYSales <> 0 then LYSales else 0 end
Then switch the measure definition to use that column.