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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.