Exclude Certain Rows from DATEDIFF measure

Today I’m feeling a little overwhelmed by something simple I’m trying to do with a DATEDIFF measure!

Process Time = 
DATEDIFF(MIN('StatusHistory'[DateCreated] ), MAX('StatusHistory'[DateCreated]), DAY )

The measure works fine, it returns the total number of days for the required context.

The issue is I need to exclude rows from the table StatusHistory where column Action is equal to “Completed and closed”, below image shows a table where I have filtered on column RequestNumber, where we have a “Completed and closed” in the Action column, I have intentionally only showed one group of records RequestNumber 00488, i have many other instances of like groups in the table.

So for RequestNumber 00488, I need to exclude from the measure.

Image below shows the current table with the measure Process Time.

CAR-5

How best to go about getting the results i need?

Regards
J

@jprlimey ,

It’s always a bit of a crapshoot providing DAX solutions without seeing the underlying data model or having a PBIX file to test on, but here goes nothing. See if this works for you:

If not, please provide a PBIX file and I’d be glad to work through the specific solution with you.

  • Brian
1 Like

@BrianJ

Brian, thank you for your response, and as you stated somewhat of a crap-shoot to get a solution without an actual pbix file. I have attached a basic file that covers the scope of what I need to achieve.

The image below shows the result of changing the DAX measure to include your suggestion, as you can see it is returning the total for each instance of the request number.

CAR-6

Hopefully having a pbix file will make the potential solution much easier.

Regards
J

CAR eDNA Rev-1.pbix (112.7 KB)

@jprlimey ,

Indeed it does. If we add in a variable that tracks the current request number and then uses that as an additional filter condition, I think it now runs like a champ:

Hope this get you what you need.

– Brian

@BrianJ

Brian, thank you for the response. I added your revised DAX with the VAR for the current request number. I really don’t think it worked the way we planned. For example I can still see (in the visual) request number where we have a corresponding “Completed and closed” for StatusHistory [Action]. So we have not filtered out those records. The code is below

Process Time eDNA BJ = 
VAR _CurrentReq = SELECTEDVALUE( 'StatusHistory'[RequestNumber] )
VAR _Result =
CALCULATE(
    DATEDIFF(
        MIN('StatusHistory'[DateCreated] ), 
        MAX('StatusHistory'[DateCreated]), 
        DAY
    ),
    FILTER(
        ALLSELECTED( 'StatusHistory' ),
        'StatusHistory'[Action] <> "Completed and closed" &&
        'StatusHistory'[RequestNumber] = _CurrentReq
    )
)
RETURN
_Result

The image below shows the request number 00488, which has a matching “Completed and closed”
CAR-7

The image below shows that request number 00488 is still part of the visual.

CAR-8

One thing I did notice is that the RequestNumber in the visual is actually from the CorrectiveAction table, shown in the image below.

CAR-9

When I did change to the ‘StatusHistory’ [RequestNumber] it still didn’t work, and it also didn’t calculate the measure [Pareto Chart] correctly. Which then prompted me to update where needed all the measures that referenced the ‘CorrectiveAction’ [RequestNumber] to ‘StatusHistory’ [RequestNumber] and change the visual to ‘StatusHistory’ [RequestNumber] , and things went from bad to worse.

Not sure where to go from here. Revised pbix file is attached.

Regards
J

CAR eDNA Rev-2.pbix (112.5 KB)

@jprlimey ,

No, the DAX I wrote works perfectly – the problem is it answers completely the wrong question. Sorry – my bad on that one. I totally misinterpreted your requirement. I thought you just wanted to eliminate the row that was closed, and recalculate the DATEDIFF for that request number minus the observations that were closed.

Now I understand that if ANY of the rows are closed for that request number, then you want that request number eliminated entirely from the calculation and the visuals.

Let me rework this – this solution now lends itself much better to a Power Query-based solution than a DAX-based approach.

Be back shortly with a proposed solution. Sorry for the confusion.

– Brian

@BrianJ ,

Yes, you got it, i need to exclude from the calculation any rows with “Completed and closed” under the Action column for that request number.

Regards
J

@jprlimey ,

Definitely going to want to do this in Power Query. Can you please post the data file?

One other question. From a data modeling standpoint, why is Corrective Action set up as a dimension table? It seems to me that should just be a separate field in the fact table.

Thanks.

– Brian

@BrianJ ,

Good observation… The Corrective Action table is very much minimized, too much sensitive information in the original file. In my real data the Corrective Action table is used as a lookup table.

data.xlsx is the StatusHistory table
data2.xlsx is the CorrectiveAction table

Do you need anything else?

Regards
J
data.xlsx (56.9 KB)
data2.xlsx (11.8 KB)

@jprlimey ,

Okay, let’s see if I got it this time. 100%-DAX free solution.

image

Here are the two major steps:

Group on request number, and while we’re doing that, calculate the min and max dates for each request number

Then, with some custom M determine whether any record within the nested tables grouped by request number contain the phrase “Completed and closed” in the Action field

Then filtered all records that got a one, and finally subtracted the min date from the max date for the remaining records.

I hope this is helpful. Full solution file attached below.

@BrianJ,

I worked through your solution today in my real database, and everything looks great. Thank you so much for the assistance.

Regards
J

@jprlimey ,

Great - glad to hear that’s working well for you. Sorry for the early detour… :smiley:

  • Brian