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

How to subtract if column is having both 'text' and 'number'?

Hi All,

 

I am having a table in power query. I want to subtract old value from new value. However my issue is that my columns have both 'text' and 'number' as shown in image. When i am trying to do this in power query, the subtract column shows error due to data type issues. I want to get the final column as shown in 'Final Remark'. How is this possible in power query? or will you advise DAX?

 

adhumal2_0-1644586177471.png

 

2 ACCEPTED SOLUTIONS
BA_Pete
Super User
Super User

Hi @adhumal2 ,

 

Try this in a new custom column:

 

let
  oldValue = Number.From([oldValue]),
  newValue = Number.From([newValue]),
  variance = Number.Abs(newValue - oldValue)  
in
try if [oldValue] = [newValue] then "Same"
else if Value.Is(oldValue, type number) and Value.Is(newValue, type number) and newValue < oldValue then Text.From(variance) & " reduced"
else if Value.Is(oldValue, type number) and Value.Is(newValue, type number) and newValue > oldValue then Text.From(variance) & " added"
else null otherwise "Unrated to Rated"

 

 

Output:

BA_Pete_0-1644588415587.png

 

Full M query:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlLSUTJWitWJVjKBs0rzihJLUlOAfBM0PowFUw+RN4XojAUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [oldValue = _t, newValue = _t]),
    addFinalRemark = Table.AddColumn(Source, "finalRemark", each let
  oldValue = Number.From([oldValue]),
  newValue = Number.From([newValue]),
  variance = Number.Abs(newValue - oldValue)  
in
try if [oldValue] = [newValue] then "Same"
else if Value.Is(oldValue, type number) and Value.Is(newValue, type number) and newValue < oldValue then Text.From(variance) & " reduced"
else if Value.Is(oldValue, type number) and Value.Is(newValue, type number) and newValue > oldValue then Text.From(variance) & " added"
else null otherwise "Unrated to Rated")
in
    addFinalRemark

 

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

Hi @adhumal2 ,

 

This bit needs to be changed:

BA_Pete_0-1644591427201.png

 

Should be:

try if [#"Old Value - with OUT"] = [#"New Value - with Out"] then "Same"

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

6 REPLIES 6
adhumal2
Helper III
Helper III

@BA_Pete Thank you very much! It worked nicely.

 

I have another question - where can we learn powerquery / M code in easy to understand manner? 

Hi @adhumal2 ,

 

I'd start with Microsoft Learn. It takes you right from beginner to advanced and I *think* it's all free (except for certification exams).

https://docs.microsoft.com/en-us/learn/powerplatform/power-bi 

 

Other than that, I'd say that just working with Power Query day-to-day will be a good way to learn in an understandable way.

Look for real-world problems that you have, or things you would like to do with your own data, and search the Power Query forums here for related threads. The solutions provided here are usually explained in very clear terms and, when you have a real-world problem that you need to solve, the PQ solution becomes far more relatable and therefore more easily understood and learnt.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




@BA_Pete Thank you very much! I agree with you.

BA_Pete
Super User
Super User

Hi @adhumal2 ,

 

Try this in a new custom column:

 

let
  oldValue = Number.From([oldValue]),
  newValue = Number.From([newValue]),
  variance = Number.Abs(newValue - oldValue)  
in
try if [oldValue] = [newValue] then "Same"
else if Value.Is(oldValue, type number) and Value.Is(newValue, type number) and newValue < oldValue then Text.From(variance) & " reduced"
else if Value.Is(oldValue, type number) and Value.Is(newValue, type number) and newValue > oldValue then Text.From(variance) & " added"
else null otherwise "Unrated to Rated"

 

 

Output:

BA_Pete_0-1644588415587.png

 

Full M query:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlLSUTJWitWJVjKBs0rzihJLUlOAfBM0PowFUw+RN4XojAUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [oldValue = _t, newValue = _t]),
    addFinalRemark = Table.AddColumn(Source, "finalRemark", each let
  oldValue = Number.From([oldValue]),
  newValue = Number.From([newValue]),
  variance = Number.Abs(newValue - oldValue)  
in
try if [oldValue] = [newValue] then "Same"
else if Value.Is(oldValue, type number) and Value.Is(newValue, type number) and newValue < oldValue then Text.From(variance) & " reduced"
else if Value.Is(oldValue, type number) and Value.Is(newValue, type number) and newValue > oldValue then Text.From(variance) & " added"
else null otherwise "Unrated to Rated")
in
    addFinalRemark

 

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




@BA_Pete Many Thanks for jumping in.

 

I am getting 'Unrated to Rated' every row. below is the custom column formula . Am i doing anything wrong?

 

let
oldValue = Number.From([#"Old Value - with OUT"]),
newValue = Number.From([#"New Value - with Out"]),
variance = Number.Abs(newValue - oldValue)
in
try if [oldValue] = [newValue] then "Same"
else if Value.Is(oldValue, type number) and Value.Is(newValue, type number) and newValue < oldValue then Text.From(variance) & "Less"
else if Value.Is(oldValue, type number) and Value.Is(newValue, type number) and newValue > oldValue then Text.From(variance) & "More"
else null otherwise "Unrated to Rated"

 

adhumal2_0-1644590854482.png

 

Hi @adhumal2 ,

 

This bit needs to be changed:

BA_Pete_0-1644591427201.png

 

Should be:

try if [#"Old Value - with OUT"] = [#"New Value - with Out"] then "Same"

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




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!

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