Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Daurenbrun
Regular Visitor

add columns from data contained in 1 column (based on indentation)

Dear Community,

Need help to split one column data into several based on indentation in that first column.

The thing is I can do it in Excel by macro (indentlevel), however when uploaded into Power Query the indentation dissapears.

 Intention is to just create plain table to make further analysis.

 

Account    
Currency    
Clientcolumn to addcolumn to addcolumn to addcolumn to add
DocumentAccountCurrencyClientDocument
1210 1210   
    EUR 1210 EUR  
        XXXXXX 1210 EUR  XXXXXX 
              B1 1210 EUR  XXXXXX              B1
              B2 1210 EUR  XXXXXX              B2
              B3 1210 EUR  XXXXXX              B3
              B4 1210 EUR  XXXXXX              B4
1 ACCEPTED SOLUTION
v-stephen-msft
Community Support
Community Support

Hi @Daurenbrun ,

 

Here's my approach, and I'm trying to get as close to your expected outcome as possible.

Load your data into Power Query:

vstephenmsft_1-1725006039306.png


Add a Custom Column to Identify Indentation Levels:

Go to the “Add Column” tab and select “Custom Column”.
Use a formula to identify the indentation level. For example, if indentation is represented by spaces, you can count the number of leading spaces:
= Text.Length([Account]) - Text.Length(Text.TrimStart([Account]))
Create a New Column for Each Indentation Level:

Add a custom column for each level of indentation. For example, for level 1:
Repeat this for each level of indentation, adjusting the condition accordingly.
Fill Down the Values:

Select the new columns created for each indentation level.
Go to the “Transform” tab and select “Fill” -> “Down”.
Remove Unnecessary Columns:

Remove the original column and the “IndentationLevel” column if they are no longer needed.
Here's final result:

vstephenmsft_2-1725006227116.png

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wci4tKkrNS65UitUBcnIyU/NKwEyX/OTSXBjH0MjQAMxQAALX0CA4GwQiwABFCAKcDLEJGmETNMYmaKIUGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Account = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each Text.Length([Account]) - Text.Length(Text.TrimStart([Account]))),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each if [Custom] = 0 then [Account] else null),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Custom.2", each if [Custom] = 4 then [Account] else null),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Custom.3", each if [Custom] = 8 then [Account] else null),
    #"Added Custom4" = Table.AddColumn(#"Added Custom3", "Custom.4", each if [Custom] = 14 then [Account] else null),
    #"Filled Down" = Table.FillDown(#"Added Custom4",{"Custom.2", "Custom.3", "Custom.4", "Custom.1"}),
    #"Removed Columns" = Table.RemoveColumns(#"Filled Down",{"Custom"})
in
    #"Removed Columns"

 

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

7 REPLIES 7
v-stephen-msft
Community Support
Community Support

Hi @Daurenbrun ,

 

Here's my approach, and I'm trying to get as close to your expected outcome as possible.

Load your data into Power Query:

vstephenmsft_1-1725006039306.png


Add a Custom Column to Identify Indentation Levels:

Go to the “Add Column” tab and select “Custom Column”.
Use a formula to identify the indentation level. For example, if indentation is represented by spaces, you can count the number of leading spaces:
= Text.Length([Account]) - Text.Length(Text.TrimStart([Account]))
Create a New Column for Each Indentation Level:

Add a custom column for each level of indentation. For example, for level 1:
Repeat this for each level of indentation, adjusting the condition accordingly.
Fill Down the Values:

Select the new columns created for each indentation level.
Go to the “Transform” tab and select “Fill” -> “Down”.
Remove Unnecessary Columns:

Remove the original column and the “IndentationLevel” column if they are no longer needed.
Here's final result:

vstephenmsft_2-1725006227116.png

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wci4tKkrNS65UitUBcnIyU/NKwEyX/OTSXBjH0MjQAMxQAALX0CA4GwQiwABFCAKcDLEJGmETNMYmaKIUGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Account = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each Text.Length([Account]) - Text.Length(Text.TrimStart([Account]))),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each if [Custom] = 0 then [Account] else null),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Custom.2", each if [Custom] = 4 then [Account] else null),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Custom.3", each if [Custom] = 8 then [Account] else null),
    #"Added Custom4" = Table.AddColumn(#"Added Custom3", "Custom.4", each if [Custom] = 14 then [Account] else null),
    #"Filled Down" = Table.FillDown(#"Added Custom4",{"Custom.2", "Custom.3", "Custom.4", "Custom.1"}),
    #"Removed Columns" = Table.RemoveColumns(#"Filled Down",{"Custom"})
in
    #"Removed Columns"

 

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Hi, Stephen

 

Thank you

The problem is when I get original file from Excel to Query indentation dissappears, so we cannot use Text.Trimstart function. In original Excel file it is like in column "Account". But when it opened in Query the first letter in each raw starts from zero.

Can you post a sample (sanitized) raw file from "1C accounting program" ?

Account
Currency
Client
Document
1210 (Account)
 EUR
        XXXXXX Client1
B1 Doc1
B2 Doc2
B3 Doc 3
B4 Doc 4
lbendlin
Super User
Super User

Hopefully this is not how your actual source data looks like?  Where is it coming from?

No, my actual data loos like as first column - name is currency. The rest 4 column should be result

This is how 1C accouunting program, so not convinient.

By the way, I solved this by splitting the first column, so there left only 3-4 similar texts in the column.

Then I've used if else function to add new column (using above texts) and did "fill down".

It took more steps, but in Excel we can use a macros with indent.level function.

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

AugPowerBI_Carousel

Power BI Monthly Update - August 2024

Check out the August 2024 Power BI update to learn about new features.

August Carousel

Fabric Community Update - August 2024

Find out what's new and trending in the Fabric Community.