Compute a calculation over SUMMARIZE in DAX - excel

I have the following scenario. I have students who pass test. They may have more than 1 supervisor at the same time. I would like to create a calculation in DAX that computes the average score at every level (i.e. department, supervisor, student).
The original table contains a single test per student, but I've "left joined" this table with a newly created one, student-supervisor, so I can compute also the score over the supervisor. The problem is when I compute the average score per department, because it contains all the duplicates I created with this new table.
These are my tables:
And this is my model:
The obvious DAX that just computes the average of the score works fine for Students and Supervisors on the PivotTable below, but it's wrong at a department level:
Avg Score:=AVERAGE(score[Score])
At this point I've tried something like the following, but without success. My point was to create a dynamic table with SUMMARIZE that groups by testid and does the average of score. Then I wanted to average that again, which would be the correct score and convert that 1column-1row into a numeric value. But this doesn't work, and I'm not sure why:
Avg Score= VAR ThisTable=SUMMARIZE(score,score[TestId],"IndividualScore",AVERAGE(score[Score])) RETURN SUMMARIZE(ThisTable,"AvgScore",AVERAGE([IndividualScore]))

This is the way I'd approach it.
First create a measure like the below to get the score in each context:
Sum Score := MAX(Score[Score])
Then create the average calculation measure:
Avg Score :=
DIVIDE (
SUMX ( DISTINCT ( Score[Student] ), [Sum Score] ),
DISTINCTCOUNT ( Score[Student] )
)
Note the Sum Score measure is required because the Avg Score measure uses it to perform the calculation.
You will get something like this Matrix (Pivot Table) in Power BI:
Let me know if this helps.

Alright, so thanks to alejandro's idea I could figured out the answer. Basically I'm creating an on-the-fly table with a group by test id and the average score (i.e. the real score). Then I'm using AVERAGEX to compute the average of those test. Here's the DAX code:
Avg CSAT:=VAR ThisTable=SUMMARIZE(score,score[TestId],"SumOfScore",AVERAGE(score[Score])) RETURN AVERAGEX(ThisTable,[SumOfScore])

Related

Power Pivot: Average by group using many-to-many table relationship

I have three tables in Power Pivot. Pupils, Baselines and Reports. The Pupils table connects to the Reports and Baselines tables via a One to Many relationship as shown below.
The Reports table has each pupil listed multiple times for each subject they have a report in. They are also listed multiple times in the Baselines table for each baseline score they have. What I want to accomplish is to create a measure that calculates the average baseline score in each subject. So take all pupils who have a maths report, I want to know the average baseline score in maths.
I tried the following measure:
Av Baseline:=AVERAGEX( CALCULATETABLE( Baselines, Baselines[Type] = "Overall" ), Baselines[Average] )
However the above when used in a pivot table produces the same result for all subjects, so the filter context is not being applied. I keep hearing people using bridge tables, which does add a ton of data and are not very efficient so I hope to avoid those if at all possible.
I have provided some example data with the desired output table, hope that helps?
Could you please test this:
Note: I hope Your Pupils table has a column called [Subject Code] It is not clear from your shared data.
Version - 1
Av Baseline :=
AVERAGEX (
VALUES ( Pupils[Subject Code] ),
CALCULATE (
SUM ( Baselines[Average] ),
CALCULATETABLE ( Baselines, Baselines[Type] = "Overall" )
)
)
Version - 2
Av Baseline =
VAR Combin =
SUMMARIZE (
Baselines,
Baselines[Type],
Pupils[Subject Code],
Baselines[Result]
)
VAR Combinfiltered =
FILTER ( Combin, Baselines[Type] = "Overall" )
VAR Result =
CALCULATE ( AVERAGE ( Baselines[Result] ), Combinfiltered )
RETURN
Result

Calculate monthly efficiency in power pivot

I am very new to this power pivot and dax Commands ,kindly help me out here
my sample table looks like this :
Now to calculate monthly efficiency i need to make a measure which satisfies below criteria:
Sum the efficiency for each distinct date & divide by the count of distinct dates for the month
so far for days my below formula is working , but when i group the date into month it is only showing the sum, so kindly help me how to achieve this ;
Actual Efficiency CL2:=CALCULATE(SUM(CL1[Day Wise Efficiency]),DISTINCT(CL1[Date (dd/mm/yy)]))
Divide part is not in your formula, below DAX might help:
Actual Efficiency CL2 :=
DIVIDE (
CALCULATE ( SUM ( CL1[Day Wise Efficiency] ) ),
CALCULATE ( DISTINCT ( CL1[Date (dd/mm/yy)] ) )
)
Note that DISTINCT ( CL1[Date (dd/mm/yy)] ) is not required in the sum as dates in rows will be unique.
To Divide the sum at month level DISTINCT ( CL1[Date (dd/mm/yy)] ) is required as we need how many days a month has.
At day level there will always be 1 to divide by.
Thanks

Calculate median of aggregated values for a category using pivot table

I have hourly data about sales $ of certain categories (Fruits, snacks etc). I'd like to display the median of daily sales values over a month or an year using the hourly data in the table. So, for each category it needs to sum the hourly values and create a daily value first and then calculate the median
regular pivot does not provide median function. I am familiar with Power pivots but have not used Measures. i tried to create a measure using medianX function, but could not make it work
First, you will need to add a Calendar table to your data model:
Calendar Table Explained
The Calendar table must have fields you want to group your sales by, such as "Year-Month" and "Year-Quarter".
Then, connect the Calendar table to your Sales table via date fields, to get a structure like this:
With this data model in place, create 2 measures ( I will assume your sales table is called "Sales", and calendar table is called "Date"):
Measure 1:
Total Sale = SUM(Sales[Amount])
It will simply calculate sum of sale amounts.
Measure 2:
Median Monthly Sale = MEDIANX( VALUES(Date[Year-Month]), [Total Sale])
This measure will first sum up sales by Year-Month, and then calculate the median of the sums.
To get median sales by a different period, just replace Year-Month with the desired grouping level, for example:
Median Yearly Sale = MEDIANX( VALUES(Date[Year]), [Total Sale])
Ali,
When you create your measure you will want to use the MEDIAN funciton not MEDIANX
The MedianX would be if you were trying to calculate the median value for a column in your table that did not already exist.
For Example, lets say we have Table1 which has two columns Net Revenue and Net Expense. And the following 3 data points exist for each row in the column.
Net Revenue:
Jan-2019 100
Feb-2019 300
Mar-2019 300
Net Expense:
Jan-2019 50
Feb-2019 100
Mar-2019 50
Since we do not have a Net Profit column in the table we could use MEDIANX to specify the table we would like to use then write our expression for the column that doensn't exist. In the example above it would go =MEDIANX(Table1, [Net Revenue] - [Net Expense]. This would create a median measure for NetProfit even though the column itself doesn't exist in our table.
In your case, since you are aggregating a column that already exists you can just stick with =MEDIAN.
If you need more clarification let me know!
Best,
Brett

How can I get a percentage field with power pivot?

This should be a fairly easy question for Power Pivot users since I'm a newbie. I am trying to do the following. After pivoting a table I get a crosstab table like this
rating count of id
A 1
B 2
Grand Total 3
You can imagine the original table only has two columns (rating and id) and three rows (1 id for A and two different id's for the B rating). What DAX formula do I have to write in order to create a measure that simply shows
rating percent of id
A 1/3
B 2/3
Grand Total 3/3
By 1/3 of course I mean 0.3333, I wrote it like that so that it is clear that I simply want that percent of id is the count for each rating divided by the total count. Thank you very much
You need to divide the count for each row by the total count.
DIVIDE (
COUNT ( Table1[ID] ),
CALCULATE ( COUNT ( Table1[ID] ), ALL ( Table1 ) )
)
For this particular calculation, you don't have to write DAX though. You can just set it in the Value Field Settings.
Summarize Value By : Count
Show Values As : % of Column Total

How to refer to a single value in a Calculated Column in a measure (DAX)

I have a table(Data_all) that calculates daycount_ytd in one table.
[Date] is in Date Format.
[Fiscal Year] is just year. eg: 2016
Calculated Column
daycount_ytd=DATEDIFF("01/01/"&[Fiscal Year],Data_all[Date],day)+1
Im trying to create a measure that refers to this Calculated Column
Measure:
Amt_X Yield %:=[Amt X]/([Amt Y]/365* (Data_all[DayCount_YTD]))
I get the error that Data_all[DayCount_YTD] refers to a list of values.
How do i filter the expression to get a single value without using a aggregation function eg:(sum, median)?
Or perhaps, is there another way to achieve the same calculation?
You've arrived an a fundamental concept in DAX and once you've worked out how to deal with it then the solution generalises to loads of scenarios.
Basically you can't just pass columns into a DAX measure without wrapping them in something else - generally some kind of mathematical operation or you can use VALUES() depending on exactly what you are trying to do.
This measure will work OK if you use it in a PIVOT with the date as a row label:
=
SUM ( data_all[Amt X] )
/ (
SUM ( data_all[Amt Y] ) / 365
* MAX ( data_all[daycount_ytd] )
)
However you will see it gives you an incorrect total as it is in the latest for the entire thing. What you need is a version that iterates over the rows and then performs a calculation to SUM or AVERAGE each item. There is a whole class of DAX functions dedicated to this such as SUMX, AVERAGEX etc. You can read more about them here
It's not totally clear to me what the maths behind your 'total' should be but the following measure calculates the value for each day and sums them together:
=
SUMX(
VALUES(data_all[date]),
SUM(data_all[Amt X]) /
(SUM(data_all[Amt Y]) / 365 * MAX(data_all[daycount_ytd]))
)

Resources