cancel
Showing results for
Did you mean:

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Frequent Visitor

## Calculate last day with condition

Dear all,

I have a data set where:
Items are audited, reporting 0 or 1 as a result.

But not all items are audited every day, the frequency is random. So some days, where the item was not audited, the result is reported as NA (not available)

See an example below for 3 items since March 7th until March 19th:

 Audit date 07-03-2017 08-03-2017 09-03-2017 10-03-2017 11-03-2017 12-03-2017 13-03-2017 14-03-2017 15-03-2017 16-03-2017 17-03-2017 18-03-2017 19-03-2017 Item A NA NA 0 0 1 1 NA 1 0 NA 0 NA NA Item B 0 0 1 1 NA 1 NA 0 NA 0 0 0 0 Item C 1 1 1 0 1 1 1 NA 1 0 NA 0 0

What I'm trying to create are 5 DAX formulas:

 DAX FORMULAS Last audit date Previous audit to last audit date last audited "0" date last audited "1" date Consequtive days audited "0" since last audited "0" date Item A 17-03-2017 15-03-2017 17-03-2017 14-03-2017 1 Item B 19-03-2017 18-03-2017 19-03-2017 12-03-2017 4 Item C 19-03-2017 18-03-2017 19-03-2017 15-03-2017 2

Where,
1. Last audit date = It is the last date different to "NA"

2. Previous audit to last audit date = It is the penultimate date different to "NA"

3. Last audited "0" date = It is the last date equal to "0"

4. Last audited "1" date = It is the last date equal to "1"

5. Consequtive days audited "0" since last audited "0" date = How many consequtive days were "0" since the last day equal to "0"

Anyone could help me?

regards

1 ACCEPTED SOLUTION
Microsoft Employee

@M4R10

In this scenario, you apply the filter to limit the table context into "Answer = 0", the measure will not return result when the current context is "Answer =1". You may apply IF condition to combine the "last audit 0" and "last audit 1" into one measure.

To calculate the consecutive appearance, please refer to my expression in following thread:

http://community.powerbi.com/t5/Desktop/Count-last-actual-consecutive-sorted-rows/m-p/149683

Regards,

3 REPLIES 3
Super User

First of all, I would unpivot your date columns to end up with something like:

Item,Audit date,Value

Item A,07-03-2017,NA

Item A,08-03-2017,NA

Item A,09-03-2017,0

...

At that point, most of your calculations should be relatively trivial along the lines of wrapping a MAX function for Audit date in a CALCULATE with an appropriate FILTER clause.

Become an expert!: Enterprise DNA
External Tools: MSHGQM
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...
Frequent Visitor

thanks @Greg_Deckler

Actually, my tables are normalized, I have two tables:

- VISITS to register visit dates

- AUDIT to register the answers of each visit

According to your suggestion, I created 3 new measures:

LAST AUDIT DATE = CALCULATE(MAX('TBL_VISITS'[AUDIT DATES]);

LAST 0 AUDIT = CALCULATE(MAX('TBL_VISITS'[AUDIT DATES]);

LAST 1 AUDIT = CALCULATE(MAX('TBL_VISITS'[AUDIT DATES]);

However I still have an issue, the results of "audit 0" and "audit 1" are reported ONLY when it was 0 or 1 respectively:

In the following sample, the column "Last 0 Audit" should report 13-12-2016 for the 2nd and 3rd row (03-01-2017 and 16-02-2017):

Any idea?

thank you!!!

Microsoft Employee

@M4R10

In this scenario, you apply the filter to limit the table context into "Answer = 0", the measure will not return result when the current context is "Answer =1". You may apply IF condition to combine the "last audit 0" and "last audit 1" into one measure.

To calculate the consecutive appearance, please refer to my expression in following thread:

http://community.powerbi.com/t5/Desktop/Count-last-actual-consecutive-sorted-rows/m-p/149683

Regards,

Announcements

#### Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.