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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
KM07
New Member

REplacing value based on grouping in another columns and condition

Hello,

I've searched in forums and googled but I haven't found answer how to replace value if the condition is based on one column grouping and value of another column. If to look the sample data (there are other columns as well which are not basis of the replacing)

CurrencyNodeType
EUR180dOIS
EUR180dIB
EUR270dOIS
EUR270dIB
EUR270dTR
EUR1yrSW
GBP7dIB
USD3dIB
USD3dTR

 

What I need is that under each currency separately where Type "OIS" is reprsented everything else beside "TR" is changed to "OIS". If there's no "OIS", it should remain as it is. Result should be as follows:

CurrencyNodeTypeReplace
EUR180dOISOIS
EUR180dIBOIS
EUR270dOISOIS
EUR270dIBOIS
EUR270dTRTR
EUR1yrSWOIS
GBP7dIBIB
USD3dIBIB
USD3dTRTR

 

I can't use group by since I need the table to remain in the same format since there are other fields which I use in latter steps based on this new field.
Thanks in advance.

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcg0NUtJRMrQwSAFS/p7BSrE6aIKeTkhiRuZYFEIFsSkMCUI2sLIISAaHg4XcnQKAHHMkjaHBLkCOMXYRkEGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Currency = _t, Node = _t, Type = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Currency", type text}, {"Node", type text}, {"Type", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Currency"}, {{"Rows", each _, type table [Currency=nullable text, Node=nullable text, Type=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "has OIS", each List.Contains([Rows][Type],"OIS")),
    #"Expanded Rows" = Table.ExpandTableColumn(#"Added Custom", "Rows", {"Node", "Type"}, {"Node", "Type"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded Rows", "Replace", each if [Type]="TR" or [has OIS] = false then [Type] else "OIS"),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"Currency", "Node", "Type", "Replace"})
in
    #"Removed Other Columns"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

View solution in original post

2 REPLIES 2
KM07
New Member

Great. I used it in my code from #"Changed Type" and it works. Thank you.

lbendlin
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcg0NUtJRMrQwSAFS/p7BSrE6aIKeTkhiRuZYFEIFsSkMCUI2sLIISAaHg4XcnQKAHHMkjaHBLkCOMXYRkEGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Currency = _t, Node = _t, Type = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Currency", type text}, {"Node", type text}, {"Type", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Currency"}, {{"Rows", each _, type table [Currency=nullable text, Node=nullable text, Type=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "has OIS", each List.Contains([Rows][Type],"OIS")),
    #"Expanded Rows" = Table.ExpandTableColumn(#"Added Custom", "Rows", {"Node", "Type"}, {"Node", "Type"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded Rows", "Replace", each if [Type]="TR" or [has OIS] = false then [Type] else "OIS"),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"Currency", "Node", "Type", "Replace"})
in
    #"Removed Other Columns"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors