Forum Discussion

Jbrunson09's avatar
Jbrunson09
Frequent Visitor
4 years ago
Solved

Value spread across dates and time based on calendar assignment table

Hello,

 

Pre warning is that I'm newer to DAX so my terminology might not be correct. But any help on solving my issue would be greatly appreicated as I couldnt find anything in previous posts.

 

The Goal:

To be able to spread cost values across dates per activity based on

  1. start date / finish date
  2. start time / finish time
  3. calendar assignment (Some may be 7 day workweek and some may be 6 day, 5 day, etc.)

In the end it should look something like this. notice how there is no values on the days that are non-working days for each activity which is defined by the calendar reltionship and its working / non working days

 

The Problem:

for the sample file I'm providing all activities work an 8 hour day (8am-12pm 1pm-5pm). the trick is that some activities start at 8am and others start at another time in the work day. So i have a cost per hour calculation but I need to be able to say theres only 5 hours in day 1 and 8 hours in days 2-4 and 3 hours on day 5 and then be able to multiply that by cost per hour to find out what each days total value is.

 

Here is a simple version in excel explaining some of the logic

 

 

I feel like this is a countrows type of problem or sumx where I need to count rows from the date table that

date>= Start && date<= Finish

&& [activities]clndr_id = [CALENDAR]clndr_id

&& [CALENDAR]day_count = 1 (ive added in a couln that gives 1 if its a working day and 0 if its a non working day)

 

But i dont know how to factor time into this. (same as day_count where 1 = working time and 0 = non working time) so you would need a 1 for day_count and a 1 for time_count to consider it a working day and time

 

 

Here is what I have been able to create but as you can see it is not calculating correctly

 

Here is a view of the model

 

 

Im hoping I have explained my problem well enough and that someone much smarter than I can help solve my issue. 

 

Any help is appreicated!! Thank you!

 

See attached my sample data file

PBIX Testing Data File 

  • Kudos for taking on such a rather complex topic.  You're on the right track but you may want to clean up a bit. Too many dangly bits, too many extra visuals etc.

     

    All you need to start is the Activities table and the (disconnected) Dates table.  Then you can process each interval the way you did it - Beginning day, intermediate days, ending day.  There are a lot of caveats here that we are going to ignore  (for example work starting on a weekend day or starting on a workday at 12pm, beginning and ending on the same day, exact weekend days etc)

    Hour_Count_PD = 
    switch(TRUE(),
    -- beginning and ending on the same day
    max(ACTIVITIES[Start_date_key])=max(ACTIVITIES[Finish_date_key]),DATEDIFF(min(ACTIVITIES[Start_time_key]),min(ACTIVITIES[Finish_time_key]),HOUR)-if(min(ACTIVITIES[Start_time_key])<time(12,0,0),1,0),
    -- starting day hours
    SELECTEDVALUE(DATES[Date])=max(ACTIVITIES[Start_date_key]),DATEDIFF(min(ACTIVITIES[Start_time_key]),TIME(17,0,0),HOUR)-if(min(ACTIVITIES[Start_time_key])<time(12,0,0),1,0),
    -- ending day hours
    SELECTEDVALUE(DATES[Date])=max(ACTIVITIES[Finish_date_key]),DATEDIFF(TIME(8,0,0),min(ACTIVITIES[Finish_time_key]),HOUR)-if(min(ACTIVITIES[Finish_time_key])>time(12,0,0),1,0),
    -- in between days
    SELECTEDVALUE(DATES[Date])>max(ACTIVITIES[Start_date_key]) && SELECTEDVALUE(DATES[Date])<max(ACTIVITIES[Finish_date_key]) && SELECTEDVALUE(DATES[Day of Week]) in {1,2,3,4,5},8)

    see attached

7 Replies

  • Kudos for taking on such a rather complex topic.  You're on the right track but you may want to clean up a bit. Too many dangly bits, too many extra visuals etc.

     

    All you need to start is the Activities table and the (disconnected) Dates table.  Then you can process each interval the way you did it - Beginning day, intermediate days, ending day.  There are a lot of caveats here that we are going to ignore  (for example work starting on a weekend day or starting on a workday at 12pm, beginning and ending on the same day, exact weekend days etc)

    Hour_Count_PD = 
    switch(TRUE(),
    -- beginning and ending on the same day
    max(ACTIVITIES[Start_date_key])=max(ACTIVITIES[Finish_date_key]),DATEDIFF(min(ACTIVITIES[Start_time_key]),min(ACTIVITIES[Finish_time_key]),HOUR)-if(min(ACTIVITIES[Start_time_key])<time(12,0,0),1,0),
    -- starting day hours
    SELECTEDVALUE(DATES[Date])=max(ACTIVITIES[Start_date_key]),DATEDIFF(min(ACTIVITIES[Start_time_key]),TIME(17,0,0),HOUR)-if(min(ACTIVITIES[Start_time_key])<time(12,0,0),1,0),
    -- ending day hours
    SELECTEDVALUE(DATES[Date])=max(ACTIVITIES[Finish_date_key]),DATEDIFF(TIME(8,0,0),min(ACTIVITIES[Finish_time_key]),HOUR)-if(min(ACTIVITIES[Finish_time_key])>time(12,0,0),1,0),
    -- in between days
    SELECTEDVALUE(DATES[Date])>max(ACTIVITIES[Start_date_key]) && SELECTEDVALUE(DATES[Date])<max(ACTIVITIES[Finish_date_key]) && SELECTEDVALUE(DATES[Day of Week]) in {1,2,3,4,5},8)

    see attached

    • Jbrunson09's avatar
      Jbrunson09
      Frequent Visitor

      This was a big help! I went down a rabbit hole of SUMX which worked but wasnt very efficient and would lock up my larger dataset.

       

      One thing I'm still struggling with is making this a calculated column. When I bring it to the column it doesnt add up correctly. it should match the 'remain_drtn_hr_cnt' column.

      see here:

      though it works find in a mtrix view seen here:

      what am I doing wrong? I feel like its something simple but Im missing it.

       

      P.S. Ive reworked your formula to take into account calendar type (7 day, 6, 5 etc. and I have added in an exceptions column to the 'DATES' table as I need it to skip holidays as determined by the calendar.

       

      Many thanks for the help so far!

      Link to Workbook

      • lbendlin's avatar
        lbendlin
        Icon for Super User rankSuper User

        Sure, you could make that a calculated column, but that would mean you need to use a CROSSJOIN and would potentially use up a lot of storage.  Are you sure you want to go that route?

  • Jbrunson09's avatar
    Jbrunson09
    Frequent Visitor

    Is anyone able to help me determine the best approach to solve this challenge / optimize what I currently have. When I load my smaller files, it takes awhile to load the data because I have used a calculated column. When I load my larger files it takes 1+ hour. What am I doing wrong and how can I optimize this?

     

    The challenge is I need to keep it flexible to which days and hours to work based on what comes from the calendar table and which days to skip based on the exceptions table. Ive used multiple layers of sumx but I feel like thats a part of the issue in conjunction with the use of the complex calculated column (is there a way to keep this all in measure form?

     

    Data Model:

     

    Final result I'm looking for:

     

    Any help is appreciated!!

     

    Link to test file