Power Automate Desktop: Find One-to-Many Values Between Two Columns
The Problem
You have a table with two columns, A and B. You want to find every A value that is linked to more than one different B value.
| ✅ One-to-many | ❌ Not one-to-many |
|---|---|
| Apple → Red, Green | Banana → Yellow, Yellow (same value repeated) |
| Date → Brown (only one value) |
No scripts needed. Only built-in Power Automate Desktop (PAD) actions.
Sample Data
| A | B |
|---|---|
| Apple | Red |
| Apple | Green |
| Banana | Yellow |
| Banana | Yellow |
| Cherry | Red |
| Cherry | Black |
| Date | Brown |
| Fig | Purple |
| Fig | Green |
| Grape | Purple |
How It Works (3 Simple Steps)
| Step | What happens |
|---|---|
| 1 | Get the unique list of A values |
| 2 | For each A value, collect its unique B values |
| 3 | If there are more than 1 B values → add it to the result |
Variables Used
| Variable | Type | Purpose |
|---|---|---|
InputDT | Data table | Source data (A, B) |
DistinctAValues | List | Unique A values |
OneToManyResults | Data table | Final result |
CurrentRow | Data row | Row being read (Step 1) |
CurrentAValue | Text | A value being checked |
BValuesForThisA | List | Unique B values for current A |
DataRow | Data row | Row being read (Step 2) |
BValuesText | Text | B values joined as "Red, Green" |
Build the Flow
Step 1 — Create the input table
Variables → Create new data table → add columns A and B → enter the sample rows → name it InputDT.
In real projects, use Excel → Read from Excel worksheet instead.
Step 2 — Get unique A values
| Action | Setting |
|---|---|
| Create new list | Name: DistinctAValues |
| For each | Loop InputDT, item: CurrentRow |
| If | DistinctAValues Does not contain CurrentRow['A'] |
| Add item to list | Add CurrentRow['A'] to DistinctAValues |
Result: Apple, Banana, Cherry, Date, Fig, Grape
Step 3 — Create the result table
Variables → Create new data table → columns A Value and Matching B Values → name it OneToManyResults.
💡 The visual builder adds one empty row by default. Delete it in the builder (or add Clear data table right after) so your result doesn't start with a blank line.
Step 4 — Check each A value
| Action | Setting |
|---|---|
| For each | Loop DistinctAValues, item: CurrentAValue |
| Create new list | Name: BValuesForThisA (inside the loop, so it resets each time) |
| For each | Loop InputDT, item: DataRow |
| If | DataRow['A'] Equal to CurrentAValue |
| If | BValuesForThisA Does not contain DataRow['B'] |
| Add item to list | Add DataRow['B'] to BValuesForThisA |
| If | BValuesForThisA.Count Greater than 1 |
| Join text | Join BValuesForThisA with , → BValuesText |
| Insert row into data table | Add [CurrentAValue, BValuesText] to OneToManyResults |
Step 5 — Show the result
Message boxes → Display message → show OneToManyResults.
Output
| A Value | Matching B Values |
|---|---|
| Apple | Red, Green |
| Cherry | Red, Black |
| Fig | Purple, Green |
Banana, Date and Grape are skipped. ✔️
Quick Tips
| Tip | Why |
|---|---|
Create BValuesForThisA inside the loop | Otherwise old values carry over |
Use %CurrentRow[0]% if no headers | Column index starts at 0 |
| Good for small/medium tables | Nested loops slow down on thousands of rows |
Complete Code
Copy the code below and paste it directly into the PAD designer (Ctrl + V).
Variables.CreateNewDatatable InputTable: { ^['A', 'B'], [$'''Apple''', $'''Red'''], [$'''Apple''', $'''Green'''], [$'''Banana''', $'''Yellow'''], [$'''Banana''', $'''Yellow'''], [$'''Cherry''', $'''Red'''], [$'''Cherry''', $'''Black'''], [$'''Date''', $'''Brown'''], [$'''Fig''', $'''Purple'''], [$'''Fig''', $'''Green'''], [$'''Grape''', $'''Purple'''] } DataTable=> InputDT
Variables.CreateNewList List=> DistinctAValues
LOOP FOREACH CurrentRow IN InputDT
IF NotContains(DistinctAValues, CurrentRow['A'], False) THEN
Variables.AddItemToList Item: CurrentRow['A'] List: DistinctAValues
END
END
Variables.CreateNewDatatable InputTable: { ^['A Value', 'Matching B Values'], [$'''''', $''''''] } DataTable=> OneToManyResults
LOOP FOREACH CurrentAValue IN DistinctAValues
Variables.CreateNewList List=> BValuesForThisA
LOOP FOREACH DataRow IN InputDT
IF DataRow['A'] = CurrentAValue THEN
IF NotContains(BValuesForThisA, DataRow['B'], False) THEN
Variables.AddItemToList Item: DataRow['B'] List: BValuesForThisA
END
END
END
IF BValuesForThisA.Count > 1 THEN
Text.JoinText.JoinWithCustomDelimiter List: BValuesForThisA CustomDelimiter: $''', ''' Result=> BValuesText
Variables.AddRowToDataTable.AppendRowToDataTable DataTable: OneToManyResults RowToAdd: [CurrentAValue, BValuesText]
END
END
Display.ShowMessageDialog.ShowMessage Title: $'''One-to-Many Results''' Message: OneToManyResults Icon: Display.Icon.None Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: False ButtonPressed=> ButtonPressed
No comments:
Post a Comment