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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
dariaglb
Frequent Visitor

DAX mesure depens on calendar and dates

Hi, 
I have two tables and Calendar_table

Table 1

ProjectSiteStart dateEnd dateDuration
11221201.05.202310.07.202310
11221211.07.202302.09.20238
11221203.09.202313.12.202314
11331302.02.202302.06.202317
11331303.06.202312.09.202314
11331313.09.202315.11.20239
11331401.04.202325.06.202312
11331426.06.202305.10.202314
11331406.10.202313.12.202310

 

Table 2

 

PersonProjectSiteStart date appointment siteEnd dateappointment site
Name11221201.05.202310.09.2023
Name11331302.02.202313.09.2023
Name11331401.04.202326.06.2023
Name211221211.09.2023 
Name211331314.09.2023 
Name211331426.06.2023 


Table 2 has many-to-many connection with Table 1, where Table 2 ---> Table 1 (not both) by Site

I need mesure depens on Calendar_table - which returns actual Duration (period Start Date-End Date) and actual appointment Site for Person. 

For example, 

Choose in Calendar_table - March 2023
Name - Site 13 - returns 17
Name2 - Site 13 - returns null

Calendar_table - November 2023
Name - Site 13 - returns null
Name2 - Site 13 - returns 9

1 REPLY 1
DataNinja777
Super User
Super User

Hi @dariaglb ,

 

To calculate the actual Duration for each Person and Site based on the Calendar_table date selection, we can use DAX to:

  1. Filter the data for the selected date range.
  2. Intersect the appointment period with the project duration period.
  3. Return the overlapping duration for each person and site.

Steps to Create the Measure

Here’s a DAX measure to compute the Actual Duration:

Actual Duration = 
VAR SelectedDate = MAX(Calendar_table[Date]) -- The selected date from the Calendar table
VAR AppointmentPeriod = 
    FILTER(
        Table2,
        Table2[Start date appointment site] <= SelectedDate &&
        (ISBLANK(Table2[End dateappointment site]) || Table2[End dateappointment site] >= SelectedDate)
    )
VAR ProjectPeriod = 
    FILTER(
        Table1,
        Table1[Start date] <= SelectedDate &&
        Table1[End date] >= SelectedDate
    )
VAR MatchingPeriods =
    FILTER(
        GENERATE(AppointmentPeriod, ProjectPeriod),
        Table2[Project] = Table1[Project] &&
        Table2[Site] = Table1[Site]
    )
RETURN
    SUMX(MatchingPeriods, Table1[Duration])

Expected Output

  • March 2023
    • Name → Site 13 → Returns 17 (Project active for Name, duration overlaps with Calendar_table).
    • Name2 → Site 13 → Returns null (No overlapping duration for the date).
  • November 2023
    • Name → Site 13 → Returns null (Appointment ended for the date).
    • Name2 → Site 13 → Returns 9 (Project active for Name2 in November 2023).

Best regards,

 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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

October NL Carousel

Fabric Community Update - October 2024

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

Top Solution Authors