Power BI measure to calculate students retained between study periods - powerbi

Just started getting into Power BI/DAX after living in Excel for most of my life.
I'm working on a retention/attrition rate report and I am stuck on how to compare one pool of students in one study period to another pool in another study period.
The calculation is relatively simple:
(Returned Students in Study Period X+1) / (Enrolled students in Study Period X - Graduated Students in Study Period X)
So if I had 110 students enrolled in 201902, and 10 graduated, and 10 did not return in 201903, I would have a 90% retention rate:
(90 Returned Students in 201903) / (110 Enrolled Students in 201902 - 10 Graduated Students in 201902)
= 90 / (110 - 10)
= 90 returned / 100 eligible
Ideally, since not all study periods follow the same naming convention in the larger data set (it varies by institution), the calculation would be based on the 'Study Period Order' as those will be sequential regardless of 'Study Period/RPL' code.
Sample data here: Sample File
Thank you for any guidance you might be able to provide.

It depends on what you add to the visualization. If we add [Study Period Order] to visualization, then your calculation may look like:
calc =
var __StudentsCurrentSeasson =
CALCULATE (
COUNT ( 'YourTable'[Student Code] ),
FILTER (
VALUES ( 'YourTable'[Study Period Order] ),
[Study Period Order] = SELECTEDVALUE ( [Study Period Order] )
)
)
var __StudentsGraduatedCurrentSeasson =
CALCULATE (
COUNT ( 'YourTable'[Student Code] ),
FILTER (
VALUES ( 'YourTable'[Study Period Order], 'YourTable'[Status] ),
[Study Period Order] = SELECTEDVALUE ( [Study Period Order] ) && 'YourTable'[Status] = "Graduated"
)
)
var __StudentsLastSeasson =
CALCULATE (
COUNT ( 'YourTable'[Student Code] ),
FILTER (
VALUES ( 'YourTable'[Study Period Order] ),
[Study Period Order] = SELECTEDVALUE ( [Study Period Order] ) - 1
)
)
return DIVIDE(__StudentsCurrentSeasson, (__StudentsLastSeasson - __StudentsGraduatedCurrentSeasson ) )
If you have multiple StudentCode in one Period then use distinctcount instead of count.

Related

How to stop showing a running total in Power BI

I have a running total in power BI as a line graph.
I want to only show my running total line up to the most recent months data (september 22), in order to get the flat line off the visualisation, so I can forecast the rest of the line.
My cumulative total measure is :
'''Actual Spend YTD =
CALCULATE (
SUM ( CombinedUsageFact[TargetCost] ),
FILTER (
ALLSELECTED ( MonthNumDim[DateMonth]),
ISONORAFTER ( MonthNumDim[DateMonth], MAX ( MonthNumDim[DateMonth] ), DESC )
)
)'''
I need my axis to display up to year end (march 2023) like it does.
What can I do to stop my cumulative line at the most recent data?
Thanks
Would attach images/sample but it says there's a problem with the server!
To get the cumulative calculation to stop on the last period with data, filter the last date by the fact table:
Actual Spend YTD =
VAR maxDate =
CALCULATE ( MAX ( MonthNumDim[DateMonth] ), CombinedUsageFact )
RETURN
CALCULATE (
SUM ( CombinedUsageFact[TargetCost] ),
FILTER (
ALLSELECTED ( MonthNumDim[DateMonth] ),
ISONORAFTER ( MonthNumDim[DateMonth], maxDate, DESC )
)

PowerBI Last N Months Trend Line - Ignore All Date Filters

Have a simple, 2 table model: [Sales]*-[Calendar] with sales that span from 1/1/2021 to yesterday. Day grain. Calendar is a standard date dimension.
Need: I want a line chart that shows the sales trend for each day since the first of the month, 3 months back, to yesterday.
I want this line chart measure to ignore the influence from all Calendar[Date] slicers/filter that happen to be on the report.
I understand I can use the "edit interactions" feature and stop the Date slicers from affecting the trend line - and that works - with this code.
However, I want to learn how to ignore these external filter affects within DAX & not rely on using the edit-interaction feature.
I've gotten this far. This code calcs the measure I want, but a date slicer on the report affects it and I don't want it to. What am I missing?
Total $ Last 3 Months Trend =
VAR __monthsBack = 3
VAR __endDate =
CALCULATE ( MAX ( Sales[SalesDate] ), ALL ( 'Calendar' ) )
VAR __endDateMinus_NMonths =
EDATE ( __endDate, - __monthsBack )
VAR __firstDate =
DATE ( YEAR ( __endDateMinus_NMonths ), MONTH ( __endDateMinus_NMonths ), 01 )
RETURN
IF (
MAX ( 'Calendar'[Date] ) <= __endDate
&& MAX ( 'Calendar'[Date] ) >= __firstDate,
CALCULATE (
[Total $],
FILTER (
'Calendar',
'Calendar'[Date] >= __firstDate
&& 'Calendar'[Date] <= __endDate
),
REMOVEFILTERS ( 'Calendar'[Date] )
),
BLANK ()
)

Power BI: Use unrelated date table to create a measure

Hi I have been struggling with this for a bit now and I hope I didn't miss a previous question. I am trying to get a count of the vehicles we have based on an EOMONTH date. We are buying and selling cars on a regular basis and for reporting we need to know how many we had at the end of each month and the report is a rolling 12 months.
I've tried creating the relationship with the purchasedate of the vehicle to the date of my date table but when I create the measure (Used to calculate the number of vehicles purchased but haven't been sold):
SalesBlank = CALCULATE(
COUNT(Vehicles[MVANumber]),
FILTER(Vehicles, Vehicles[purchasedate] <= RELATED('Date'[EOMONTH]) && ISBLANK(Vehicles[saledate])))
I only get a count of vehicles purchased that month and don't have a sale date - I'm not surprised because my relationship with the date table is the purchase date.
How can I set up a measure to look at the date table and filter the vehicles table with this logic:
purchasedate <= date[EOMONTH] && ISBLANK(salesdate)
Any help would be greatly appreciated!!
Thanks,
Matt
Sample Data and Desired Results
Relationships
If I understand you correctly, you want to get a count of the vehicles on hand at the end of each month. That could be calculated by counting the vehicles with a purchase date less than or equal to the selected end of month and subtracting the count of vehicles with a sale date less than or equal to the selected end of month.
You can create an active relationship between Vehicle[PurchaseDate] and Date[Date]. Then create an inactive relationship based upon Vehicles[SaleDate] and Date[Date].
You could use a measure that is something like this:
Inventory Count =
VAR MaxDate =
MAX ( 'Date'[Date] )
VAR MinDate =
CALCULATE ( MIN ( 'Date'[Date] ), ALL ( 'Date' ) )
VAR Purch =
CALCULATE (
COUNT ( 'Vehicles'[VehicleID] ),
DATESBETWEEN ( 'Date'[Date], MinDate, MaxDate )
)
VAR Sales =
CALCULATE (
COUNT ( 'Vehicles'[VehicleID] ),
USERELATIONSHIP ( 'Date'[Date], Vehicles[Sale Date] ),
DATESBETWEEN ( 'Date'[Date], MinDate, MaxDate )
)
VAR MaxPurDt =
CALCULATE ( MAX ( 'Vehicles'[Purchase Date] ), ALL ( 'Vehicles' ) )
VAR MaxSlDt =
CALCULATE ( MAX ( 'Vehicles'[Sale Date] ), ALL ( 'Vehicles' ) )
RETURN
IF (
MIN ( 'Date'[Date] ) <= MaxPurDt
|| MIN ( 'Date'[Date] ) <= MaxSlDt,
Purch - Sales
)
This measure gets a cumulative count of purchases and a cumulative count of sales and then subtracts them. The IF check is to avoid propagation of cumulative totals beyond the maximum date in the Vehicle table.
I'm not sure how to interpret your showing of just 3 months in the desired results. This will produce the same answers as what you have, but without a filter applied to the table, it starts at 3/31/2016 (the date of the first sale).
Edit: There's probably a more efficient way along the lines you were thinking, but it is escaping me at the moment.

DAX AVERAGEX including a 0 for total average

My table represents users working on a production line. Each row in the table provides the number of units a user produced within a 15 minute window. I am trying to calculate Units/Hour per User (which seems to be working fine), but my overall Average seems to be off.
Table and results of my measure:
Row by row it is what I am looking for. But the total average of 179.67 is wrong. It should be 196. I think for the 11:30 timestamp, Leondro did not have any work, and it is including a 0 for him. I would like to exclude that.
Measure:
UPH =
var unitshour = CALCULATE(SUM(Table1[Units]) / (DISTINCTCOUNT(Table1[DateTime])/4))
var users = AVERAGEX( VALUES(Table1[DateTime]), DISTINCTCOUNT(Table1[Username]))
RETURN
unitshour/ users
I don't think 196 is the number you want if you want to treat each time period equally. I'd suggest this alternative:
UPH =
AVERAGEX (
VALUES ( Table1[DateTime] ),
CALCULATE ( 4 * SUM ( Table1[Units] ) / DISTINCTCOUNT ( Table1[Username] ) )
)
If you want each time period to be weighted by the number of users in that time period, then the 196 it what you want.
UPHUserWeighted =
VAR Summary =
SUMMARIZE (
Table1,
Table1[DateTime],
Table1[Username],
"UPH", 4 * SUM ( Table1[Units] ) / DISTINCTCOUNT ( Table1[Username] )
)
RETURN AVERAGEX ( Summary, [UPH] )

How to work out Attrition for Staff members with a Start & End Date in Power BI?

I have a table with Staff data with columns DateEmployed & TerminationDate.
I would like to work out the number of people that started & left (which I used a count formula) as well as the net growth for all date periods.
The formula would count each DateEmployed as 1 & if an individual does not have a Termination Date then it would not count it.
e.g. 4 people starts in June 2016 & 2 leaves in June 2016 giving me a net value of 2.
The issue arises further as I need a date dimension to view them for each month for each year.
I would like to display all 3 dimensions of data in one graph as well.
The data should read like the bar graphs consolidated:
enter image description here
The best practice in every case is to create a calendar/date table. Fortunately calendar tables are easy to create in Power BI. With an expression you can create it.
In Power BI / Modeling tab / New Table icon, you can create a custom date table using this expression:
DateTable = CALENDAR (DATE(2016,1,1), DATE(2016,12,31))
This expression generates dates from 2016-1-1 to 2016-12-31 so if your data expands to a wider range you have to modify it to suit to your requeriment.
Create these measures:
Starting: Measure counting the people starting in each month or year.
Starting =
CALCULATE (
COUNT ( 'Table'[FullName] ),
FILTER (
ALL ( 'Table' ),
COUNTROWS (
FILTER (
'Table',
NOT ISBLANK ( [DateEmployed] )
&& MONTH ( EARLIER ( 'Table'[DateEmployed] ) ) = MONTH ( MAX ( DateTable[Date] ) )
)
)
)
)
Ending: Measure for counting the people ending in each month or year.
Ending =
CALCULATE (
COUNT ( 'Table'[FullName] ),
FILTER (
ALL ( 'Table' ),
COUNTROWS (
FILTER (
'Table',
NOT ISBLANK ( [TerminationDate] )
&& MONTH ( EARLIER ( 'Table'[TerminationDate] ) )
= MONTH ( MAX ( DateTable[Date] ) )
)
)
)
)
Net: Total for each month:
Total = ABS([Starting] - [Ending])
Now in a bar chart or any visualization set the measures something like this: