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
Chirantan
Frequent Visitor

Subtract rows in a Table based on a id

Hi I have a table as follows:

SourceTable.PNG

 

And I would like to achieve something like this:

 

Output.PNG

 

I am trying to subtract rows based in the Attribute/indx, which in turn is trying to find the diffence between attribute/idx in each preceding year.

 

the above data is a subset of the original, with multiple InstituionNumber, Typekey and Date.

 

 

Thanks in advance.

P.S. new to powerBi and DAX.

1 ACCEPTED SOLUTION

Hi Chirantan,

 

To achieve your requirement, you can create a calculate column as below:

Calculation = 
VAR First_Value = Table1[Value]
VAR First_Index = Table1[Index]
VAR Last_Value = CALCULATE(MAX(Table1[Value]), FILTER(Table1, Table1[Index] = First_Index + 4))
RETURN
IF(Last_Value = 0, 0, Last_Value - First_Value)

捕获.PNG 

 

Regards,

Jimmy Tao

View solution in original post

4 REPLIES 4
Greg_Deckler
Community Champion
Community Champion

So, where is the first number in your Calculation column coming from? I mean, what are the rules?

 

But, in general, you are probably going to have to use EARLIER so...See my article on Mean Time Before Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395...



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Hi Greg,

 

All the Attribute/Indx with the same value needs to be substracted. I am hilighting the rows to show how the calculatin is being performed.

Edited.PNG

 

 

 

I am just finding it difficult to apply DAX, as a work around I am using SQL to get the same result...but it is making the data model awkward.

Hi Chirantan,

 

To achieve your requirement, you can create a calculate column as below:

Calculation = 
VAR First_Value = Table1[Value]
VAR First_Index = Table1[Index]
VAR Last_Value = CALCULATE(MAX(Table1[Value]), FILTER(Table1, Table1[Index] = First_Index + 4))
RETURN
IF(Last_Value = 0, 0, Last_Value - First_Value)

捕获.PNG 

 

Regards,

Jimmy Tao

Right, got it. Read the article, it will get you to where you need to be. Basically, you want to use EALIER to filter out all other matching rows into a temporary table that you create with VAR. Then, use MINX to get the minimum Date. Then use FILTER to filter the temp table to that minimum date and finally use any aggregation to get the value in that row and subtract the current row's value. The article referenced demonstrates this procedure.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

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.