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
IEPMost
Helper III
Helper III

How to subtract yesterday´s value from today´s and then multiplicate with a factor? (in M or DAX)

Hi,

 

I guess it´s simple but I´m not able to find a solution..

 

I have three columns [Date], [meter reader] and [Factor]; I need to subtract today´s value from yesterday´s for [meter reader] and then multiplicate the result with a factor. Here is my input:

Datemeter readerFactor
04.09.202012000,5
05.09.202014000,6
06.09.202016000,3
07.09.202016500,5

 

My output should be like this:

Datemeter readerFactorConsumption
04.09.202012000,5100
05.09.202014000,6120
06.09.202016000,315
07.09.202016500,5 

 

for the first row: (1400 - 1200) * 0,5 = 100

2nd row: (1600 - 1400) * 0,6 = 120

...

 

Hope you know what I mean! Thank you very much for any help! Cheers

1 ACCEPTED SOLUTION
PhilipTreacy
Super User
Super User

Hi @IEPMost 

Copy/paste this into your Advanced Editor in Power Query.  Here's a sample PBIX file.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDRM7DUMzIwMlDSUTI0MgBRBnqmSrE6QDlTZDkTqJwZRM4MWc4MKmcMkTNHkTOFmxkLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"meter reader" = _t, Factor = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"meter reader", Int64.Type}, {"Factor", type number}}),
    Col = List.Combine({List.RemoveFirstN(Table.Column(#"Changed Type", "meter reader"),1),{0}}),
    Columns = List.Combine({Table.ToColumns(#"Changed Type"),{Col}}),
    #"Converted to Table" = Table.FromColumns(Columns,List.Combine({Table.ColumnNames(#"Changed Type"),{"Next"}})),
    #"Added Custom" = Table.AddColumn(#"Converted to Table", "Consumption", each if [Next] <> 0 then ([Next]-[meter reader])*[Factor] else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Next"})
in
    #"Removed Columns"

 

Regards

Phil


If I answered your question please mark my post as the solution.
If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


View solution in original post

4 REPLIES 4
PhilipTreacy
Super User
Super User

Hi @IEPMost 

Copy/paste this into your Advanced Editor in Power Query.  Here's a sample PBIX file.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDRM7DUMzIwMlDSUTI0MgBRBnqmSrE6QDlTZDkTqJwZRM4MWc4MKmcMkTNHkTOFmxkLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"meter reader" = _t, Factor = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"meter reader", Int64.Type}, {"Factor", type number}}),
    Col = List.Combine({List.RemoveFirstN(Table.Column(#"Changed Type", "meter reader"),1),{0}}),
    Columns = List.Combine({Table.ToColumns(#"Changed Type"),{Col}}),
    #"Converted to Table" = Table.FromColumns(Columns,List.Combine({Table.ColumnNames(#"Changed Type"),{"Next"}})),
    #"Added Custom" = Table.AddColumn(#"Converted to Table", "Consumption", each if [Next] <> 0 then ([Next]-[meter reader])*[Factor] else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Next"})
in
    #"Removed Columns"

 

Regards

Phil


If I answered your question please mark my post as the solution.
If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Hi @PhilipTreacy ,

 

almost.. I get an error in the last line (doesn´t recognize "meter reader" in the "added custom" line).
[Next] - [meter reader]

 

Why is that? Thank you a lot!!

Hi @IEPMost 

That line is taking the name of the column meter reader as shown in your sample image.

If your real data has a different column name then you need to change the code to pick this up.  That is, if your column is actually called XYZ then change the code to [Next] - [XYZ].

NOTE: column names are case sensitive so meter reader is different to Meter Reader, so make sure the your column name and the code use the same case.

Regards

Phil


If I answered your question please mark my post as the solution.
If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Hi @PhilipTreacy ,

 

thank you for your help! That wasn´t the problem; I changed the Code in my column names. Anyway, the column [next] was very helpful. I just subtracted my [meter reader] from [Next] and multiplied it by the factor. Thank you 🙂

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.