Dynamic conditional replace function

seaottr

Board Regular
Joined
Feb 10, 2010
Messages
60
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi Power Query gurus out there!

I'm going to do my best to explain some help that I need it replacing values, conditionally based on another column's values, while dynamically referencing field names.

Here's some sample data:

DateGreetingAppreciate Patience (if hold was used)Resolution StatementDiscuss Follow-UpGreeting Max PointsAppreciate Patience (if hold was used) Max PointsResolution Statement Max PointsDiscuss Follow-Up Max Points
8-22-2022​
5​
0​
20​
5​
5​
20​
20​
8-24-2022​
5​
2.5​
20​
5​
5​
20​
20​
8-23-2022​
5​
5​
0​
20​
5​
5​
20​
20​
8-24-2022​
5​
20​
20​
5​
5​
20​
20​
8-25-2022​
5​
2.5​
0​
5​
5​
20​
20​
8-26-2022​
5​
5​
5​
5​
20​
20​


I want to replace any values in the "Max Points" columns (the last 4) with zero if the corresponding non-"Max Points" columns' values are null. I need this to be dynamic in case I add more fields. The "Max Points" fields will always be identical to the non-"Max Points" fields with the exception of the " Max Points". I believe I may need to use a function like "each Record.Field", but not sure how to make it dynamic enough to replace the values with zero in the "Max Points" field by looking for nulls in the same field name without " Max Points".

I totally understand how complicated I made that sound, but not sure how else to phrase it...LOL!

So in the above above data set, I want to replace the 4th record in "Appreciate Patience (if hold was used) Max Points" with zero because "Appreciate Patience (if hold was used)" is null. Similarly, records 1, 2, 5 and 6 in "Discuss Follow-Up Max Points" should be zero and same with the last record for "Resolution Statement Max Points"

I understand I can do this by referencing each column separately, but I want this to be dynamic enough for future changes.

Any help would be greatly appreciated! Thank you in advance!
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    MaxPointsColNames = List.Select(Table.ColumnNames(Source), each Text.EndsWith(_,"Max Points")),
    ColNames = List.Transform(MaxPointsColNames, each Text.Start(_, Text.Length(_) - 11)),
    lst = Table.TransformRows(Source, each 
        List.Accumulate(ColNames, _, (s,c)=> if Record.Field(s, c) = null then Record.TransformFields(s, {{c & " Max Points", (x)=> 0}}) else s)),
    Result = Table.FromRecords(lst)
in
    Result

Book2
ABCDEFGHIJ
1DateGreetingAppreciate Patience (if hold was used)Resolution StatementDiscuss Follow-UpGreeting Max PointsAppreciate Patience (if hold was used) Max PointsResolution Statement Max PointsDiscuss Follow-Up Max Points
28-22-20225020552020
38-24-202252.520552020
48-23-202255020552020
58-24-202252020552020
68-25-202252.50552020
78-26-202255552020
8
9DateGreetingAppreciate Patience (if hold was used)Resolution StatementDiscuss Follow-UpGreeting Max PointsAppreciate Patience (if hold was used) Max PointsResolution Statement Max PointsDiscuss Follow-Up Max Points
108-22-2022502055200
118-24-202252.52055200
128-23-202255020552020
138-24-202252020502020
148-25-202252.5055200
158-26-2022555500
16
Sheet1
 
Upvote 0
Solution
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    MaxPointsColNames = List.Select(Table.ColumnNames(Source), each Text.EndsWith(_,"Max Points")),
    ColNames = List.Transform(MaxPointsColNames, each Text.Start(_, Text.Length(_) - 11)),
    lst = Table.TransformRows(Source, each
        List.Accumulate(ColNames, _, (s,c)=> if Record.Field(s, c) = null then Record.TransformFields(s, {{c & " Max Points", (x)=> 0}}) else s)),
    Result = Table.FromRecords(lst)
in
    Result

Book2
ABCDEFGHIJ
1DateGreetingAppreciate Patience (if hold was used)Resolution StatementDiscuss Follow-UpGreeting Max PointsAppreciate Patience (if hold was used) Max PointsResolution Statement Max PointsDiscuss Follow-Up Max Points
28-22-20225020552020
38-24-202252.520552020
48-23-202255020552020
58-24-202252020552020
68-25-202252.50552020
78-26-202255552020
8
9DateGreetingAppreciate Patience (if hold was used)Resolution StatementDiscuss Follow-UpGreeting Max PointsAppreciate Patience (if hold was used) Max PointsResolution Statement Max PointsDiscuss Follow-Up Max Points
108-22-2022502055200
118-24-202252.52055200
128-23-202255020552020
138-24-202252020502020
148-25-202252.5055200
158-26-2022555500
16
Sheet1

Sorry for the late reply JGordon11! I was done work for the weekend when you responded, so I didn't get a chance to try out your solution until now.

You, Sir, are a GENIUS!!!! Worked liked a charm and I can't begin to thank you enough for coming up with the solution! There's no way I would have EVER been able to figure that out. I'm amazed by your skills and expertise! Thank you SO much for your help with this!!!
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,136
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top