SUM the max values over multiple groupings. DAX SSAS - scope

Link to Table...
http://i.stack.imgur.com/hUVfc.png
I have a table with multiple repeated values associated with a certain group. I'm trying to scope out the max for each group, then sum it.
Answer needs to be 50 + 25 + 10 + 70
I've tried various SUMX and can't seem to get the filter correct.
Thanks,

Maybe:
My Measure := SUMX(VALUES(Table1[Group]),CALCULATE(MAX(Table1[Value])))

Related

Create custom group for numeric range in pivot table

I know that I can achieve what I need with a set of extra columns in the PivotTable Data Source, but I wonder if there is an alternative way to achieve this.
I have a table with Customers Orders; for each order (i.e. each row) I have the customer name and the indication of the nr of days since last order.
I am trying to put up a PivotTable that has the Customer names in the rows, the nr. of orders in the values and the following groups in the columns:
a) < 7 days
b) 7 <= days < 14
c) 14 <= days < 28
d) >= 28 days
So far, using the "group field" option, I could create the following groups:
i) < 7 days
ii) 7 <= days < 14
iii) 14 <= days < 21
iv) 21 <= days < 28
by setting:
Starting at = 7
Ending at = 28
By = 7
I could live with the extra group (iii), problem is that it does not include the group (d); said differently the grouping acts like a filter which is not what I want.
As said I can add columns for groups a-d, but is there a way to achieve this without?
As far as I know you can only add one group to a pivot table spaced at a regular interval. What I tend to do in these situations is either add a single column to the data source with a nested if formula or to add a secondary table and use a lookup formula.
For example,
The formula below finds the group in the secondary table where days are greater than or equal to the lower bound AND less than or equal to the upper bound.
=INDEX(Table2[Group],MATCH(1,([#Days]>=Table2[Lbound])*([#Days]<=Table2[Ubound]),0))

how to choose and sum data from excel table automatically

I'm setting up an excel data table that i need to make a monthly statistics from it. how can i automatically choose a characteritic from the table and get all its values sumed up ?
This is a garage based data, that includes daily entries and the productivity of every technicien on a daily basis. What i'm asking for is, can i find a way to sort all 5 techniciens in a different table and get their productivity summed up automatically ?
here is the data table and an example for what i am trying to do.
n Date CLIENT Date d'entrée MO Techniciens HT€ Prestation
1 01/03/2019 YAYA 01/03/2019 30 900 FLORENT 30 900 Passage au banc
2 05/03/2019 DAVID 05/03/2019 30 000 LOBA 30 900 Passage au banc
3 07/03/2019 NAME 20/02/2019 60 000 YAPO 60 000 Ctrle injecteurs
4 08/03/2019 MATFORCE 01/03/2019 39 000 LOBA 42 151 Passage au banc
what i want is to have another table with the techniciens name in it and the sum of HT€
technicien heures factures/mois
PRINCE 10 000
MOUSSA
ETIBOIS
Select Data
Insert -> Pivot Table
Set Technicians as Rows and Sum of HT as Values
Source data was slightly altered to better illustrate the answer
Like #Marc in the comment to the question, I would suggest SUMIF(), SUMIFS() or Pivot Tables.
Pivot Tables are the easiest as they can SUM by multiple criteria without a lot of programming. But they only do one thing at a time so SUM and AVERAGE need to be done separately.
SUMIF(range,criteria, sumrange) will return the sum of all of the cells in sumrange that are in the same row where the value in the range meets the criteria for example:
=SUMIF(A1:A999,="Charlie",B1:B999)
Where A1:A999 has the technicians' names, B1:B999 has their billing amounts
SUMIFS() is similar but allows multiple selection criteria.

DAX. Problem with subtotals and grand totals

hope you are doing well and can help solve this puzzle in DAX for PowerBI and PowerPivot.
I'm having troubles with my measure in the subtotals and grand totals. My scene is the following:
I have 3 tables (I share a link below with a test file so you can see it and work there :robothappy:):
1) "Data" (where every register is a sold ticket from a bus company);
2) "Km" (where I have every possible track that the bus can do with their respective kilometer). Related to "Data";
3) and a "Calendar". Related to "Data".
In "Data" I have all the tickets sold from a period with their price, the track that the passenger bought and the departure time of that track.
Each track can have more than 1 departure time (we can call it a service) but only have a specific lenght in kilometers (their kilometers are specified in the "Km" table). 
Basically what I need is to calculate the revenue per kilometer for each service in a period (year, month, day).
The calculation should be, basically:
Sum of [Price] (each ticket sold in the period) / Sum of [Km] (of the period considerating the services with their respective kilometers)
I managed to calculate it for the day granularity with the following logic and measures:
Revenue = SUM(Data[Price])
Unique dates = DISTINCTCOUNT(Data[Date])
Revenue/Km = DIVIDE([Revenue]; SUM(Km[Km])*[Unique dates]; 0)
I created [Unique dates] to calculate it because I tried to managed the subtotals of track granularity taking into account that you can have more than 1 day with services within the period. For example:
For "Track 1" we have registered:
1 service on monday (lunes) at 5:00am.
Revenue = $1.140.
Km = 115.
Tickets = 6.
Revenue/Km = 1.140/115 = 9,91.
1 service on tuesday (martes) at 5:00am.
Revenue = $67.
Km = 115.
Tickets = 2.
Revenue/Km = 67/115 = 0,58.
"Subtotal Track 1" should be:
Revenue = 1.140 + 67 = 1.207.
Km = 115 + 115 = 230.
Tickets = 6 + 2 = 8.
Revenue/Km = 1.207/230 = 5,25.
So at that instance someone can think my formula worked, but the problem you can see it when I have more than 1 service per day, for example for Track 3. And also this impact in the grand total of march (marzo).
I understand that the problem is to calculate the correct kilometers for each track in each period. If you check the column "Sum[Km]" is also wrong.
Here is a table (excel file to download - tab "Goal") with the values that should appear: 
[goal] https://drive.google.com/file/d/1PMrc-IUnTz0354Ko6q3ZvkxEcnns1RFM/view?usp=sharing
[pbix sample file] https://drive.google.com/file/d/14NBM9a_Frib55fvL-2ybVMhxGXN5Vkf-/view?usp=sharing
Hope you can understand my problem. If you need more details please let me know.
Thank you very much in advance!!!
Andy.-
Delete "Sum of Km" - you should always write DAX measures instead.
Create a new measure for the km traveled:
Total Km =
SUMX (
SUMMARIZE (
Data,
Data[Track],
Data[Date],
Data[Time],
"Total_km", DISTINCT ( Data[Kilometers Column] )
),
[Total_km]
)
Then, change [Revenue/Km] measure:
Revenue/Km = DIVIDE([Revenue], [Total Km])
Result:
The measure correctly calculates km on both subtotal and total levels.
The way it works:
First, we use SUMMARIZE to group records by trips (where trip is a unique combination of track, date and time). Then, we add a column to the summary that contains km for each trip. Finally, we use SUMX to iterate the summary record by record, and sum up trip distances.
The solution should work, although I would recommend to give more thoughts to the data model design. You need to build a better star schema, or DAX will continue to be challenging. For example, I'd consider adding something like "Trip Id" to each record - it will be much easier to iterate over such ids instead of grouping records all the time. Also, more descriptive names can help make DAX clean (names like km[km] look a bit strange :)

DAX measure to iterate each row for correct division (for total as well)

I am not sure if there's a way in DAX to create a measure that would help me with the following:
Calculate the efficiency by day
Display the total efficiency in a pivot table / PowerBI matrix as the overall total and not as sum of the daily efficiency
Here's a simple example:
Where:
Total Categories = Category1 + Category2 + Category3
Efficiency = (Total Categories + Category4*0.33)/Category4
At first I've created measures for each category (e.g. TotalCateg1 = SUM[Category1] etc.) and hopping to get the right result in the end. My problem is I am not able to get both the daily efficiency and the total right. Is there a way around it?
For Total Categories use this formula:
=SUM([Category 1])+SUM([Category 2])+SUM([Category 3]) .
then for the Efficiency use this formula:
=([Total Categories]+SUM([Category 4])*0.33)/SUM([Category 4])

Sum of Averages in Excel Pivot Table

I am measuring room utilization (time used/time available) from a data dump. Each row contains the available time for the day and the time used for a particular case.
The image is a simplified version of the data.
If you read the yellow and green highlights (Room 1):
In room 1, there are 200 available minutes on 1/1/2016.
Case 1 took 60 minutes, case 2 took 50 minutes.
There are 500 available minutes on 1/2/2016, and only one case occurred that day, using 350 minutes.
Room 1 utilization = (60 + 50 + 350)/(200 + 500)
The problem with summing the available time is that it double counts the 200 minutes for 1/1/2016, giving: Utilization = (60+50+350)/(200+200+500)
There are hundreds of rows in this data (and there will be multiple data dumps of differing #'s of rows) with multiple cases occurring each day.
I am trying to use a pivot table, but I cannot obtain the 'sum of averages' for a particular room (see image). I am using a macro to pull the numbers out of the grand total column.
Is this possible? Do you see another way to obtain utilization?
(note: there are lots of other columns in the data, like case start, case end, day of week, etc, that are not used in this calculation but are available)
The reason that you're getting 300 for both Average of Available Time columns is because the grand total is a grand total based on the overall average and not a sum of the averages.
Room 1: 200 + 200 + 500 / 3 = 300
Room 2: 300 + 300 + 300 / 3 = 300
I could not comment on the original question, so my solution is based on a few assumptions.
Assumption #1: The data will always be grouped. E.G. All cases in room 1 on a given day will grouped in sequential rows.
Assumption #2: The available time column is a single value for the whole day, there will never be differing available times on the same day.
Solution: Use column E as the Actual Available Time. This column will use a formula to determine if the current row has a unique combination (Date + Room + Available Time) to the previous and if so, the cell will contain that row's available time.
Formula to use in E2:
=IF(AND($A1 = $A2, $B1 = $B2, $C1 = $C2), 0, $C2)
Extend the formula as far down as necessary and then include the new column in your PivotTable data range.
End Result
I created a unique reference by combining columns and then used sumif/countif/countif.
So the formula in column E would be:
=sumif(colB,cellB,ColC)/Countif(colB,cellE)/Countif(colB,cellE)
Doesn't matter if the data is in order or not then.
Extend the formula as far down as necessary and then include the new column in your PivotTable data range.
The easiest method I would recommend is this.
=SUM(H:H)-GETPIVOTDATA("Average of Available Time",$G$3)
The first term sums the H column, and the second term subtracts the grand total value. It is a dynamic solution, and will change to fit the size of the pivot table.
My assumptions are that the Pivot Table was originally placed in cell G3.

Resources