Summing by Column - obiee

Suppose we have the following columns:
X Y Z
Category Date Amount
A January 10
A February 20
A March 30
B January 34
B February 45
B March 65
C January 87
C February 98
C March 100
D January 80
D February 90
I want to sum the Amount column by Category and Date . So for Category A, we would have the sum of the amount be 10+20+30 = 60 for the dates between January and March. In Oracle BI, how would we do this? Note that Some categories might have missing dates. So I want to sum the Amounts for the only the the available dates between January and March. Category D, for example, has March missing. So the total amount would be 80+90 = 170.
When I do the following, I just get the sum of all the amounts:
sum("Z"."Amount")

If the required result has to be achieved through OBIEE Answer, then it can be done in following way.
Create a table with columns - Category, Date, Amount.
Go to Results tab. Edit view of the table.
Click on Total By icon above Category column. Both After and Report-Based Total (when applicable) should be ticked.
The result will be coming as shown.
Category Date Amount
A January 10
February 20
March 30
A Total 60
B January 34
February 45
March 65
B Total 144
C January 87
February 98
March 100
C Total 285
D January 80
February 90
D Total 170

You can do this quite simply by editing the column formula from within the Criteria. When you look at it to begin, your Amount column formula probably looks something like "Z"."Amount". You can edit this slightly to change the aggregation level:
sum("Z"."Amount" by "X"."Category")
That should give you something like:
Category Date Amount
A Jan 60
A Feb 60
A Mar 60
B Jan 144
B Feb 144
B Mar 144

Related

Dividing within a column based on other columns matching

I am trying to calculate the current measurement in column “Total" minus the lowest measurement previously recorded in column "Total" where the current measurement in column “Total” corresponding to the value in column "Trade" is less than(<) the minimum measurement in column “Total” corresponding to the value in column "trade", and if two values in the “SUBJECT” column match and two values in the “PROCEDURE” column match. To emphasize, the minimum value must be a previously recorded. If the measurement is less than the current measurement but was not recorded previously (according to the “date” column), it does not quality to be subtracted from the current measurement. An example of the output is provided below.
data Have;
input Subject Type :$12. Date &:anydtdte. Trade Procedure :$12. Measurement;
format date yymmdd10.;
datalines;
Subject Type Date Trade Procedure Total
500 Initial 15 AUG 2017 6 Invasive 20
500 Initial 15 AUG 2017 9 Surface 35
500 Followup 15 AUG 2018 8 Invasive 54
428 Followup 15 AUG 2018 56 Outer 29
765 Seventh 3 AUG 2018 12 Other 13
500 Followup 3 JUL 2018 23 surface 98
428 Initial 3 JUL 2017 34 Outer 10
765 Initial 20 JUL 2019 4 Other 19
610 Third 20 AUG 2019 58 Invasive 66
610 Initial 17 Mar 2018 25 Invasive 17
*Example of Output;
Subject Type Date Trade Procedure Total Output
500 Initial 15 AUG 2017 6 Invasive 20 20/20
500 Initial 15 AUG 2017 9 Surface 35 35/35
500 Followup 15 AUG 2018 8 Invasive 54 54/20
428 Followup 15 AUG 2018 56 Outer 29 29/10
765 Seventh 3 AUG 2018 12 Other 13 13/19
500 Followup 3 JUL 2018 23 surface 98 98/35
428 Initial 3 JUL 2017 34 Outer 10 10/10
765 Initial 20 JUL 2019 4 Other 19 19/19
610 Third 20 AUG 2019 58 Invasive 66 66/17
610 Initial 17 Mar 2018 25 Invasive 17 17/17
not sure, but this is the closest thing i could get to match your output
I made a monotonic() variable, and then ranked it by SUBJECT and PROCEDURE variables. and then joined the table to itself using condition t1.rank_monotonic +1 = t2.rank_monotonic.

How can I modify a 2d matrix so that predefined sums in both dimensions are satisfied?

I'm working on optimizing production of a number of related widgets over a number of weeks. The total quantity of each widget, and the total quantity of widgets produced each week is fixed. By default, a few of each widget is produced each week.
For example:
Week: 1 2 3 4 5 Total
Widget A: 10 10 20 10 10 60
Widget B: 20 20 40 20 20 120
Widget C: 15 10 5 15 15 60
Totals: 45 40 65 45 45 240
However, due to overhead and setup time in the factory, I'd like the ability to reduce the number of types of widgets produced each week. For example, I'd like the user to be able to delete a number of the weekly widget runs, like this:
Week: 1 2 3 4 5 Total
Widget A: 10 __ 20 10 10 60
Widget B: 20 20 40 __ 20 120
Widget C: 15 __ 5 __ 15 60
Totals: 45 40 65 45 45 240
Given the above input, how could I code a solution to modify the numbers produced per widget per week, so that the total quantity produced per week, and the total quantity produced per widget overall, still satisfies the original totals?

Finding cummulative sum of MAX values

I need to calculate the cumulative sum of Max value per period (or per category). See the embedded image.
So, first, I need to find max value for each category/month per year. Then I want to calculate the cumulative SUM of these max values. I tried it by setting up max measure (which works fine for the first step - finding max per category/month for a given year) but then I fail at finding a solution to finding cumulative SUM (finding the cumulative Max is easy, but it is not what I'm looking for).
Table1
Year Month MonthlyValue MaxPerYear
2016 Jan 10 15
2016 Feb 15 15
2016 Mar 12 15
2017 Jan 22 22
2017 Feb 19 22
2017 Mar 12 22
2018 Jan 5 17
2018 Feb 16 17
2018 Mar 17 17
Desired Output
Year CumSum
2016 15
2017 37
2018 54
This is a bit similar to this question and this question and this question as far as subtotaling, but also includes a cumulative component as well.
You can do this in two steps. First, calculate a table that gives the max for each year and then use a cumulative total pattern.
CumSum =
VAR Summary =
SUMMARIZE(
ALLSELECTED(Table1),
Table1[Year],
"Max",
MAX(Table1[MonthlyValue])
)
RETURN
SUMX(
FILTER(
Summary,
Table1[Year] <= MAX(Table1[Year])
),
[Max]
)
Here's the output:
If you expand to the month level, then it looks like this:
Note that if you only need the subtotal to work leaving each row as a max (15, 22, 17, 54) rather than as a cumulative sum of maxes (15, 37, 54, 54), then you can use a simpler approach:
MaxSum =
SUMX(
VALUES( Table1[Year] ),
CALCULATE( MAX( Table1[MonthlyValue] ) )
)
This calculates the max for each year separately and then adds them together.
External References:
Subtotals and Grand Totals That Add Up “Correctly”
Cumulative Total - DAX Patterns

Calculating which day of the week a date falls on using Gauss's algorithm, ordinal date and modulo arithmetic

After calculating which day of the week the 1st of January falls on using Gauss's algorithm, as well as calculating the ordinal date for a given calendar date, how can the day of the week of the latter date be calculated?
For example, Gauss's algorithm can tell us that, this year, the 1st of January fell on a Sunday, the 7th day of the week. Today is the 22nd of October, with an ordinal day of 295. How can this information be used to calculate that today is a Sunday?
For common years (= non-leap years), 1st of January and 1st of October are on the same day of the week:
Jan 31
Feb 28
Mar 31
Apr 30
May 31
Jun 30
Jul 31
Aug 30
Sep 31
Sum 273 = 39 x 7
See Wikipedia
22nd October is exactly three weeks later than 1st of October.
An approach I've found, which I haven't tested extensively, but seems to work with the dates I've thrown at it, is...
(ordinal day + day of 1st of January - 1) % 7
Where Mon = 1, Tue = 2,..., Sat = 6, Sun = 0.
In the example mentioned in the question:
(295 + 0 - 1) % 7 = 0 (Sunday)

DAX AverageX where table dimension is reduced by one

I'm trying to find the right way to structure a DAX formula to compute a specific average. I think I might be able to construct the average more or less explicitly by using a sum/count construction, but I'm wondering if averagex with an appropriate set of table filters might get the job done.
Specifically, my problem can be explained like this: I'm trying to compute the average cost of a car in DAX, but my data includes the cost of all the components individually (call it body, wheels and engine for now).
Name Year Part Cost
Alice 2000 Engine $10
Alice 2000 Wheels $5
Alice 2000 Body $25
Alice 2001 Engine $8
Alice 2001 Wheels $6
Alice 2001 Body $2
Bob 2000 Engine $10
Bob 2000 Wheels $5
Bob 2000 Body $25
Bob 2001 Engine $8
Bob 2001 Wheels $6
Bob 2001 Body $2
Is there any way to tell DAX that I want to first sum across all the components of the car first, and then compute averages on the data set where the dimensionality of the data has been reduced by one (only the "part" dimension removed)?
For example, the average cost for Alice then would yield
((10+5+25)+(8+6+2))/2 = 28
While if I had a pivot table constructed per name and per year, it would show
Alice 2000 40
Alice 2001 16
etc...
Thanks.
Try this... it works in the case where Name,Year provides a unique combination.
[nCombinations]:=COUNTROWS(SUMMARIZE(Table1,Table1[Name],Table1[Year]))
[TotalCost]:=SUM(Table1[Cost])
[AverageCost]:=CALCULATE([TotalCost]/[nCombinations])
Create a PivotTable with [Name] and [Year] on rows,
Then add [nCombinations] [TotalCost] and [AverageCost] in the body.
Row nCombinations TotalCost AverageCost
Alice 2 56 28
2000 1 40 40
2001 1 16 16
Bob 2 56 28
2000 1 40 40
2001 1 16 16
Grand Total 4 112 28

Resources