Skip to main content
cancel
Showing results for 
Search instead 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.

Reply
Alirezam
Helper V
Helper V

Subtracting the rows in Query based on a specific column

Hi guys,

I want to make this change in Query and not on the table in Desktop view:

I have a table as attached here, it has multiple values in "Point Name" columns, I need to subtract the "Point Value" for each consecutive row (the table is already sorted descendingly on Date & Time). The challenge is this subtraction needs to be applied on each "Point Name" separately as they are indicating separate meters. I need to know how to write the M code whilst adding a new column.

Many thanks

 

 

Capture.PNG

1 ACCEPTED SOLUTION
V-pazhen-msft
Community Support
Community Support

@Alirezam 

 

Add an index from 0, then create the following custom column, and remember replace error and change data type.

if ([Meter] = #"Added Index"{[Index]+1} [Meter]) then ([Reading]-  #"Added Index"{[Index]+1} [Reading]) else ""

V-pazhen-msft_6-1616569987365.png

 

Check attached pbix for detail.

 


Paul Zheng _ Community Support Team
If this post helps, please Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
V-pazhen-msft
Community Support
Community Support

@Alirezam 

 

Add an index from 0, then create the following custom column, and remember replace error and change data type.

if ([Meter] = #"Added Index"{[Index]+1} [Meter]) then ([Reading]-  #"Added Index"{[Index]+1} [Reading]) else ""

V-pazhen-msft_6-1616569987365.png

 

Check attached pbix for detail.

 


Paul Zheng _ Community Support Team
If this post helps, please Accept it as the solution to help the other members find it more quickly.

Hi,

This solution is so clever and I used it in Power query however, exactly the same 'step' seems not to work in 'dataflow' and the 'new column' returns 'error'. any idea why the same command does not work in dataflow? thanks

amitchandak
Super User
Super User

@Alirezam , I am assuming you need the previous row. Check if this article can help

 

https://exceltown.com/en/tutorials/power-bi/powerbi-com-and-power-bi-desktop/power-bi-data-sources/p...

 

if not

Can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data.

Hi,

Thanks but this is not answering what I asked. Here is the table I have and the 'Diff' column is what I want to create. As you see, the function of difference should apply for each 'Meter' separately.

 

Capture.PNG

Helpful resources

Announcements
Microsoft Fabric Learn Together

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

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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