SSRS 2012 Mean & Median - aggregate

I have a report that i use Mean and Median measures that were calculated in SSAS 2012 tabular, the reports as follows:
when i use the Mean and Median directly, the values in the green box are correct and the column and row totals are incorrect.
after using aggregate instead of sum the following happens:
1- the blank row and blank column are now gone along with their values.
2- Then total of the Mean is correct in every cell except the grand total(2nd cell from right bottom corner), it appears to take the value of the cell given in Mean for the cross between blank and blank in previous pic.
3- The totals for the median are now either blank or 0 except for the grand total (right bottom corner cell), it seems to have the value for the Median from the cross of blank and blank in previous pic.
I am stuck here i dont know what to do and i want to avoid using another dataset if possible. HELP!!
A screen shot of my Query designer:
After including all empty spaces :
(=Aggregate is used on every cell),
please note after taking away extra dimension columns, the Total row in the bottom changed , the correct Mean totals from pre-pic are now gone.
The MDX for the DataSet :
SELECT { [Measures].[INCOME AVERAGE], [Measures].[INCOME MEDIAN] } ON COLUMNS, {[DIM_Type of Education for Household نوع التعليم لرب الأسرة].[EDU_TYPE_ENAME].[EDU_TYPE_ENAME].ALLMEMBERS * [DIM_Nationality of household الجنسية لرئيس الأسرة].[NATIONALITY_L1_ENAME].[NATIONALITY_L1_ENAME].ALLMEMBERS, [DIM_Type of Education for Household نوع التعليم لرب الأسرة].[EDU_TYPE_ENAME].[EDU_TYPE_ENAME].ALLMEMBERS * {[DIM_Nationality of household الجنسية لرئيس الأسرة].[NATIONALITY_L1_ENAME].[All]}, {[DIM_Type of Education for Household نوع التعليم لرب الأسرة].[EDU_TYPE_ENAME].[All]} * [DIM_Nationality of household الجنسية لرئيس الأسرة].[NATIONALITY_L1_ENAME].[NATIONALITY_L1_ENAME].ALLMEMBERS, {[DIM_Type of Education for Household نوع التعليم لرب الأسرة].[EDU_TYPE_ENAME].[All]} * {[DIM_Nationality of household الجنسية لرئيس الأسرة].[NATIONALITY_L1_ENAME].[All]}, ([DIM_Type of Education for Household نوع التعليم لرب الأسرة].[EDU_TYPE_ENAME].[EDU_TYPE_ENAME].ALLMEMBERS * [DIM_Nationality of household الجنسية لرئيس الأسرة].[NATIONALITY_L1_ENAME].[NATIONALITY_L1_ENAME].ALLMEMBERS ) } DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS FROM [Model] CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS
Thanks in advance.

Your MDX query includes a ton of fields. Can you simplify it to the following to see if it solves the problem:
+-----------+-------------+--------+---------+
| Education | Nationality | Median | Average |
+-----------+-------------+--------+---------+
| null | null | 100 | 110 |
| PhD | null | 100 | 110 |
| null | Saudi | 100 | 110 |
| PhD | Saudi | 100 | 110 |
+-----------+-------------+--------+---------+
Don't return any extra dimension columns.
I am using null to represent the All member which is now the MDX query designer does it. Hopefully just dragging and dropping Education and Nationality onto the grid in the graphical MDX query designer will get you what you want. There is a button that turns on showing the All member if I recall.
Row 1 is for the grand total in the very bottom right.
Row 2 is for the subtotal in the far right column.
Row 3 is for the subtotal on the bottom row.
Row 4 fills up the body of the Report.
Then change the layout of the Tablix so that the row group is on the Education column and nothing else. Change the column group to be on Nationality and nothing else. Make sure all the textboxes in your Tablix have =Aggregate. I am hopeful it should work now.

Related

How to summarise measured column but not from the beginning of the filtered values in powerBI

I have two tables . I have created a table visual in power BI where I am combining the two values eg below :
Table 1 :
Site Name
Type
Area(m2)
A
Hospital
50
A
Education
100
B
Tech Lab
20
B
Office
70
B
Education
90
Table 2 :
Type
Usage (hr/m2)
Type
Hospital
100
Low
Education
500
Low
Tech Lab
50
Low
Office
200
Low
This visual also has a slicer:
Usage Type
Low
Medium
High
The slicer currently chosen is lets say "Low"
On my PowerBI visual ( a table) I am merging this two tables by "Type" to give me
Site Name
Type
Area
Usage(hr/m2)
A
Hospital
50
100
A
Education
100
500
B
Tech Lab
20
50
B
Office
70
200
B
Education
90
500
The end goal is to find the usage by site. In my head the equation should be :
For A :
TotalUsage[A]=Area[Hospital]*Usage[Hospital]+Area[Education]*Usage[Education] (50x100+100x500=55000)
TotalArea[A]=Area[Hospital]+Area[Education]=150
Usage[A]=TotalUsage[A]/TotalArea[A]=55000/150~366 hr/m2
How can I achieve this ?
I first created the visual as shown above(third table)
I did quick measures for total usage for each row. The displayed that on the third table.
However when I try to measure again , this type only grouped by Site, instead of following the above equation it sums the usage rows first (so 100+500=600), then sums the area ( 50+100=150) and then calculates the usage for site A giving 600/150 ( which is wrong! )
Use these measures:
Area Usage =
SUMX(
'Table 1',
'Table 1'[Area(m2)] * LOOKUPVALUE(
'Table 2'[Usage (hr/m2)],
'Table 2'[Type], SELECTEDVALUE('Table 1'[Type]),
'Table 2'[Usage Type], SELECTEDVALUE('Table 2'[Usage Type])
)
)
Total Usage =
SUMX(
VALUES('Table 1'[Type]),
[Area Usage]
)
Total Area = SUM('Table 1'[Area(m2)])
Usage = DIVIDE([Total Usage], [Total Area]))
and put everything together in a table

Compare three column values with three inputs by size in postgres

I am writing software where I have a box size (width=10, height=20, length=10) and a Postgres database with shelves (also defined by width, height, length). Now I want to find all shelves where I can put a specific box (the shelf dimensions need to be greater or equal to the box). I was originally thinking that I could do this:
SELECT shelves.* WHERE shelves.width = 10 AND shelves.height = 20 AND shelves.length = 10
However, my box can fit in any of these shelves, because it can just be rotated to fit:
|------|---------|----------|----------|
| id | width | height | length |
|------|---------|----------|----------|
| 1 | 20 | 10 | 10 |
|------|---------|----------|----------|
| 2 | 10 | 20 | 10 |
|------|---------|----------|----------|
| 2 | 10 | 10 | 20 |
|------|---------|----------|----------|
So, in JavaScript, this code would look like this:
const boxdim = [box.width, box.height, box.length].sort();
const shelfdim = [shelf.width, shelf.height, shelf.length].sort();
const canFit = boxdim[0] <= shelfdim[0] && boxdim[1] <= shelfdim[1] && boxdim[2] <= shelfdim[2];
Is is possible to do this in Postgres? I would need something that looks like this (which is obviously pseudo code and doesn't work:
SELECT shelves.* WHERE SORT(shelves.width, shelves.height, shelves.length)[0] = 10 AND SORT(shelves.width, shelves.height, shelves.length)[1] = 10 AND SORT(shelves.width, shelves.height, shelves.length)[2] = 20
Any tips appreciated.
The intarray extension provides a sort function for arrays, but you can also write one yourself if you don't want to use that or have a different element type.
Now since in Postgres "The array ordering operators (<, >=, etc) compare the array contents element-by-element," To check whether each value is smaller than the respective shelf dimension, you can use UNNEST:
SELECT *
FROM shelves
WHERE (SELECT bool_and(box_dim <= shelf_dim)
FROM UNNEST(
sort(ARRAY[width, height, length]),
ARRAY[10, 10, 20]
) AS dims(box_dim, shelf_dim));
I am not saying that you should do this, but in PostgreSQL you can do some pretty cool (or bad, depends on your point of view) things with arrays:
SELECT id, array_agg(d) FROM (
SELECT id, unnest(ARRAY[width, height, length]) AS d
FROM shelves
ORDER BY id, d) X
GRUOP BY id;
And you have your dimensions sorted into a nice array, à là Javascript. I suggest using this query to generate a view.
Now, depending on your code you can do this again with the table of boxes or just pass the three parameters in the right order to the query that will work on the view.
I really hope someone will provide an answer that uses standard SQL, without arrays. In that case, please, choose their answer. :)

Calculated Time weigthed return in Power BI

Im trying to calculate the Time Weigthed Return for a portfolio of stocks. The formula is:
I have the following data:
Im calculate the TWR (time weigthed return) in Power Bi as:
TWR = productx(tabel1;TWR denom/yield+1)
The grey and blue marked/selected fields are individual single stock. Here you see the TWR for the grey stock is = 0,030561631 and for the blue TWR = 0,012208719 which is correct for the period from 09.03.19 to 13.03.19.
My problem is, when im trying to calculate the TWR for a portfolio of the two stocks, it takes the product og every row. In the orange field I have calculated the correct result in excel. But in Power BI it takes the product of the grey and blue stocks TWR: (0,0305661631 * 0,012208719) = 0,03143468 which is incorrect.
I want to sum(yield for both stocks)/sum(TWRDenominator for both stocks) for both stocks every single date, such that I not end up with two rows (one for each stock) but instead a common number every date for the portfolio.
I have calculated the column TWR denom/yield -1 in a measure like this:
twr denom/yield-1 = CALCULATE(1+sumx(tabel1;tabel1(yield)/sumx(tabel1;tabel1[TwrDenominator])))
How can I solved this problem?
Thank you in advance!
This is one solution to your question but it assumes the data is in the following format:
[Date] | [Stock] | [TWR] | [Yield]
-----------------------------------
[d1] | X | 12355 | 236
[d1] | y | 23541 | 36
[d2] ... etc.
I.e. date is not a unique value in the table, though date-stock name will be.
Then you can create a new calculated table using the following code:
Portfolio_101 =
CalculateTable(
Summarize(
DataTable;
DataTable[Date];
"Yield_over_TWR"; Sum(DataTable[Yield])/Sum(DataTable[TWR_den])+1
);
Datatable[Stock] in {"Stock_Name_1"; "Stock_Name_2"}
)
Then in the new Portfolio_101 create a measure:
Return_101 =
Productx(
Portfolio_101;
Portfolio_101[Yield_over_TWR]
)-1
If using your data I en up with the following table, I have created three calculated tables, one fore each stock and a third (Portfolio_103) with the two combined. In addition I have a calendar table which has a 1:1 relationship between all Portfolio tables.
Hope this helps, otherwise let me know where I've misunderstood you.
Cheers,
Oscar

Filtering data in an aggregation in a calculated field

I have 3 columns:
County | State | Population*
*(which is the population of that county)
How can I make a calculated field that displays what percent of the state's population is in each county?
If I do ([Population] / {SUM([Population])}) * 100 then that calculates each county's % of population of all counties in every state combined. I would like to calculate the SUM[Population] only where the state is the same state.
You're close. Use the FIXED key word in your LOD calc.
sum([Population]) / sum({FIXED [State] : SUM([Population])}) * 100
Here's a good link for more information. https://interworks.com/blog/rcurtis/2016/03/24/tableau-deep-dive-lod-fixed-calculation/

Division by zero error in crosstab formula

My crosstab displays the total hours for each user. The display Column's grand total is formatted as a percentage:
Task | User1 | User2 | Etc . . .
TotalTime 41.68 44.55
TotalHours 52.17 84.93
% Total 79.89% 52.45%
I use a formula field ({TotalTime}/{TotalHrs}) * 100 - but when I drag that formula field into Summarized Field, I get a Division by zero error. How can I resolve this?
Create a forumula and write below code.
If {TotalHrs}=0
then ToText(0)
Else ToText((({TotalTime}/{TotalHrs}) * 100),2)
Now use this forumula in cross tab.
You could use a numeric formula as well:
If Not(Isnull({TotalHrs})) Then ({TotalTime}/{TotalHrs}) * 100