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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
naveen_85
New Member

Substract Previous row

Hello Team,

 

i have the below table in power BI, so i need to calculate the below formula by using the below table. much appreciated if you could help on the below.

 

i need to substract with previous row (C2-B2) if satisfies thn it will call (C3-(C2-B2))/C2,C3/C2).

 

=IF((C2-B2)>0,(C3-(C2-B2))/C2,C3/C2)

 

Data refers in excel file as below;

 

C2 = 133

B2 = 128

C3 = 10 ( New Hired Column)

 

MonthTotal PayrollNew HiredTotal temporary
2021061281111
2021071331015
2021081351213
2021091401410
1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @naveen_85 

 

You can first add an Index column which starts at 0 (this column will be used like row number in the following step). Then add a custom column with below code. "Added Index" is the previous step name. Modify it according to your transformation steps. 

let 
  previousTotal = #"Added Index"{[Index]-1}[Total Payroll],
  diffTotal = [Total Payroll] - previousTotal
in 
  if diffTotal > 0 then ([New Hired]-diffTotal)/[Total temporary] else [New Hired]/[Total Payroll]

083003.jpg

 

There is an error in the first row because it doesn't have a previous row. You can replace error value with Replace Errors feature under Transform tab > Replace Values. You can format the numbers in the custom column per your need. 

 

Regards,
Community Support Team _ Jing
If this post helps, please Accept it as the solution to help other members find it.

View solution in original post

2 REPLIES 2
v-jingzhang
Community Support
Community Support

Hi @naveen_85 

 

You can first add an Index column which starts at 0 (this column will be used like row number in the following step). Then add a custom column with below code. "Added Index" is the previous step name. Modify it according to your transformation steps. 

let 
  previousTotal = #"Added Index"{[Index]-1}[Total Payroll],
  diffTotal = [Total Payroll] - previousTotal
in 
  if diffTotal > 0 then ([New Hired]-diffTotal)/[Total temporary] else [New Hired]/[Total Payroll]

083003.jpg

 

There is an error in the first row because it doesn't have a previous row. You can replace error value with Replace Errors feature under Transform tab > Replace Values. You can format the numbers in the custom column per your need. 

 

Regards,
Community Support Team _ Jing
If this post helps, please Accept it as the solution to help other members find it.

lbendlin
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMjQwU9JRMjSyAJGGECJWByplDuIbG4NIAxBhipACqzc2BesFMxFSliC+CViDCURrbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, #"Total Payroll" = _t, #"New Hired" = _t, #"Total temporary" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Total Payroll", Int64.Type}, {"New Hired", Int64.Type}, {"Total temporary", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each if [Total Payroll]-#"Changed Type"[Total Payroll]{[Index]-1} > 0 then [New Hired]-[Total Payroll]+#"Changed Type"[Total Payroll]{[Index]-1} else [New Hired]/[Total Payroll])
in
    #"Added Custom"

 

Note:  your teminology is unusual. Usually rows have numbers and columns have letters.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Solution Authors