Search code examples
powerbipowerpivotdax

DAX measure: project duration (days) from dimension starting & ending date


I have following scenario which has been simplified a little:

Costs fact table:

date, project_key, costs €

Project dimension:

project_key, name, starting date, ending date

Date dimension:

date, years, months, weeks, etc

I would need to create a measure which would tell project duration of days using starting and ending dates from project dimension. The first challenge is that there isn't transactions for all days in the fact table. Project starting date might be 1st of January but first cost transaction is on fact table like 15th on January. So we still need to calculate the days between starting and ending date if on filter context.

So the second challenge is the filter context. User might want to view only February. So it project starting date is 1.6.2016 and ending date is 1.11.2016 and user wants to view only September it should display only 30 days.

The third challenge is to view days for multiple projects. So if user selects only single day it should view count for all of the projects in progress.

I'm thankful for any help which could lead towards the solution. So don't hesitate to ask more details if needed.

edit: Here is a picture to explain this better:

enter image description here

Update 7.2.2017

Still trying to create a single measure for this solution. Measure which user could use with only dates, projects or as it is. Separate calculated column for ongoing project counts per day would be easy solution but it would only filter by date table.

Update 9.2.2017

Thank you all for your efforts. As an end result I'm confident that calculations not based on fact table are quite tricky. For this specific case I ended up doing new table with CROSS JOIN on dates and project ids to fulfill all requirements. One option also was to add starting and ending dates as own lines to fact table with zero costs. The real solution also have more dimensions we need to take into consideration.


Solution

  • To get the expected result you have to create a calculated column and a measure, the calculated column lets count the number of projects in dates where projects were executed and the measure to count the number of days elapsed from [starting_date] and [ending_date] in each project taking in account filters.

    The calculated column have to be created in the dim_date table using this expression:

    Count of Projects =
    SUMX (
        FILTER (
            project_dim,
            [starting_date] <= EARLIER ( date_dim[date] )
                && [ending_date] >= EARLIER ( date_dim[date] )
        ),
        1
    )
    

    The measure should be created in the project_dim table using this expression:

    Duration (Days) =
    DATEDIFF (
        MAX ( MIN ( [starting_date] ), MIN ( date_dim[date] ) ),
        MIN ( MAX ( [ending_date] ), MAX ( date_dim[date] ) ),
        DAY
    )
        + 1
    

    The result you will get is something like this:

    enter image description here

    enter image description here

    And this if you filter the week using an slicer or a filter on dim_date table

    enter image description here

    Update

    Support for SSAS 2014 - DATEDIFF() is available in SSAS 2016.

    First of all, it is important you realize you are measuring two different things but you want only one measure visible to your users. In the first Expected result you want to get the number of projects running in each date while in the Expected results 2 and 3 (in the OP) you want the days elapsed in each project taking in account filters on date_dim.

    You can create a measure to wrap both measures in one and use HASONEFILTER to determine the context where each measure should run. Before continue with the wrapping measure check the below measure that replaces the measure posted above using DATEDIFF function which doesn't work in your environment.

    After creating the previous calculated column that is required to determine the number of projects in each date, create a measure called Duration Measure, this measure won't be used by your users but lets us calculate the final measure.

    Duration Measure = SUMX(FILTER (
            date_dim,
            date_dim[date] >= MIN ( project_dim[starting_date] )
                && date_dim[date] <= MAX ( project_dim[ending_date] )
        ),1
    )
    

    Now the final measure which your users should interact can be written like this:

    Duration (Days) =
    IF (
        HASONEFILTER ( date_dim[date] ),
        SUM ( date_dim[Count of Projects] ),
        [Duration Measure]
    )
    

    This measure will determine the context and will return the right measure for the given context. So you can add the same measure for both tables and it will return the desired result.

    enter image description here

    Despite this solution is demonstrated in Power BI it works in Power Pivot too.