Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
maddogp
Frequent Visitor

Efficient Code

Hi folks,

I'm working in SQL Analysis Services, Live Connection.

My goal is to calculate the enrolment in a course, at various times through the semester.  For example, at 1wk after start of course, midpoint of the course, and end of the course.  However, the start and end dates change each year.

 

The data is organized as follows:

'StudentTimeTable'   
[StudentID][SchoolYear][StartDate][EndDate]
    

 

As well, in a separate table in the model (but not incorporated in my measure below):

'SchoolSchedule'   
[SchoolYear][Semester][SemesterStartDate][SemesterEndDate]
    

 

I have created this measure for 1wk after the start of the course which works (for past three years and involves manually coding the 1wk date), but I'm sure there is a better way to do this using variables and incorporating the SchoolSchedule table.

 

Enrolment@1wk = calculate(
COUNTA(StudentTimeTable[StudentID]),
FILTER(StudentTimeTable,StudentTimeTable[SchoolYear] = "20182019" &&
DATEVALUE(StudentTimeTable[StartDate]) <= date(2019, 2, 11) &&
DATEVALUE(StudentTimeTable[EndDate]) > date(2019,2,11) ||
StudentTimeTable[SchoolYear] = "20192020" &&
DATEVALUE(StudentTimeTable[StartDate]) <= date(2020, 2, 10) &&
DATEVALUE(StudentTimeTable[EndDate]) > date(2020,2,10) ||
StudentTimeTable[SchoolYear] = "20202021" &&
DATEVALUE(StudentTimeTable[StartDate]) <= date(2021, 2, 9) &&
DATEVALUE(StudentTimeTable[EndDate]) > date(2021,2,9))
)
 
I've also create a measure to determine the date 1 wk after SemesterStartDate:
7daysPostStart = CALCULATE(
DATEVALUE(
SELECTEDVALUE(SchoolSchedule[SemesterStartDate]) + 7))
 
 Here's the resulting table (not all courses run each year)
CourseYearSemester Start DateEnrolment@1wk
ABC201820192/4/201922
ABC201920202/3/202027
DEF201820192/4/201924
DEF202020212/2/202118
XYZ201820192/4/201912
XYZ201920202/3/202025
XYZ202020212/2/20214

 

I'm still learning DAX and was hoping someone with more experience could show a better way to write the measure that makes use of the SchoolSchedule table, and allows for flexibility to work with all years and all SemesterStartDate and SemesterEndDates.

 

Thanks 

1 ACCEPTED SOLUTION
daxer-almighty
Solution Sage
Solution Sage

@maddogp 

 

Please supply good examples of your live data. The two first 'tables' are not very informative. Also, please show us the model. DAX can't be written in a vacuum.

View solution in original post

1 REPLY 1
daxer-almighty
Solution Sage
Solution Sage

@maddogp 

 

Please supply good examples of your live data. The two first 'tables' are not very informative. Also, please show us the model. DAX can't be written in a vacuum.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.