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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
melo4
Regular Visitor

Split column by two criteria

Hi,

 

I am taking German equity data from investing.com and the price, change and change% are aggregated in one column. How do I split these three numbers based on two criteria (+ and -) so that it keeps the either positive or negative change format? I attached a photo below.

 

As you can see I managed to split them with formulas, but the problem is that it's treating it as text so I can't make other calculations based on this.

 

I'm guessing there must be a way to do this. Thanks for the help.

 

melo4_0-1709893571915.png

 

1 ACCEPTED SOLUTION

@melo4,

 

v1

let
    Source = Web.Page(Web.Contents("https://www.investing.com/equities/germany")),
    Data = Source{0}[Data],
    Transform = Table.TransformColumns(Data, List.Transform(Table.ColumnNames(Data), (colName)=>
        {colName, each Text.Combine(List.Transform(Text.Split(_, "#(lf)"), (x)=> Text.Trim(x, {" ", "#(lf)", "#(cr)"}) ), " "), type text})),
    #"Added Custom" = Table.AddColumn(Transform, "Custom", each Text.Combine(List.Transform(Splitter.SplitTextByCharacterTransition((x)=> not List.Contains({"+", "-"}, x), (y)=> List.Contains({"+", "-", "#(lf)", "#(cr)"}, y))([Column2]), (x)=> Text.Trim(x, {" ", "#(lf)", "#(cr)"})), "|"), type text),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Added Custom", "Custom", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"Price", "Change", "Change %"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Price", type number}, {"Change", type number}, {"Change %", Percentage.Type}}, "en-US")
in
    #"Changed Type"

 

v2

let
    Source = Web.Page(Web.Contents("https://www.investing.com/equities/germany")),
    Data = Source{0}[Data],
    Transform = Table.TransformColumns(Data, List.Transform(Table.ColumnNames(Data), (colName)=>
        {colName, each Text.Combine(List.Transform(Text.Split(_, "#(lf)"), (x)=> Text.Trim(x, {" ", "#(lf)", "#(cr)"}) ), " "), type text})),
    Ad_List = Table.AddColumn(Transform, "List", each Splitter.SplitTextByCharacterTransition((x)=> not List.Contains({"+", "-"}, x), (y)=> List.Contains({"+", "-"}, y))([Column2]), type list),
    Ad_Table = Table.AddColumn(Ad_List, "Table", each 
       List.Accumulate(
           List.Zip({ {0..List.Count([List])-1}, {"Price", "Change", "Change %"} }),
           #table(type table[Column1=text], {{[Column1]}}),
           (s,c)=> Table.AddColumn(s, c{1}, (x)=> Number.From([List]{c{0}}, "en-US"), if c{1} = "Change %" then Percentage.Type else type number)
       ), type table),
    Combined = Table.Combine(Ad_Table[Table])
in
    Combined

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

9 REPLIES 9
dufoq3
Super User
Super User

Hi @melo4, there are many ways.

 

Result:

dufoq3_0-1710004209611.png

 

 

v1

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrHQMzI1M9a11DM1M9I20DMzNVOK1YlWMjYCClhaaBuageR1DfWMjYESsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each Text.Combine(Splitter.SplitTextByCharacterTransition((x)=> not List.Contains({"+", "-"}, x), (y)=> List.Contains({"+", "-"}, y))([Column1]), "|"), type text),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Added Custom", "Custom", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"Price", "Change", "Change %"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Price", type number}, {"Change", type number}, {"Change %", Percentage.Type}}, "en-US")
in
    #"Changed Type"

 

v2

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrHQMzI1M9a11DM1M9I20DMzNVOK1YlWMjYCClhaaBuageR1DfWMjYESsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    Ad_List = Table.AddColumn(Source, "List", each Splitter.SplitTextByCharacterTransition((x)=> not List.Contains({"+", "-"}, x), (y)=> List.Contains({"+", "-"}, y))([Column1]), type list),
    Ad_Table = Table.AddColumn(Ad_List, "Table", each 
       List.Accumulate(
           List.Zip({ {0..List.Count([List])-1}, {"Price", "Change", "Change %"} }),
           #table(type table[Column1=text], {{[Column1]}}),
           (s,c)=> Table.AddColumn(s, c{1}, (x)=> Number.From([List]{c{0}}, "en-US"), if c{1} = "Change %" then Percentage.Type else type number)
       ), type table),
    Combined = Table.Combine(Ad_Table[Table])
in
    Combined

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Thank you both for the help. @dufoq3 @AlienSx 

 

If I copy all three versions of your solutions in a blank query it loads the first two lines of the table, so I see that your approaches could work. However, once I change the source in the code I get all kind of errors.

 

My source in the original query is:

= Web.Page(Web.Contents("https://www.investing.com/equities/germany"))

 

I need it to be an active link to investing.com to avoid manually downloading and refreshing the data I need.

How would the code change in this instance? 

 

melo4_0-1710233871035.png

 

Thanks again! Much appreciated. 🙂

@melo4,

 

v1

let
    Source = Web.Page(Web.Contents("https://www.investing.com/equities/germany")),
    Data = Source{0}[Data],
    Transform = Table.TransformColumns(Data, List.Transform(Table.ColumnNames(Data), (colName)=>
        {colName, each Text.Combine(List.Transform(Text.Split(_, "#(lf)"), (x)=> Text.Trim(x, {" ", "#(lf)", "#(cr)"}) ), " "), type text})),
    #"Added Custom" = Table.AddColumn(Transform, "Custom", each Text.Combine(List.Transform(Splitter.SplitTextByCharacterTransition((x)=> not List.Contains({"+", "-"}, x), (y)=> List.Contains({"+", "-", "#(lf)", "#(cr)"}, y))([Column2]), (x)=> Text.Trim(x, {" ", "#(lf)", "#(cr)"})), "|"), type text),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Added Custom", "Custom", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"Price", "Change", "Change %"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Price", type number}, {"Change", type number}, {"Change %", Percentage.Type}}, "en-US")
in
    #"Changed Type"

 

v2

let
    Source = Web.Page(Web.Contents("https://www.investing.com/equities/germany")),
    Data = Source{0}[Data],
    Transform = Table.TransformColumns(Data, List.Transform(Table.ColumnNames(Data), (colName)=>
        {colName, each Text.Combine(List.Transform(Text.Split(_, "#(lf)"), (x)=> Text.Trim(x, {" ", "#(lf)", "#(cr)"}) ), " "), type text})),
    Ad_List = Table.AddColumn(Transform, "List", each Splitter.SplitTextByCharacterTransition((x)=> not List.Contains({"+", "-"}, x), (y)=> List.Contains({"+", "-"}, y))([Column2]), type list),
    Ad_Table = Table.AddColumn(Ad_List, "Table", each 
       List.Accumulate(
           List.Zip({ {0..List.Count([List])-1}, {"Price", "Change", "Change %"} }),
           #table(type table[Column1=text], {{[Column1]}}),
           (s,c)=> Table.AddColumn(s, c{1}, (x)=> Number.From([List]{c{0}}, "en-US"), if c{1} = "Change %" then Percentage.Type else type number)
       ), type table),
    Combined = Table.Combine(Ad_Table[Table])
in
    Combined

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Thank you! It works 🙂

 

You're welcome. My old query based on your sample data would not work with real data, because in real data there are multiple lines in same row (like if you use alt+enter in excel).


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

v3

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnIMdlPSUTKx0DMyNTPWtdQzNTPSNtAzMzVTitWJVnJMyUxJLAYqMDYCylhaaBuagRTqGuoZGwNVxAIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    to_list = Table.ToList(
        Source, 
        (x) => 
            {x{0}} & 
                List.Transform(
                    Splitter.SplitTextByCharacterTransition({"0".."9"}, {"+", "-"})(x{1}),
                    Expression.Evaluate
                )
    ),
    to_tbl = Table.FromList(
        to_list, (x) => x, 
        type table 
            [Column1 = text, 
            price = number, 
            change = number, 
            #"change%" = Percentage.Type]
    )
in
    to_tbl

 

HotChilli
Super User
Super User

"but the problem is that it's treating it as text" so you have successfully split the column already?  It looks like you just need to change the data type of the column (which is straightforward right-click the column header->Change Type or use the Home tab : ->Data type:)

I've split the values outside of the query with combination of "LEFT" / "RIGHT" / "LEN" / "SEARCH" formulas.

I tried changing the data type in the query just like you said, but I get an error in the cells.

 

I also tried chaning the cell types to numbers and percentages where I have the formula, but still i always get #VALUE! errors.

Hi @melo4 ,
Then this is an Excel problem, not a Power Query problem.

Try wrapping your Excel formula used to split the values with  NUMBERVALUE like this: 
=NUMBERVALUE(yourExcelFormulaForTheSplit,".")

 

If this post answered your question, please mark it as a solution to help other users find useful content.
Kudos are another nice way to acknowledge those who tried to help you.

J. Payeras
Mallorca, Spain

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors