Powerpivot: Retrieve max value for a group in a related table - max

I have 2 tables with a one-to-many relationship.
-TableGroup: table with groupletter
-TableAll: table with unique identifier, groupletter, a date
Problem: I want to get the max value of the date from TableAll into a new column in TableGroup. See below.
Question: What is the formula for column MAXdate?
TableAll:
ID | Group | date
1 A 4/01/2017
2 A 2/10/2016
3 A 2/06/2016
4 B 2/12/2016
5 B 15/12/2016
6 B 2/03/2017
7 C 5/02/2016
8 C 16/01/2016
TableGroup:
Group | MAXdate
A 4/01/2017
B 2/03/2017
C 5/02/2016
The below formula doesn't work:
See here

The answer is:
CALCULATE (
MAX ( TableAll[Date] ),
FILTER ( TableAll, TableAll[Group] = EARLIER ( TableGroup[Group] ) )
)

Try:
CALCULATE (
MAX ( TableAll[Date] ),
FILTER ( TableGroup, TableGroup[Group] = EARLIER ( TableGroup[Group] ) )
)
How it works:
EARLIER ( TableGroup[Group] ) expression essentially means "current row". Filter function goes row by row over TableGroup table, filters it by current row's group, and then finds max date for that group.

Related

DAX Query to group column with getting its average of another column where date column is latest one

Let's say I have a table which contains school data with schoolName, StudentCount, Year columns.
What I want is get average of studentCount for latest year by school.
It may possible that school 1 's latest year is 2021 where as school 2 's latest year is 1990.
This is probably late but I guess it can help someone.
The table is named SchoolData and the goal is to average the last record for each school across all schools.
DEFINE
VAR SchoolAndMaxYear = //LastYearForEachSchool
GROUPBY (
SchoolData,
SchoolData[SchoolName],
"YearValue", MAXX ( CURRENTGROUP (), SchoolData[YearValue] )
)
VAR StudentCountForSchoolAndMaxYear =
CALCULATETABLE (
SchoolData,
TREATAS ( SchoolAndMaxYear, SchoolData[SchoolName], SchoolData[YearValue] )
)
EVALUATE StudentCountForSchoolAndMaxYear
EVALUATE
ROW (
"AVGStudentCountLyBySchool", AVERAGEX ( StudentCountForSchoolAndMaxYear, SchoolData[StudentCount] )
)

How to modify DAX ALLEXCEPT to allow filtering

I have a Table called Tracking, as shown below:
Category
Item Id
Work Date
A
1
1/1/2020
B
1
2/1/2020
C
1
3/1/2020
D
1
4/1/2020
A
2
7/1/2020
B
2
8/1/2020
C
2
9/1/2020
D
2
10/1/2020
Using the below measure, I can get Maximum Work Date from the above Table.
maxWorkDate =
CALCULATE (
MAX ( 'Tracking'[Work Date] ),
ALLEXCEPT (
'Tracking',
'Tracking'[Item Id]
)
)
For Item 1, the maximum work date is 4/1/2020 and for Item 2, the maximum work date is 10/1/2020.
I want to get maximum work date by Excluding Category D. So, For Item 1, the maximum work date should be 3/1/2020 and for Item 2, the maximum work date should be 9/1/2020.
I would like to exclude category D within the measure or I can also exclude it in Visual Filters. Please advise. Thanks
It's possible to implement the measure with a filter expression 'Tracking'[Category] <> "D" like follows
maxWorkDate =
CALCULATE(
MAX( 'Tracking'[Work Date] ),
REMOVEFILTERS( 'Tracking' ),
VALUES( 'Tracking'[Item Id] ),
'Tracking'[Category] <> "D"
)
This is what FILTER is for:
maxWorkDate =
CALCULATE (
MAX ( 'Tracking'[Work Date] ),
FILTER (
ALLEXCEPT (
'Tracking',
'Tracking'[Item Id]
),
'Tracking'[Category] <> "D"
)
)

Finding Percentile of a calculated measure in PowerPivot / DAX

A table that is similar to the data set I am working on (although much simpler) is below that I would like to calculate some measures on and then find the percentiles of the measures.
Table Name: Data
Owner AgeRating OtherRating
A 1 2
A 4 4
A 4 6
B 3 3
B 3 9
B 7 4
C 8 8
C 4 2
First - A little background: I start by taking an average of the ratings (By Owner) and then normalize all ratings by dividing each rating by the maximum owner's rating - This creates the measure I would like to take the percentile of:
NormAgeRating=
average(Data[AgeRating])/
calculate(
maxx(
SUMMARIZE(Data,[Owner],"avg",average([AgeRating]))
,[avg]
)
,all(Data[owner])
)
I have a pivot table with Rows being the owner which then looks like
Owner NormAgeRating
A .5
B .72
C 1
Now for the question:
I would like to get the .33 percentile.inc of the new NormAgeRating. I would like to use this to classify each owner into groups (<=33%ile or > 33%ile)
This is what I am trying to get to:
Owner NormAgeRating 33%ile classification
A .5 .64 bottom
B .72 .64 top
C 1 .64 top
I have tried this with no success and many other variation with different groupby's etc. and continually get the wrong value:
33%ile=percentilex.inc(all(data[owner]),[NormAgeRating],0.33)
Any help would be greatly appreciated
Update:
When I try sumx countx and averagex in the form:
=
averagex(
SUMMARIZE(
all(Data[Owner]),
[Owner],
"risk",[NormAgeRating]),
[risk]
)
I am getting the right values, so I am not sure why using percentilex.inc/exc would produce the wrong values...
PERCENTILEX (and all iterator functions) operates row by row on the table in the first argument. Therefore, you need that table to be at the desired granularity before you try to compute the percentile, which means you need to summarize Data[Owner] so that you have a unique row per owner rather than iterating over the raw column.
Keeping this in mind, both measures can be written similarly:
NormAgeRating =
DIVIDE (
AVERAGE ( Data[AgeRating] ),
MAXX (
SUMMARIZE (
ALL ( Data[Owner] ),
Data[Owner],
"Avg", AVERAGE ( Data[AgeRating] )
),
[Avg]
)
)
33%ile =
PERCENTILEX.INC (
SUMMARIZE (
ALL ( Data[Owner] ),
Data[Owner],
"Risk", [NormAgeRating]
),
[Risk],
0.33
)

Ranking results from a SUMMARIZECOLUMNS operation

I created the following DAX code in DAX Studio which works correctly:
EVALUATE
SUMMARIZECOLUMNS(
'Florida Sightings'[Locality Id],
Hotspot[Subnational 1 Code],
Hotspot[Name],
'Calendar'[Month],
FILTER(Hotspot, Hotspot[Subnational 1 Code] = "US-FL"),
"Species Count", COUNTROWS(VALUES('Florida Sightings'[Common Name]))
)
The output looks like this, sorted by month and species count:
I would like to take the results of the SUMMARIZECOLUMNS and add a rank column based on species count for each locality Id and month. So, for the first locality Id (L127258) and Month (1), the rank would be 1. And, for the second locality Id (L123565) and month (1), the rank would be 2 etc.
The months run from 1 through 12 for each locality.
Without sample data to work with, I'm shooting in the dark a bit but try something along these lines:
ADDCOLUMNS (
SummaryTable,
"Rank", RANKX (
FILTER (
SummaryTable,
[Locality Id] = EARLIER ( 'Florida Sightings'[Locality Id] )
&& [Month] = EARLIER ( 'Calendar'[Month] )
),
[Species Count]
)
)

how to sum day wise unique count to MTD

I want to calculate tasks per day(no of tasks/no of unique users) in my dashboard.
I used the distinct count to get the unique no of employees. When I put the measure in pivot, day wise calculation is okay but at the end in MTD it is not added the day wise unique no of emp.
Tasks per day:=divide(sum(fdata[resolved]),distinctcount(fdata[sap id]))
I expect the output like second table.
Model
Date ID Resolved
12-05-2019 a 1
12-05-2019 a 1
12-05-2019 b 1
13-05-2019 a 1
13-05-2019 b 1
13-05-2019 c 1
Expected Result
Date No of emp Resolved Task Per Day
12-05-2019 2 3 1.5
13-05-2019 3 3 1
Grand Total **5** 6 1.2
This will do the trick:
Tasks per day:=
IF(
ISFILTERED( fdata[date] )
, DISTINCTCOUNT( fdata[sap id] )
, COUNTROWS(
GROUPBY(
fdata
, fdata[date]
, fdata[sap id]
)
)
)

Resources