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
Hi NMorenoLAC , first off, thank you for providing such details and sample data to work it. This case was very interesting to me. I solved it. You can download the files from my shared drive. Basically I created t_[color] columns as...
t_blue = IF(
'PlannerTasks'[tagblue] = TRUE,
RELATED(TagNames[blue]),
BLANK()
)
And the final 'alltags' column as...
allTags =
CONCATENATE(
IF(ISBLANK('PlannerTasks'[t_blue]), "", 'PlannerTasks'[t_blue] & ", "),
CONCATENATE(
IF(ISBLANK('PlannerTasks'[t_aqua]), "", 'PlannerTasks'[t_aqua] & ", "),
CONCATENATE(
IF(ISBLANK('PlannerTasks'[t_darkGreen]), "", 'PlannerTasks'[t_darkGreen] & ", "),
CONCATENATE(
IF(ISBLANK('PlannerTasks'[t_pink]), "", 'PlannerTasks'[t_pink] & ", "),
CONCATENATE(
IF(ISBLANK('PlannerTasks'[t_lime]), "", 'PlannerTasks'[t_lime] & ", "),
IF(ISBLANK('PlannerTasks'[t_red]), "", 'PlannerTasks'[t_red])
)
)
)
)
)
https://1drv.ms/f/s!Aq3n-sopiGyqgols-1JPgWEEAo6jkQ?e=5pTDTD
If I answered your question, please mark this thread as accepted and Thums Up!
Follow me on LinkedIn:
https://www.linkedin.com/in/mustafa-ali-70133451/
- NMorenoLAC2 years agoRegular Visitor
Appreciate your response. Dax was going to be my back up, cause I am more comfortable with it but also knew it would be a lot of replication I'd have to do for every color. The sample files definitely help as well. Thank you for the thorough response.