# Add row for every unique combination based on multiple columns

**URL:** <https://connect.superhuman.com/t/add-row-for-every-unique-combination-based-on-multiple-columns/45828>\
**Category:** Ask the Community\
**Created:** [January 16, 2024, 5:44pm UTC](https://connect.superhuman.com/t/add-row-for-every-unique-combination-based-on-multiple-columns/45828 "2024-01-16T17:44:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![candyspecs](https://avatars.discourse-cdn.com/v4/letter/c/ee59a6/32.png) [@candyspecs](https://connect.superhuman.com/u/candyspecs)\
**Post date:** [January 16, 2024, 5:44pm UTC](https://connect.superhuman.com/t/add-row-for-every-unique-combination-based-on-multiple-columns/45828/1 "2024-01-16T17:44:39Z")

</div>

Hi everyone!

I’d like to be able to add row for every unique values combination based on multiple columns.

I have Product table

Product  
Product 1  
Product 2

I have Releases table

Release  
2024  
2025

I have Features table

Feature Product Release  
Feature 1 Product 1 Product 2 2024  
Feature 2 Product 2 2024 2025

I’d like to be able to add rows for every unique combination available where feature can be associated to multiple products and releases. I’m getting stuck with a nested foreach and even using if condition is unable to match values. Any help is greatly appreciated. Thanks!

Result table:  
Feature Product Release  
Feature 1 Product 1 2024  
Feature 1 Product 2 2024  
Feature 2 Product 2 2024  
Feature 2 Product 2 2025

---

<div class="post-metadata">

**Author:** ![Paul\_Danyliuk](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/paul_danyliuk/32/27228_2.png) [@Paul\_Danyliuk](https://connect.superhuman.com/u/Paul_Danyliuk)\
**Post date:** [January 16, 2024, 10:40pm UTC](https://connect.superhuman.com/t/add-row-for-every-unique-combination-based-on-multiple-columns/45828/2 "2024-01-16T22:40:37Z")

</div>

Make a button with a formula like:

```auto
Product.ForEach(CurrentValue.WithName(P,
  Features.ForEach(CurrentValue.WithName(F,
    Release.ForEach(CurrentValue.WithName(R,
      Result.AddRow(
        Result.Product, P,
        Result.Feature, F,
        Result.Release, R
      )
    ))
  ))
))

```

You can then also improve it by omitting the combinations you already have, so that you can click it again when you add something, i.e. replace the `Result.AddRow(...)` part with

```auto
If(
  Result.Any(
    CurrentValue.Product = P
    AND CurrentValue.Feature = F
    AND CurrentValue.Release = R
  ),
  _Noop(), // do nothing
  // otherwise do the Result.AddRow(...)
)

```

P.S. In this code, `P`, `F`, `R` will be row references (not just text values) so columns in the result table should be single-select Relations to respective tables.

---

<div class="post-metadata">

**Author:** ![candyspecs](https://avatars.discourse-cdn.com/v4/letter/c/ee59a6/32.png) [@candyspecs](https://connect.superhuman.com/u/candyspecs)\
**Post date:** [January 17, 2024, 1:07pm UTC](https://connect.superhuman.com/t/add-row-for-every-unique-combination-based-on-multiple-columns/45828/3 "2024-01-17T13:07:32Z")

</div>

Thank you, @Paul_Danyliuk.

However, I’m getting a different result

 ![image](https://global.discourse-cdn.com/coda1/original/3X/b/4/b4e2dd17e2a8868da51c08d697b59945cdfccd5f.png)

It’s kind of tricky to have 3 nested loops but nested loops from look up tables may not work.  
I am looping Products, Releases column values within Features table only because they should match the row contents. Still trying to figure out as well.

Thanks!

---

<div class="post-metadata">

**Author:** ![Paul\_Danyliuk](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/paul_danyliuk/32/27228_2.png) [@Paul\_Danyliuk](https://connect.superhuman.com/u/Paul_Danyliuk)\
**Post date:** [January 17, 2024, 1:46pm UTC](https://connect.superhuman.com/t/add-row-for-every-unique-combination-based-on-multiple-columns/45828/4 "2024-01-17T13:46:09Z")

</div>

Nothing tricky there, it works if applied right.

Can you share the doc please? I’ll quickly fix it for you.  
[paul@codatricks.com](mailto:paul@codatricks.com)

---

<div class="post-metadata">

**Author:** ![candyspecs](https://avatars.discourse-cdn.com/v4/letter/c/ee59a6/32.png) [@candyspecs](https://connect.superhuman.com/u/candyspecs)\
**Post date:** [January 17, 2024, 2:44pm UTC](https://connect.superhuman.com/t/add-row-for-every-unique-combination-based-on-multiple-columns/45828/5 "2024-01-17T14:44:57Z")

</div>

> [@Paul\_Danyliuk](#):
>
> [paul@codatricks.com](mailto:paul@codatricks.com)

I’ve created a new doc and shared with you.  
Thank you very much!

---

<div class="post-metadata">

**Author:** ![Paul\_Danyliuk](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/paul_danyliuk/32/27228_2.png) [@Paul\_Danyliuk](https://connect.superhuman.com/u/Paul_Danyliuk)\
**Post date:** [January 17, 2024, 3:07pm UTC](https://connect.superhuman.com/t/add-row-for-every-unique-combination-based-on-multiple-columns/45828/6 "2024-01-17T15:07:58Z")

</div>

Yeah, I got your scenario. You didn’t just want all possible combinations from all tables — but only use the products and releases selected for that feature.

For that you just put `Features` on the outer loop, and replace `Products` and `Releases` respectively with `F.Products` and `F.Releases` (i.e. not all rows from the table Products, but only the selected ones for that `F` currentvalue feature).

Also I saw you wanted to include rows with empty selection if nothing was selected for either a product or a release — this can be achieved by telling Coda to use a list of one blank value if the original list is blank, i.e. instead of `F.Products.ForEach(...)` use `F.Products.IfBlank(List("")).ForEach(...)`

Here’s the full code:

```auto
Features.ForEach(CurrentValue.WithName(Feature,
  // For each product OF THAT FEATURE:
  Feature.Products.IfBlank(
    // If the list itself is blank (i.e. 0 products) fake up a list of one blank value
    List("")
  ).ForEach(CurrentValue.WithName(Product,
    // For each year OF THAT FEATURE
    Feature.Releases.IfBlank(
      // (or a blank item if no releases):
      List("")
    ).ForEach(CurrentValue.WithName(Release,
      // If there's already such combination, do nothing, otherwise add row
      If(
        Result.Any(
          Feature = Feature
          AND Product = Product
          AND Release = Release
        ),
        _Noop(),
        Result.AddRow(
          Result.Feature, Feature,
          Result.Product, Product,
          Result.Release, Release
        )
      )
    ))
  ))
))

```

There’s still one thing that can be improved. This will add rows like these when there’s no product or no release for a feature:  
 ![image](https://global.discourse-cdn.com/coda1/original/3X/d/7/d786ccc671f3ea4b09dc6edf1d5fabdfcace6698.png)  
but will not remove them once there is a product or a release. If you need that, let that be your own homework 😉

---

<div class="post-metadata">

**Author:** ![candyspecs](https://avatars.discourse-cdn.com/v4/letter/c/ee59a6/32.png) [@candyspecs](https://connect.superhuman.com/u/candyspecs)\
**Post date:** [January 17, 2024, 4:02pm UTC](https://connect.superhuman.com/t/add-row-for-every-unique-combination-based-on-multiple-columns/45828/7 "2024-01-17T16:02:10Z")

</div>

This is great, @Paul_Danyliuk! I just learned that I can continue to access from the outer loop. Thanks so much!

---

<div class="post-metadata">

**Author:** ![system](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/system/32/24484_2.png) [@system](https://connect.superhuman.com/u/system)\
**Post date:** [April 16, 2024, 4:03pm UTC](https://connect.superhuman.com/t/add-row-for-every-unique-combination-based-on-multiple-columns/45828/8 "2024-04-16T16:03:04Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
