Forum Discussion
If value is TRUE, replace value from another table where one column equals another column
- 2 years ago
Hi NMorenoLAC,
amustafa created solution in DAX so I created one in Power Query.
Important: I'm not sure if you have null values as power query null or as text "null". I've used text "null" in my query. If you have default PQ null - let me know and I'll update the query.How to use my query:
1st: create blank query, open Advanced Editor, delete whole code and paste there my query.
Then:
Query:
//If value is TRUE, replace value from another table where one column equals another column //https://community.fabric.microsoft.com/t5/Power-Query/If-value-is-TRUE-replace-value-from-another-table-where-one/m-p/3671868#M120793 let Table_TagNames = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCklNzFUwVNJRCk9NKs4sSS0GMvNKc3IQVFBqQX5RCbp4rA5UrxFCIjg1ubQos6QSxCwtAOlCyLkkFmck5ScWpRRjmGAMFHEsKMjJTE4syczPQ7LJPb8stSgvMS85FcNVHqk5BSmpxdlKsbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [team = _t, blue = _t, aqua = _t, darkGreen = _t, pink = _t, lime = _t, red = _t]), Table_TagNames_HeadersLower = Table.TransformColumnNames(Table_TagNames, Text.Lower), Table_PlannerTasks = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCkkszvZ0MVTSUQpJTcxVADOCQl2BVF5pTg6CwiIYqwPTbgTTboRXA6ogQrsxTLsxMdZCBRHaTXBrx0qhaTfF4nh8tqM53gw56HDbh6Y9FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [taskId = _t, team = _t, tagblue = _t, tagAqua = _t, tagDarkGreen = _t, tagPink = _t, tagLime = _t, tagRed = _t]), Source = Table_PlannerTasks, Table_PlannerTasksHeadersEdited = Table.TransformColumnNames(Source, each if Text.StartsWith(_, "tag") then Text.AfterDelimiter(Text.Lower(_), "tag") else _), MergedQueryItself = Table.NestedJoin(Table_PlannerTasksHeadersEdited, {"team"}, Table_TagNames_HeadersLower, {"team"}, "Table_TagNames_HeadersLower", JoinKind.LeftOuter), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(MergedQueryItself, {"taskId", "team", "Table_TagNames_HeadersLower"}, "Attribute", "Value"), Ad_Color = Table.AddColumn(#"Unpivoted Other Columns", "Color Tag Name", each Record.FieldOrDefault(Table.ToRecords([Table_TagNames_HeadersLower]){0}, [Attribute], null), type text), ReplaceValues_ValueColumn = Table.ReplaceValue(Ad_Color, each [Value], each [Color Tag Name], (x,y,z)=> if x = "TRUE" then z else y, {"Value"} ), #"Removed Columns" = Table.RemoveColumns(ReplaceValues_ValueColumn,{"Table_TagNames_HeadersLower", "Color Tag Name"}), #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "Value"), RenamedColumnsBackToOriginal = Table.RenameColumns(#"Pivoted Column", List.Zip({Table.ColumnNames(#"Pivoted Column"), Table.ColumnNames(Source)})), Ad_allTags = Table.AddColumn(RenamedColumnsBackToOriginal, "allTags", each Text.Combine(List.Select(Record.ToList(Record.SelectFields(_, List.Select(Record.FieldNames(_), each Text.StartsWith(_, "tag")))), each _ <> "null"), ", "), type text) in Ad_allTags
Just wanted to say thank you to all of you. I ended up using parts of everyone's feedback to ultimately come up with a solution. I modified dufoq3 query to replace all of the color tags with the appropriate values, christinepayton to create my tag table for filtering using the "unpivot other columns method", and used a modified version of amustafa dax for the allTags column. Couldn't have done it without all of your help! 😊