cancel
Showing results 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.

Frequent Visitor

## DAX formula to turn, in each line, the anterior date for the same ID&amp;Service.

Hello Everyone!

Can you help me with a formula that i will try to explain:

I have a single table in PBI wich each row is an ID (sow&cycle) and a n.º of service(1, 2, 3...) and date (mating date). One ID (sow&cycle) can have or not more than 1 service. One service (p.e. 1) can end with a repetition (so it gets after another row with the service 2...) or can end in a farrowing ( There is not more services after with this ID (sow&cycle)).  There are more results of the data mating but its not necessary for the question.

I need to calculare de diference between (in days):

service 1 and 2 if i have a repetition (not a farrowing) in service 1
service 2 and 3 if i have a repetition (not a farrowing) in service 2

service 3 and 4 if i have a repetition (not a farrowing) in service 3

So it would be easier for me if i have the dates of the next service in the same row (if that row is a repetition) and make the diference in days after.

I tried a lot of formulas, the one that is more likely and have better results is:

Column

Repeat Date 1try = IF(
PCH[Mating Result]="Repetition" && PCH[Service]=1,
CALCULATE
([Dia 2] , ALLEXCEPT(PCH, PCH[Sow&Cycle] )),

IF(
PCH[Mating Result]="Repetition" && PCH[Service]=2,
CALCULATE
([Dia 3] , ALLEXCEPT(PCH, PCH[Sow&Cycle] )),

IF(
PCH[Mating Result]="Repetition" && PCH[Service]=3,
CALCULATE
([Dia 4] , ALLEXCEPT(PCH, PCH[Sow&Cycle] ))

)))

Whrere the measures "dia 2", "dia 3" and "dia 4" are:

Measures:

Dia 1 = MIN(PCH[MatingDate])

Dia 2 =
CALCULATE(
MINX(
TOPN(2, 'PCH', PCH[MatingDate], DESC),
PCH[MatingDate]), ALLEXCEPT(PCH, PCH[Sow&Cycle])
)

Dia 3 = VAR MinValue = [Dia 2]
RETURN
CALCULATE(
MIN(PCH[MatingDate]),
FILTER(ALLSELECTED(PCH), PCH[MatingDate] > MinValue),
ALLEXCEPT(PCH, PCH[Sow&Cycle])
)

Dia 4 = VAR MinValue = [Dia 3]
RETURN
CALCULATE(
MIN(PCH[MatingDate]),
FILTER(ALLSELECTED(PCH), PCH[MatingDate] > MinValue),
ALLEXCEPT(PCH, PCH[Sow&Cycle])
)

But i can only get good values until day 3, after that it appear or in blank or the first date (Service=1)
I can't make a rank of dates in each primary ID (sow&cycle). If i can it will be easier.

 Sow&Cycle Cycle Service MatingDate Mating Result Repeat Date 1try IRR1 SOW10-1 1 1 06/03/2022 Repetition 13/03/2022 7 SOW10-1 1 2 13/03/2022 Repetition 14/03/2022 1 SOW10-2 1 3 14/03/2022 Repetition should be 30/03/2022 16 SOW10-1 1 4 30/03/2022 Farrowed SOW10-2 2 1 24/06/2022 Repetition 26/07/2022 32 SOW10-2 2 2 26/07/2022 Repetition 05/08/2022 10 SOW10-3 2 3 05/08/2022 Farrowed

Can you help me? i think the error is in the ranking ASC of dates for each Sow&Cycle
Thanks a million!
Regards

1 ACCEPTED SOLUTION
Community Support

Hi @DBrito79 ,

If you want to fetch the previous row, please try this measure.

``````Repeat Date = VAR _PREVIOUSDATE=CALCULATE(MAX('Table'[MatingDate]),FILTER(ALLSELECTED('Table'),[Sow&Cycle]=MAX('Table'[Sow&Cycle])&&[Service]=MAX('Table'[Service])+1))
RETURN IF(MAX('Table'[Mating Result])="Farrowed",BLANK(),_PREVIOUSDATE)``````

Make sure to tick ‘Show items with no data’.

Best Regards,

Stephen Tao

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

3 REPLIES 3
Frequent Visitor

Sorry but the table have one error.
Here is the writ one

Thanks

 Sow&Cycle Cycle Service MatingDate Mating Result Repeat Date 1try IRR1 SOW10-1 1 1 06/03/2022 Repetition 13/03/2022 7 SOW10-1 1 2 13/03/2022 Repetition 14/03/2022 1 SOW10-1 1 3 14/03/2022 Repetition should be 30/03/2022 16 SOW10-1 1 4 30/03/2022 Farrowed SOW10-2 2 1 24/06/2022 Repetition 26/07/2022 32 SOW10-2 2 2 26/07/2022 Repetition 05/08/2022 10 SOW10-2 2 3 05/08/2022 Farrowed
Community Support

Hi @DBrito79 ,

If you want to fetch the previous row, please try this measure.

``````Repeat Date = VAR _PREVIOUSDATE=CALCULATE(MAX('Table'[MatingDate]),FILTER(ALLSELECTED('Table'),[Sow&Cycle]=MAX('Table'[Sow&Cycle])&&[Service]=MAX('Table'[Service])+1))
RETURN IF(MAX('Table'[Mating Result])="Farrowed",BLANK(),_PREVIOUSDATE)``````

Make sure to tick ‘Show items with no data’.

Best Regards,

Stephen Tao

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

Frequent Visitor

Hello Stephen Tao

Thank you very much
It works exacly!
Thanks again, you saved me a lot of time in getting this done
Best Regards
Diogo

Announcements

#### 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

#### Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

#### Power BI Monthly Update - March 2024

Check out the March 2024 Power BI update to learn about new features.

Top Solution Authors
Top Kudoed Authors