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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

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
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.