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
pye
Helper I
Helper I

Convert the data into same rate in new column

Hi there!

 

I have two tables here, conversion table and data table. The value in data table is inserted by user with different mass unit (g, kg, lb, oz etc.). I would like to convert the inserted value into same unit in kg, referring to the conversion table.

 

Here is the illustration, new column (green, column I) created to show the convertion of inserted value in column G-H. This can be achieved by multiplication and vlookup from the coversion table. However, is there anyway to achieve it in power query?

 

pye_1-1670396114308.png

 

Currently, I am using hardcode in power query M language, by adding a new columnn and using if / else function as suggested by one of the questions.

 

if [unit] = "g" then [value]*0.001 else if [unit] = "kg" then [value]*1 else if [unit] = "lb" then [value]*0.45359237 else if [unit] = "oz" then [value]*0.02834952 else if [unit] = null then [value] else "error"

 

 

Checking if there is a better turn around on this as there are more than 30 conversion rate to type in. Thanks!

 

1 ACCEPTED SOLUTION
wdx223_Daniel
Super User
Super User

=ConversionTable{[#"From unit"=[Unit]]}?[Conversion Rate]?*[Value]

 

View solution in original post

4 REPLIES 4
pye
Helper I
Helper I

Hey @wdx223_Daniel thanks for your help on this custom formula. May I check is there is topic related on this? I would like to do some learning, but couldn't find it such as the ? validation.

 

As I have another doubt on writing the code if I would like to add an additional custom column to show the convertion unit referring to the conversion table, when there are different "to unit" in the conversion table.

pye_0-1670922981873.png

 

Tried to add a new custom column with code: ConversionRate{[#"From unit"=[Unit]]}?[Convert to Universal Unit])

It is not working, as seems the final result is a number and causing error.

according to your snapshot sample data, you can try this code to add two columns in one step.

NewStep=#table(Table.ColumnNames(DataTable)&{"To Value","To Unit"},List.Transform(Table.ToRows(PreviousStepName),each let a=ConversionUnitTable{[#"From unit"=_{2}]}?,b=if a=null then {null,null} else {a[Conversion Rate]*_{1},a[To unit]} in _&b))

 

and about the learning of M code, i suggest you to read the book of "M is for data monkey", which is my first M book.

thank you! i learnt a lot from this

wdx223_Daniel
Super User
Super User

=ConversionTable{[#"From unit"=[Unit]]}?[Conversion Rate]?*[Value]

 

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.

Top Solution Authors
Top Kudoed Authors