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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
Anonymous
Not applicable

HELP! Difference between dates in the same column and conditions

I need urgent help please, i need to calculate the difference between dates in the same column depending on the status one sample data:

ID FechaStatus
101/01/2020Activo
201/01/2020Vacante
103/01/2020Activo
203/01/2020Activo
104/03/2020Vacante
204/03/2020Activo
105/03/2020Vacante
205/03/2020Vacante
104/04/2020Activo
204/04/2020Activo
120/05/2020Activo
220/05/2020Vacante
108/06/2020Activo
208/06/2020Activo

 

What i need to do is calculate the difference between the last date that status reads "Vacante" and the first next one that reads "Activo" for example in ID = 1 it would be the difference 05 march and 04 april, the other issue that i have is that this combination can happen multiple times over the year for the same ID, ID=2 is an example and for every time that this status shift occurs i need to calculate the difference please im in need of urgent help

1 ACCEPTED SOLUTION

It depends on how you define average. You can use this:

Total Average = AVERAGEX('Sample Data',[Date Differences])

But that average will include the 0's for the active codes.

This will return the average for just Vacante status.

Total Average =
AVERAGEX(
    FILTER(
        'Sample Data',
        'Sample Data'[Status] = "Vacante"
    ),
    [Date Differences]
)

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

6 REPLIES 6
edhans
Super User
Super User

See if this works for you:

Date Differences = 
VAR CurrentDate = 
    MAX( 'Sample Data'[Fecha] )
VAR CurrentID =
    MAX( 'Sample Data'[ID ] )
VAR CurrentStatus =
    MAX( 'Sample Data'[Status] )
VAR Result =
    IF(
        CurrentStatus = "Vacante",
        DATEDIFF(
            CurrentDate,
            MINX(
                FILTER(
                    ALL( 'Sample Data' ),
                    'Sample Data'[ID ] = CurrentID
                        && 'Sample Data'[Status] = "Activo"
                        && 'Sample Data'[Fecha] >= CurrentDate
                ),
                'Sample Data'[Fecha]
            ),
            DAY
        ),
        0
    )
RETURN
    Result

 

2020-06-08 13_09_23-Untitled - Power BI Desktop.png



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
Anonymous
Not applicable

It Works! but now i need to get the average of those numbers is there a way to obtain it ?

It depends on how you define average. You can use this:

Total Average = AVERAGEX('Sample Data',[Date Differences])

But that average will include the 0's for the active codes.

This will return the average for just Vacante status.

Total Average =
AVERAGEX(
    FILTER(
        'Sample Data',
        'Sample Data'[Status] = "Vacante"
    ),
    [Date Differences]
)

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
Anonymous
Not applicable

thank you i´ll try this, is there a way to calculate the difference in a calculated column?

Possibly. I try not to use calculated columns, and you didn't specify that in the original request. Since the original answer works, can you mark that as the solution? If you want to redo this entire model as calculated columns you should repost as a new thread with those requirements.

 

In general, try to avoid calculated columns. There are times to use them, but it is rare. Getting data out of the source system, creating columns in Power Query, or DAX Measures are usually preferred to calculated columns. See these references:
Calculated Columns vs Measures in DAX
Calculated Columns and Measures in DAX
Storage differences between calculated columns and calculated tables



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
Anonymous
Not applicable

Do i try it as a measure or a calculated column?

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.