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

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

Related

Calculating a score for each road in Openstreetmap produces unexpected result. What am I missing?

I have a Postgres database with a postgis extention installed and filles with open street map data.
With the following SQL statement :
SELECT
l.osm_id,
sum(
st_area(st_intersection(ST_Buffer(l.way, 30), p.way))
/
st_area(ST_Buffer(l.way, 30))
) as green_fraction
FROM planet_osm_line AS l
INNER JOIN planet_osm_polygon AS p ON ST_Intersects(l.way, ST_Buffer(p.way,30))
WHERE p.natural in ('water') or p.landuse in ('forest') GROUP BY l.osm_id;
I calculate a "green" score.
My goal is to create a "green" score for each osm_id.
Which means; how much of a road is near a water, forrest or something similar.
For example a road that is enclosed by a park would have a score of 1.
A road that only runs by a river for a short period of time would have a score of for example 0.4
OR so is my expectation.
But by inspection the result of this calculation I get sometimes Values of
212.11701212511463 for a road with the OSM ID -647522
and 82 for a road with osm ID -6497265
I do get values between 0 and 1 too but I don't understand why I do also get such huge values.
What am I missing ?
I was expecting values between 1 and 0.
Using a custom unique ID that you must populate, the query can also union eventually overlapping polygons:
SELECT
l.uid,
st_area(
ST_UNION(
st_intersection(ST_Buffer(l.way, 30), p.way))
) / st_area(ST_Buffer(l.way, 30)) as green_fraction
FROM planet_osm_line AS l
INNER JOIN planet_osm_polygon AS p
ON st_dwithin(l.way, p.way,30)
WHERE p.natural in ('water') or p.landuse in ('forest')
GROUP BY l.uid;

SSRS 2012 Mean & Median

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.

MDX How to add bands to grouped fact table

I'm trying to create income bands on my fact table.
The fact table that consists of
Date, TranType, AdvKey, Amount
01/01/2015 TRAN1 ID01 5000
01/02/2015 TRAN1 ID02 13000
01/03/2015 TRAN2 ID01 500
The AdvKey links back to DimIFADetails, this table has a ParentKey, a Parent Child hierarchy. There are two levels, Adviser and Adviser Group
ADVKEY PARENTKEY
----------------
ID01 ID03
ID02 ID03
ID03 NULL
I want to be able to group on AdvKey or AdvGrpKey and have the summed amounts (income) allocated to income bands.
So when I group on Adviser, each adviser will be assigned an income band, ie 0-10k, 10-50k, 50-100k, same applies when the grouping is changed to the Group Level.
In the above
ID01 5300 0-10000
ID02 13000 10000-20000
or if by Adviser Group
ID03 18300 10000-20000
In SQL, I could group by the key necessary, then add a column and case statement to allocate each aggregated amount to a band.
The facttable has about 2 million rows.
The income is how much is generated from the advisers and adviser group, so this will change over time.
I can't think of any way to do it with a dimension, as there is no way to link back to the fact table seeing as these are groupings done at run time.
I now have something that works
with
MEMBER Measures.[Calc Sum] as
IIF( Sum(EXISTING [Dim IFA Details].[Parent Key].[Adviser Group].Members,
Measures.[Amount] * -1
) <= 10000 , '0-10000', IIF( Sum(EXISTING [Dim IFA Details].[Parent Key].[Adviser Group].Members,
Measures.[Amount] * -1
) <= 20000 , '10001 - 20000' , '>20000' ))
SELECT { [Measures].[Amount] , measures.[Calc Sum] }
ON COLUMNS,
NONEMPTY( [Dim IFA Details].[Parent Key].[Adviser Group].Members , [Measures].[Amount])
on rows
FROM [Income and Emails Cube]
Its going to have a rather bulky IIF when I add more bands.
Is it possible to create a dimension with the categories and add the keys on the fly like this?
or would there be no benefit.

How to display null values for a field if some other field has values in Oracle

We have requirement wherein we need to display the sum of line cost for all the labor, material, service, tools associated to work order in Maximo. I have written the query however the sum of material line cost is getting doubled of there are more than one service line cost.
For example
wonum - 1234
material line cost - 10
service line cost - 5 and 6 ( 2 service lines)
total material line cost - 20
total service line cost - 11
The total for material line cost is wrong. I have used the below query, please let me know how to fix it
select a.wonum,a.description,a.location,a.crewid,a.worktype,a.wopriority,a.supervisor,a.actstart,a.siteid,sum(d.linecost) as totalmaterialcost,
sum(b.loadedcost)as totalservicecost
from workorder a
left outer join matusetrans d
on a.wonum=d.refwo and a.siteid=d.siteid
left outer join servrectrans b
on a.wonum=b.refwo and a.siteid=b.siteid
where a.wonum='1234' and a.siteid='ABC'
group by a.wonum,a.description,a.location,a.crewid,a.worktype,a.wopriority,a.supervisor,a.actstart,a.siteid
The problem is that the joins are all at the top level of your statement. This leads to multiple lines/records per workorder.
One solution would be to calculate the sum of matusetrans and servrectrans in two seperate sub-select-statements.
Example:
select a.wonum,
b.sum as totalservicecost,
d.sum as totalmaterialcost
from workorder a
left join (
select sum(b.loadedcost) as sum, b.siteid, b.refwo
from servrectrans b
group by b.siteid, b.refwo
) b on a.wonum = b.refwo and a.siteid = b.siteid
left join (
-- second sum-select goes here
) d on -- second join condition goes here
As a second approach check the workorder-table for columns already containing this data (eventually there is some de-normalization to boost performance).
The WORKORDER table will calculate these values for you already. Are the following fields not providing the data you need?
ACTINTLABCOST
ACTLABCOST
ACTMATCOST
ACTOUTLABCOST
ACTSERVCOST
ACTTOOLCOST
ESTATAPPRINTLABCOST
ESTATAPPRLABCOST
ESTATAPPRMATCOST
ESTATAPPROUTLABCOST
ESTATAPPRSERVCOST
ESTATAPPRTOOLCOST
ESTINTLABCOST
ESTLABCOST
ESTMATCOST
ESTOUTLABCOST
ESTSERVCOST
ESTTOOLCOST

How to calculate a median on a group in SSRS 2008 R2

I'm using SSRS 2008 R2 to do some reports in my project, In one of my report i want to display the median of login duration along with average total etc. and the users are grouped by region and country and I want the median of that group.
I have tried following link
http://blogs.msdn.com/b/robertbruckner/archive/2008/07/20/using-group-variables-in-reporting-services-2008-for-custom-aggregation.aspx
but here I'm getting only the median of entire login duration not for the grouped items while using this so all the medians are same. it should be different for different groups by region or country.
I had the same issue, and the problem is that you can only use the vbCode only on one set of data, since there is a global shared variable "values".
To tackle this issue, I used the following query inside a subreport for each
Median:
Select Median.*, (Convert (decimal, Median.bh) + Convert(decimal, Median.th))/2 as MedainResult
from(
sELECT
(SELECT max(finalGrade)
from
( SELECT top 50 percent finalGrade
FROM GradeTable
WHERE (TermCode = 201410) (CRN = 11735)
order by finalGrade) as BottomHalf) as bh
,
(select min(finalGrade)
from
( SELECT top 50 percent finalGrade
FROM GradeTable
WHERE (TermCode = 201410) (CRN = 11735)
order by finalGrade desc
) as TopHalf
) as th
) as Median