Power Query: Allowed values

Mer333

Board Regular
Joined
Jun 28, 2014
Messages
54
Hi all!

I have a nice idea in my mind. Does anyone know how to check whether value or text in a column contains in an Allowed values list?

To be more specific.

Imagine that we have a column like:
Tanks
Cars
Planes
Bikes
Apples

<tbody>
</tbody>

And I know that Allowed values for that column only:
Tanks
Cars
Planes
Bikes


<tbody>
</tbody>

So I need a function which would check every value in every row and return true or false into new custom column.

Like
Tankstrue
Carstrue
Planestrue
Bikestrue
Applesfalse

<tbody>
</tbody>
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Here's how I did this.

I created two tables:

Table one is your table with all values (including Apple). I added a header called Values, and called the table Values. I then pulled it into Power Query, and just chose to Close and Load, creating a connection only. (So it shows in the Workbook Queries pane as "load is disabled"

Table two is your table with the acceptable values (no Apple). I added a header called Allowed, and called the table Allowed. I then pulled that one into Power Query as well, again choosing to Cloe and Load, creating a connection only. (I now have two queries showing in the Workbook Queries pane showing "load is disabled".

Next I went to the Power Query tab and clicked "Merge" to create a new query.

I selected Values first, Allowed in the second box. I then selected each column in their respective windows and clicked OK.
Once in Power Query, I expanded the second column... notice how the last line reads "null"
I added a new column, and used the following formula: if [NewColumn.Allowed] = null then "False" else "True"

Hope that helps,
 
Upvote 0
Hhhm. I suppose it should be legit. I will try it in a real example which is more complex than two columns. Also to be honest, I'll need to compare a lot of columns at the same time but not the whole table... Not sure yet that it will work...

I also asked Chris Webb about it and he sent me a link to his post on this matter - https://cwebbbi.wordpress.com/2014/01/27/comparing-columns-in-power-query/

But it's still not an answer. Like I wrote to him I have a huge table with o lot of columns. And for some of them I have a lists of Allowed values. I need to check all rows of those specific columns and see where they don’t match my allowed values. I’m thinking about writing a few functions with lists of allowed values but not sure yet how to do it.

I'll create more specific example:
column1column2column3column4column5column6
monkey1girlsky3Pro
elephant2girl___s ky6Newbie
dino1girlstar4Pro

<tbody>
</tbody>

And a list of allowed values would be:
column1column3column4column6
monkeygirlskyPro
elephantboystarNewbie

<tbody>
</tbody>

So the result ideally should looks like:
column1column2column3column4column5column6CheckingProblem
monkey1girlsky3Protrue
elephant2girl___s ky6Newbiefalsecolumn3, column4
dino1girlstar4Profalsecolumn1

<tbody>
</tbody>
 
Upvote 0
As requested, here's an Excel workbook that does this using a function: http://1drv.ms/1BKWAWE

Here's the code for the entire query:

Code:
let
    //Load input table to check
    InputTable = Excel.CurrentWorkbook(){[Name="Input"]}[Content],
    //Load validation table to check against
    ValidationTable = Excel.CurrentWorkbook(){[Name="Validation"]}[Content],
    //Define a function to check the input table row by row
    GetDifferences = (CurrentRow) =>
     let
      //Remove any fields from the current row that aren't in the validation table
      RemoveFields = Record.SelectFields(CurrentRow, Table.ColumnNames(ValidationTable), 1),
      //Get a list of field names
      CurrentRowFields = Record.FieldNames(RemoveFields),
      //Get a list of field values
      CurrentRowValues = Record.FieldValues(RemoveFields),
      //Find the number of fields
      NumberOfFields = List.Count(CurrentRowFields),
      //Get a list of numbers from 0 to NumberOfFields-1
      ListOfNumbers = List.Numbers(0,NumberOfFields-1),
      //Find the names of the fields that contain values that aren't in the equivalent column in the validation table
      GetDifferences = List.Transform(ListOfNumbers, each if List.Contains(Table.Column(ValidationTable, CurrentRowFields{_}), CurrentRowValues{_}) then null else CurrentRowFields{_}),
      RemoveNulls = List.RemoveNulls(GetDifferences)
     in
      RemoveNulls,
    //Add a calculated column to call the GetDifferences function
    ListDifferences = Table.AddColumn(InputTable, "ListDifferences", each GetDifferences(_)),
    //Add a calculated column to check if the ListDifferences list is empty
    Checking = Table.AddColumn(ListDifferences, "Checking", each List.IsEmpty(
[ListDifferences])),
    //Declare a function to combine text by comma
    CombineWithComma = Combiner.CombineTextByDelimiter(", "),
    //Get text containing all the non-matching column names
    Problem = Table.AddColumn(Checking, "Problem", each CombineWithComma(
[ListDifferences])),
    //Remove the ListDifferences column
    RemoveList = Table.RemoveColumns(Problem,{"ListDifferences"})
in
    RemoveList
 
Upvote 0
Chris, it's simply amazing!!! I would never do it myself... Brilliantly!!! Thank you so much!
 
Upvote 0
Sorry Chris... it doesn't work properly :(
Put i.e "Yes" to G5 (Source table, Sheet1) and refresh Query.
Try my method if you want (post #8 this topic)

Regards :)
 
Upvote 0
Hi Bill! ;) Thank you for another good solution! It really proves how many smart people there are among us!

Though Chris's function seems better because you are able to use it even against 100 columns.

Guys, any ideas how to check columns not separately but row by row?

I mean if I have a validation table where each row represents allowed values, not column.
 
Upvote 0

Forum statistics

Threads
1,215,223
Messages
6,123,715
Members
449,118
Latest member
MichealRed

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