Forum Discussion

Snagalapur's avatar
Snagalapur
Icon for Helper IV rankHelper IV
1 year ago
Solved

Add new column into data table with space using existing column

Hi All,

 

Please suggest how to add Column having space (customised based on need) based on data in existing column, so that when i use this New column into Martix table it should retain format Ex: below.

 

 

Thank you.

  • You can create a calculated column. However, you'll need to ensure the Text Wrap for your values is off

     

3 Replies

  • Here is an approach in Power Query that uses non-breaking spaces (unicode 160). Non-breaking spaces won't get collapsed like regular spaces do in certain contexts (like when displayed in row headers). I added some inputs (LevelWeight, LevelLookup) that let you assign/update spacing a little more easily and transparently.

     

    You can see how this works if you paste into Advanced Editor in Power Query. You can copy the relevant steps over to your own query, just be sure to update for column name differences. It's possible to do something very similar with DAX in a calculated column if needed.

     

    let
        Source = Table.FromColumns( 
            {{"Type 1","Type 2","Type 3","Type 4","Type 5","Type 6"}}, 
            type table [Accounts = text]
        ),
        LevelWeight = 3, 
        LevelLookup = [ Type 1 = 0, Type 2 = 2, Type 3 = 1, Type 4 = 1, Type 5 = 0, Type 6 = 0 ],
        AddExpectedResult = 
        Table.AddColumn(
            Source, 
            "Expected result", 
            each Text.Repeat( 
                Character.FromNumber( 160 ) , 
                Record.Field( LevelLookup, [Accounts] ) * LevelWeight 
            ) & [Accounts], 
            type text
        )
    in
        AddExpectedResult

     

    Table in Power Query:

     

    Put into a matrix and you can see that spacing is not collapsed.

     

     

    Note that to have [Expected result] ignore the spacing when sorting, you will need to set the column's 'Sort by column' setting to [Accounts].

     

  • Syk's avatar
    Syk
    Icon for Resident Rockstar rankResident Rockstar

    You can create a calculated column. However, you'll need to ensure the Text Wrap for your values is off