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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
jcatswi
Frequent Visitor

Conditional based on difference between current and previous row from different columns

Dear community,

 

How do I compute the difference (days) between a column and the next row of another column and build a conditional which uses this difference?

 

As it can be seen in the example table, the same client can have several contracts, so I need to compute for all clients, what is the difference between the end of one contract and the beginning of the next one (in bold). That is, difference between current and previous row from different columns. 

 

Then, I would like to create a column called Time Lapsed (the desired columns shown in italics). This is a conditional which uses that computed difference and has several cases. This is not defined for the first contract. For (current) clients who only have one contract this is 0.  How do I create this conditional (switch?) with indexes from two different rows and columns?

 

I thought about something like that but I do not know how to implement it in Power BI :

 

If

Client ID (index) = Client ID (index -1)  

Case 1: If Contract End empty then  Time Lapsed = 0

Case 2: Time Lapsed = Contract Start (index) – Contract End (index-1)

Else

Time Lapsed = NA

 

At the end, I need to classify clients by status, i. e. created another column, where I would have the following cases:

 

1: Left: If Client ID (index) = Client ID (index -1) and (Time Lapse(index) = 1 or Time Lapse(index) = NA) and Contract end (index) < Today

2: Current: If Client ID (index) = Client ID (index -1) and Time Lapse(index) = 1 and

Contract end (index) Empty

3: Returned Left: If Client ID (index) = Client ID (index -1) and Time Lapse(index) > 1 and Contract end (index) < Today

4: Returned Current: If Client ID (index) = Client ID (index -1) and Time Lapse(index) < 1 and Contract end (index) empty

 

Both columns required similar construction, which requires a conditional based on comparing current and previous row from different columns, and I do not how to do this in Power BI. I would appreciate any help on this. I have seen several solutions on difference betwenn current and previos row but not with a conditional based on that. 

 

Client ID

Contract Number

Contract Start

Date

Contract End

Date

Time Lapsed

Status

50951328184

543972

 01/10/14

 31/12/16

NA

 

50951328184

515577

 01/01/17

 30/09/17

1

Left

50951502704

515580

 01/10/17

 31/12/17

NA

 

50951502704

514651

 01/01/18

 

1

Current

50951362629

745280

 01/01/19

 31/12/19

NA

 

50951362629

543972

 01/01/20

 31/12/20

1

 

50951362629

515211

 01/01/22

 30/09/22

365

Returned Left

50951948031

744544

 01/10/21

 30/06/22

NA

 

50951948031

121237

 01/07/22

 31/12/22

1

 

50951948031

455899

 01/02/23

31/08/22

30

 

50951948031

564654

01/10/2023

 

0

Returned Current

50951265039

158898

01/10/17

 

0

Current

50951271747

685645

01/07/22

01/07/23

NA

Left

 

3 REPLIES 3
lbendlin
Super User
Super User

How do I compute the difference (days) between a column and the next row of another column and build a conditional which uses this difference?

Power BI does not guarantee row numbering.  You must bring your own index column to definitively indicate how you want your rows sorted. 

Hello,

 

Thanks for your comments, yes I realised that. So I added two indexes, one which orders the clients by ID and contract dates, and one which orders the clients by ID only:

 

Client IDContract Number IndexIndex ContractContract Start DateContract End Date
509513265325011361610/1/2014 0:009/30/2015 0:00
509513265325011362610/1/2015 0:0012/31/2016 0:00
50951326532501136361/1/2017 0:009/30/2017 0:00
509513265325011364610/1/2017 0:0012/31/2017 0:00
50951326532501136561/1/2018 0:0012/31/2018 0:00
50951326532501136661/1/2019 0:002/25/2020 0:00
509521547028809447810/1/2014 0:009/30/2015 0:00
509521547028809448810/1/2015 0:0012/31/2016 0:00
50952154702880944981/1/2017 0:009/30/2017 0:00
5095215470288094410810/1/2017 0:0012/31/2017 0:00
509521547028809441181/1/2018 0:0012/31/2018 0:00
509521547028809441281/1/2019 0:009/30/2021 0:00
5095215470288094413810/1/2021 0:006/30/2022 0:00
509521547028809441487/1/2022 0:009/30/2022 0:00
5095215470288094415810/1/2022 0:0012/31/2022 0:00

 

How do i go through the rows comparing whether the are the same ID and the difference between the end date of one and the start date of the next? 

 

Thanks!

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ldFdDoQgDATgq2x8NqEdKD97FeP9r7GgyyZUNtUHozTxy5TZtkWoCHtE8VjWemL2sX5wfY43OXYgDi96E9VBcZ7aQM7Bvv4loAjpBMP5YxJNw3fjJJJOkUwhqBTpksI2ZEyRL0Q2iTgSpRNwkDoAjQJYQqIm5EwltCVSOzxoZEJkRZiNTIzSjXuNTAQmFcOsZIbwmMPsZGZgNIraBWwTftyl/1LL/hqwjV5NOgnoGDcIUTGgr+OH7B8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Client ID" = _t, #"Contract Number " = _t, Index = _t, #"Index Contract" = _t, #"Contract Start Date" = _t, #"Contract End Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Client ID", Int64.Type}, {"Contract Number ", Int64.Type}, {"Index", Int64.Type}, {"Index Contract", Int64.Type}, {"Contract Start Date", type datetime}, {"Contract End Date", type datetime}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Difference", each if [Index]=1 then null
else if [#"Contract Number "]<>#"Changed Type"{[Index]-2}[#"Contract Number "] then null
else [Contract Start Date]-#"Changed Type"{[Index]-2}[Contract End Date])
in
    #"Added Custom"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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