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
Anonymous
Not applicable

Duration with multiple applied filters for transactional data

Hello all,

 

I am working on a data model with the following columns ContractID, Stage, Start Date, End Date. ContractID can have multiple Stages and a stage can have multiple rows with Start Date and End Date (transactional data). What I'm looking to calculate is a column "Stage Duration (days)" that determines for a ContractID and specific Stage the total duration given by Min of Start Date and Max of End Date. Here is an Excel example of what the desired outcome should look like:

 

ContractIDStageStart DateEnd DateStage Duration (days)
1A7/10/20207/11/202010
1A7/11/20207/13/202010
1A7/13/20207/20/202010
1B7/20/20207/24/202016
1B7/24/20208/5/202016
1C8/5/20208/6/202011
1C8/6/20208/8/202011
1C8/8/20208/12/202011
1C8/12/20208/16/202011
2A6/25/20206/28/20203
2B6/28/20206/30/20204
2B6/30/20207/2/20204
2C7/2/20207/13/202014
2C7/15/20207/15/202014
2C7/15/20207/16/202014

 

For example,  for ContractID=1 in Stage=A, Min of Start Date for all rows is 7/10/2020 and Max of End Date for all rows is 7/20/2020 so the difference would 10 (days).

 

I tried a few formulas but I couldn't find a good solution. Any advice would be helpful.

 

Thank you!

2 ACCEPTED SOLUTIONS
DataZoe
Employee
Employee

Hi @Anonymous ,

 

You could try this measure:

Duration Measure =
VAR _start =
    CALCULATE (
        MIN ( 'Table'[Start Date] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Stage] = SELECTEDVALUE ( 'Table'[Stage] )
                && 'Table'[ContractID] = SELECTEDVALUE ( 'Table'[ContractID] )
        )
    )
VAR _end =
    CALCULATE (
        MAX ( 'Table'[end Date] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Stage] = SELECTEDVALUE ( 'Table'[Stage] )
                && 'Table'[ContractID] = SELECTEDVALUE ( 'Table'[ContractID] )
        )
    )
RETURN
    DATEDIFF ( _start, _end, DAY )

 

 You could also do this version to see a total of number of days taken too:

Duration Measure =
VAR _start =
    CALCULATE (
        MIN ( 'Table'[Start Date] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Stage] = SELECTEDVALUE ( 'Table'[Stage] )
                && 'Table'[ContractID] = SELECTEDVALUE ( 'Table'[ContractID] )
        )
    )
VAR _end =
    CALCULATE (
        MAX ( 'Table'[end Date] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Stage] = SELECTEDVALUE ( 'Table'[Stage] )
                && 'Table'[ContractID] = SELECTEDVALUE ( 'Table'[ContractID] )
        )
    )
RETURN
    COALESCE (
        DATEDIFF ( _start, _end, DAY ),
        DATEDIFF ( MIN ( 'Table'[Start Date] ), MAX ( 'Table'[End Date] ), DAY )
    )

StageDur.JPG

 

Respectfully,
Zoe Douglas (DataZoe)



Follow me on LinkedIn at https://www.linkedin.com/in/zoedouglas-data
See my reports and blog at https://www.datazoepowerbi.com/

View solution in original post

v-zhenbw-msft
Community Support
Community Support

Hi @Anonymous ,

 

If you need a measure, the @DataZoe ’s solution is perfect.

If you want to create a column, you can refer this formula.

 

Stage Duration (days) = 
var _min_date = CALCULATE(MIN('Table'[StartDate]),FILTER(ALL('Table'),EARLIER('Table'[Stage])='Table'[Stage] && EARLIER('Table'[ContractID])='Table'[ContractID]))
var _max_date = CALCULATE(MAX('Table'[EndDate]),FILTER(ALL('Table'),EARLIER('Table'[Stage])='Table'[Stage] && EARLIER('Table'[ContractID])='Table'[ContractID]))
return
DATEDIFF(_min_date,_max_date,DAY)

 

d1.jpg

 

If you have any question, please kindly ask here and we will try to resolve it.

BTW, pbix as attached.

 

Best regards,

 

Community Support Team _ zhenbw

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
v-zhenbw-msft
Community Support
Community Support

Hi @Anonymous ,

 

If you need a measure, the @DataZoe ’s solution is perfect.

If you want to create a column, you can refer this formula.

 

Stage Duration (days) = 
var _min_date = CALCULATE(MIN('Table'[StartDate]),FILTER(ALL('Table'),EARLIER('Table'[Stage])='Table'[Stage] && EARLIER('Table'[ContractID])='Table'[ContractID]))
var _max_date = CALCULATE(MAX('Table'[EndDate]),FILTER(ALL('Table'),EARLIER('Table'[Stage])='Table'[Stage] && EARLIER('Table'[ContractID])='Table'[ContractID]))
return
DATEDIFF(_min_date,_max_date,DAY)

 

d1.jpg

 

If you have any question, please kindly ask here and we will try to resolve it.

BTW, pbix as attached.

 

Best regards,

 

Community Support Team _ zhenbw

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Ashish_Mathur
Super User
Super User

Hi,

To your matrix visual, drag ContractID and Stage to the row section.  Write these measures

Start = min(data[start date])

End = max(data[end date])

Duration = [end]-[start]

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
DataZoe
Employee
Employee

Hi @Anonymous ,

 

You could try this measure:

Duration Measure =
VAR _start =
    CALCULATE (
        MIN ( 'Table'[Start Date] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Stage] = SELECTEDVALUE ( 'Table'[Stage] )
                && 'Table'[ContractID] = SELECTEDVALUE ( 'Table'[ContractID] )
        )
    )
VAR _end =
    CALCULATE (
        MAX ( 'Table'[end Date] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Stage] = SELECTEDVALUE ( 'Table'[Stage] )
                && 'Table'[ContractID] = SELECTEDVALUE ( 'Table'[ContractID] )
        )
    )
RETURN
    DATEDIFF ( _start, _end, DAY )

 

 You could also do this version to see a total of number of days taken too:

Duration Measure =
VAR _start =
    CALCULATE (
        MIN ( 'Table'[Start Date] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Stage] = SELECTEDVALUE ( 'Table'[Stage] )
                && 'Table'[ContractID] = SELECTEDVALUE ( 'Table'[ContractID] )
        )
    )
VAR _end =
    CALCULATE (
        MAX ( 'Table'[end Date] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Stage] = SELECTEDVALUE ( 'Table'[Stage] )
                && 'Table'[ContractID] = SELECTEDVALUE ( 'Table'[ContractID] )
        )
    )
RETURN
    COALESCE (
        DATEDIFF ( _start, _end, DAY ),
        DATEDIFF ( MIN ( 'Table'[Start Date] ), MAX ( 'Table'[End Date] ), DAY )
    )

StageDur.JPG

 

Respectfully,
Zoe Douglas (DataZoe)



Follow me on LinkedIn at https://www.linkedin.com/in/zoedouglas-data
See my reports and blog at https://www.datazoepowerbi.com/

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.