2
votes

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.

3
Do you need to calculate the duration in days between start and end dates or you need to calculate the costs between them? Could you edit your question to include basic sample data and expected result.alejandro zuleta
Duration of days between start and end date (if context is not filtered with other dates). I will add the data.Henri

3 Answers

3
votes

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 demostrated in Power BI it works in Power Pivot too.

Let me know if this helps.

1
votes

First I would create 2 relationships:

  1. project_dim[project_key] => costs_fact[project_key]
  2. date_dim[date] => costs_fact[date]

The Costs measure would be just: SUM ( costs_fact[costs] )

The Duration (days) measure needs a CALCULATE to change the filter context on the Date dimension. This is effectively calculating a relationship between project_dim and date_dim on the fly, based on the selected rows from both tables.

Duration (days) =
CALCULATE (
    COUNTROWS ( date_dim ),
    FILTER (
        date_dim,
        date_dim[date] >= MIN ( project_dim[starting_date] )
            && date_dim[date] <= MAX ( project_dim[ending_date] )
    )
)
1
votes

I suggest you to separate the measure Duration (days) into different calculated column/measure as they don't actually have the same meaning under different contexts.

First of all, create a one-to-many relationship between dates/costs and projects/costs. (Note the single cross filter direction or the filter context will be wrongly applied during calculation)

relationship

For the Expected result 1, I've created a calculated column in the date dimension called Project (days). It counts how many projects are in progress for a given day.

Project (days) = 
COUNTROWS(
    FILTER(
        projects,
        dates[date] >= projects[starting_date] &&
        dates[date] <= projects[ending_date]
    )
)

Project (days)

P.S. If you want to have aggregated results on weekly/monthly basis, you can further create a measure and aggregate Project (days).

For Expected result 2 and 3, the measure Duration (days) is as follows:

Duration (days) = 
COUNTROWS(
    FILTER(
        dates,
        dates[date] >= FIRSTDATE(projects[starting_date]) &&
        dates[date] <= FIRSTDATE(projects[ending_date])
    )
)

The result will be as expected: result 1 result 2