Search code examples
powerquerym

What is the best way to set all cells in a table to the same value?


What is the best way to pass all cells in a table the same value?

For instance, if I start with this table:

enter image description here

How might I most easily change it to this:

enter image description here

Or to this:

enter image description here

I would like the solution to work for any size table, without having to hard-code the columns' names. So I'm pretty sure the solution will include Table.ColumnNames.


Solution

  • My suggestion would be to create a new table with the dimensions and table type of the original table. The table type includes column names and column data types (and any keys if these would be present).

    Note: this won't work if the original table has a primary key, e.g. after removing duplicates.

    let
        NewValue = "a",
        Source = Table1,
        NewTable = Table.FromColumns(List.Repeat({List.Repeat({NewValue},Table.RowCount(Source))},Table.ColumnCount(Source)), Value.Type(Source))
    in
        NewTable