Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
Mants
Frequent Visitor

Replace values in matrix/table based on reference table

Hi all, 

I'm trying to create a table with a specific strcuture and im not sure what the best way is to create it, i hope someone could help.

 

My situation is that i have a table with codes, and a table with a certain amount, per code. i need to recreate the table with the same Areas and column headers, and replace the codes in the cells with the corresponding values (from the Sums table), and where there is no value, put 0 (or null)

 

Mants_0-1729009104774.png

Eventually there will be a table with around 1000 separate cells (and separate codes from table 1)

 

Is this possible to do in power query and if so what options are there? 

 

Thanks

1 ACCEPTED SOLUTION
Omid_Motamedise
Super User
Super User

Just copy the next code and past it into advance editor.

Summery:
Step 1: Unpviot table 1

Step 2: Merge the Table 2 and result of unpviot table 1

Step 3: remove extra column

Step 4: Pivot the result table

 

let

     Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcixKTVRwVNJRMgRiIyA2VorVgQo7AbmmQGwGxOYIYWcg1wKILUG6DJRiYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Area = _t, #"round 1" = _t, #"round 2" = _t, #"round 3" = _t]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Area"}, "Attribute", "Value"),
    Source2= Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcixKTVRwVNJRMgJiQ3OlWB2omBOQbwoSN0QVMwepM0GIOQP5lkBsbKYUGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Area = _t, Cluster = _t, Count = _t]),
    #"Merged Queries" = Table.NestedJoin(Source2, {"Area", "Cluster"}, #"Unpivoted Other Columns", {"Area", "Value"}, "New", JoinKind.LeftOuter),
    #"Expanded New" = Table.ExpandTableColumn(#"Merged Queries", "New", {"Attribute", "Value"}, {"Attribute", "Value"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded New",{"Cluster", "Value"}),
    #"Sorted Rows" = Table.Sort(#"Removed Columns",{{"Attribute", Order.Ascending}}),
    #"Pivoted Column" = Table.Pivot(#"Sorted Rows", List.Distinct(#"Sorted Rows"[Attribute]), "Attribute", "Count")
in
    #"Pivoted Column"

 

result

Omid_Motamedise_0-1729031911464.png

 

If this answer helped resolve your issue, please consider marking it as the accepted answer. And if you found my response helpful, I'd appreciate it if you could give me kudos. 

Thank you!


If my answer helped solve your issue, please consider marking it as the accepted solution.

View solution in original post

5 REPLIES 5
wdx223_Daniel
Community Champion
Community Champion

if the combined value of Area and Cluster in Table2 is unique.

=Table.ReplaceValue(Table1,each [Area],"",(x,y,z)=>Table2{[Area=y,Cluster=x]}?[Count]? ??x,List.Skip(Table.ColumnNames(Table1)))

Omid_Motamedise
Super User
Super User

Just copy the next code and past it into advance editor.

Summery:
Step 1: Unpviot table 1

Step 2: Merge the Table 2 and result of unpviot table 1

Step 3: remove extra column

Step 4: Pivot the result table

 

let

     Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcixKTVRwVNJRMgRiIyA2VorVgQo7AbmmQGwGxOYIYWcg1wKILUG6DJRiYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Area = _t, #"round 1" = _t, #"round 2" = _t, #"round 3" = _t]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Area"}, "Attribute", "Value"),
    Source2= Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcixKTVRwVNJRMgJiQ3OlWB2omBOQbwoSN0QVMwepM0GIOQP5lkBsbKYUGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Area = _t, Cluster = _t, Count = _t]),
    #"Merged Queries" = Table.NestedJoin(Source2, {"Area", "Cluster"}, #"Unpivoted Other Columns", {"Area", "Value"}, "New", JoinKind.LeftOuter),
    #"Expanded New" = Table.ExpandTableColumn(#"Merged Queries", "New", {"Attribute", "Value"}, {"Attribute", "Value"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded New",{"Cluster", "Value"}),
    #"Sorted Rows" = Table.Sort(#"Removed Columns",{{"Attribute", Order.Ascending}}),
    #"Pivoted Column" = Table.Pivot(#"Sorted Rows", List.Distinct(#"Sorted Rows"[Attribute]), "Attribute", "Count")
in
    #"Pivoted Column"

 

result

Omid_Motamedise_0-1729031911464.png

 

If this answer helped resolve your issue, please consider marking it as the accepted answer. And if you found my response helpful, I'd appreciate it if you could give me kudos. 

Thank you!


If my answer helped solve your issue, please consider marking it as the accepted solution.

Thank you Omid, much appreciated this helps!

Ahmedx
Super User
Super User

pls try this

let
 f = (w) =>
    Table.ReplaceValue(
      w, 
      (x) => x, 
      (x) => x, 
      (x, y, z) =>
        try List.Select(List.Select(lst, (p) => p{0} = y[Area]), (y) => y{1} = x){0}{2} otherwise 0, 
      List.Skip(Table.ColumnNames(Types))
    ),

    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcixKTVRwVNJRMgRiIyA2VorVgQpfmATkmwKxGRCbI8SdgVwLILYEaTNQio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Area = _t, #"round 1" = _t, #"round 2" = _t, #"round 3" = _t]),
    Types = Table.TransformColumnTypes(Source,{{"Area", type text}, {"round 1", Int64.Type}, {"round 2", Int64.Type}, {"round 3", Int64.Type}}),
    lst = Table.ToList(#"table 2 codes",(x)=>x),
    to =f(Types)
in
    to
lbendlin
Super User
Super User

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information. Do not include anything that is unrelated to the issue or question.

Need help uploading data? https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...

Please show the expected outcome based on the sample data you provided.

Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.