Add Custom Column to check for the same digits in a cell

programsam

Board Regular
Joined
Feb 10, 2016
Messages
123
Greetings, attempting to add a custom column in PQ that runs down a column and if the column has numbers of the same value in them it marks the custom column with an "X".

In the screenshot below, the first and third numbers have the same digits in the cells so it would mark an "X" in the custom column.

1697577548484.png
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Assuming the value you are testing is in cell A1, for example, change reference as necessary...

For the test: first three digits are the same...

=if(and(left(text(A1,"####"),1)=mid(text(A1,"####"),2,1),left(text(A1,"####"),1)=mid(text(A1,"####"),3,1)),"X","")

For the test: first and third digits are the same... (as in your question)

=if(and(left(text(A1,"####"),1)=mid(text(A1,"####"),3,1)),"X","")
 
Upvote 0
One way of doing this is creating a list from the whole number by using the Text.ToList() function, then using List.Distinct() function to return only distinct values from this list, and counting the list items by using the List.Count() function. If the result is 1 then it means all digits are the same and you can return "X", otherwise return null or empty string or something else you desire. See the following sample query (copy and paste it into a blank query in PQ advanced editor). I used manually entered data as sample, so you can safely ignore the Source and ChangeTypes steps, and focus on AddColumn step by using your own Source data and column names.

Power Query:
let
  Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WsoQDpVidaCVDI2MTUzNzCwjPFA6UYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
  ChangeTypes = Table.TransformColumnTypes(Source, {{"Column1", Int64.Type}}),
  AddColumn = Table.AddColumn(ChangeTypes, "Check", 
                each if List.Count(
                      List.Distinct(
                        Text.ToList(Text.From([Column1]))
                    )
                  ) = 1 then "X" else null
                )
in
  AddColumn

1697582767455.png
 
Upvote 1
Solution

Forum statistics

Threads
1,223,936
Messages
6,175,508
Members
452,650
Latest member
Tinfish

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