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
WALEED
Advocate I
Advocate I

Transform Values taken from a Lookup Table

This is a tough. Please help!

 

I'm trying to change multiple values in multiple fields on a table. I have another table which has the name of fields, old values, and new values.  e.g.

 

Source Table:

INDEX

FIELD 1FIELD 2
BOB001NOT AVAILABLE.3 mm
BOB002ABCN/A
BOB003DEGBLA BLA BLA

 

Lookup table:

FIELD NAMEOLD VALUENEW VALUE
FIELD 1NOT AVAILABLENA
FIELD 1DEGDEF
FIELD 2.3 mm0.3 mm
FIELD 2N/ANA

 

Result:

TAGFIELD 1FIELD 2
BOB001NA0.3 mm
BOB002ABCNA
BOB003DEFBLA BLA BLA

 

The lookup table is BIG and also gets changed and appended often. That's why I don't want to do the changes using an old school formula.

1 ACCEPTED SOLUTION

HI @WALEED

 

Yes in that case Power Query is the choice

 

See the attached file here fro Power Query solution


Regards
Zubair

Please try my custom visuals

View solution in original post

7 REPLIES 7
Zubair_Muhammad
Community Champion
Community Champion

@WALEED

 

Do you want a calculated column, NEW TABLE, MEASURE?

 

For example you can add following Calculated Column in Source Table

 

Revised Field 1 =
LOOKUPVALUE (
    LookUpTable[NEW VALUE],
    LookUpTable[OLD VALUE], SourceTable[FIELD 1],
    LookUpTable[FIELD NAME], "FIELD 1"
)

1021.png


Regards
Zubair

Please try my custom visuals

@WALEED

 

In case you want the Old Value when there is no corresponding NEW value, you can use this

Revised Field 1 =
VAR myvalue =
    LOOKUPVALUE (
        LookUpTable[NEW VALUE],
        LookUpTable[OLD VALUE], SourceTable[FIELD 1],
        LookUpTable[FIELD NAME], "FIELD 1"
    )
RETURN
    IF ( ISBLANK ( myvalue ), SourceTable[FIELD 1], myvalue )

1022.png


Regards
Zubair

Please try my custom visuals

Many thanks; please allow me to pick your brain a little more:

 

I'm using Power BI. will this work in Power Query?

Is there a way to identify the field name using the first column of the lookup table?

And finally, replace the original column with the transformed one.

 

The reason I want it to be dynamic is there are 243 ever-changing fields.

HI @WALEED

 

Yes in that case Power Query is the choice

 

See the attached file here fro Power Query solution


Regards
Zubair

Please try my custom visuals

You, sir, are a LIFESAVER!!

 

Many thanks for the effort you've put into creating that example pbix.

I'll share the formulae below: (I hope you don't mind) for the benefit of others.

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\hp\Desktop\Forum Posts\PowerBI\Transform Values taken from a Lookup Table.xlsx"), null, true),
    SourceTable_Table = Source{[Item="SourceTable",Kind="Table"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(SourceTable_Table,{{"INDEX", type text}, {"FIELD 1", type text}, {"FIELD 2", type text}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"INDEX"}, "Attribute", "Value"),
    #"Merged Queries" = Table.NestedJoin(#"Unpivoted Columns",{"Attribute", "Value"},LookUpTable,{"FIELD NAME", "OLD VALUE"},"LookUpTable",JoinKind.LeftOuter),
    #"Expanded LookUpTable" = Table.ExpandTableColumn(#"Merged Queries", "LookUpTable", {"NEW VALUE"}, {"LookUpTable.NEW VALUE"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded LookUpTable", each true),
    #"Added Custom" = Table.AddColumn(#"Filtered Rows", "Revised Values", each if [LookUpTable.NEW VALUE]=null then [Value] else [LookUpTable.NEW VALUE]),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"INDEX", "Attribute", "Revised Values", "Value", "LookUpTable.NEW VALUE"}),
    #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Value", "LookUpTable.NEW VALUE"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "Revised Values")
in
    #"Pivoted Column"

@WALEED

 

Here are the steps for Power Query Solution

 

Step #1: select Field Columns in Source Table and unpivot them

 

1023.png

 

Step 2: Perform a Merge Query using 2 fields each from Source Table and LookUp Table

1024.png


Regards
Zubair

Please try my custom visuals

@WALEED

 

You can follow the remaining steps from the Query Editor.

 

This is the end result.

 

1025.png


Regards
Zubair

Please try my custom visuals

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.