Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

How to combine column 1 and column 2 such that all the elements in column 2 move below column 1?

Hi everyone, 

 

I want to combine variable, variable1 and variable2 into one column and value, value1 and value2 into another column which means the total number of rows will be 36. So at the end of the table it will only have 2 columns which is variable and value. May I know how should I do? Thank you.

  • Anonymous 

    Create a function from the code above (named  "function_")

    (inputT_ as table)=> 
    let
        colNames_ = Table.ColumnNames(inputT_),
        colNum_ = Table.ColumnCount(inputT_),
        auxList_ = List.Numbers(0,colNum_/2),
        auxList2_ = List.Transform(auxList_, each Table.RenameColumns(Table.SelectColumns(inputT_,List.Range(colNames_, _*2,2)),{{colNames_{_*2}, "Value"}, {colNames_{_*2+1}, "Variable"}})), 
        TFinal_ = Table.Combine(auxList2_)
    in
        TFinal_

     Then you can apply it on the query you showed above:

    let
     Source = Web.Page(Web.Contents("https://finviz.com/quote.ashx?t=FB&ty=c&p=d&b=1")),
     res_ = function_ (Source{0}[Data])
    in
     res_

     

    Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

    Cheers 

     

  • Hello Anonymous 

     

    did any of the answer suit your requirements? If yes, please mark them as solution please

     

    BR

     

    Jimmy

11 Replies

  • AlB's avatar
    AlB
    Icon for Community Champion rankCommunity Champion

    Anonymous 

    Create a function from the code above (named  "function_")

    (inputT_ as table)=> 
    let
        colNames_ = Table.ColumnNames(inputT_),
        colNum_ = Table.ColumnCount(inputT_),
        auxList_ = List.Numbers(0,colNum_/2),
        auxList2_ = List.Transform(auxList_, each Table.RenameColumns(Table.SelectColumns(inputT_,List.Range(colNames_, _*2,2)),{{colNames_{_*2}, "Value"}, {colNames_{_*2+1}, "Variable"}})), 
        TFinal_ = Table.Combine(auxList2_)
    in
        TFinal_

     Then you can apply it on the query you showed above:

    let
     Source = Web.Page(Web.Contents("https://finviz.com/quote.ashx?t=FB&ty=c&p=d&b=1")),
     res_ = function_ (Source{0}[Data])
    in
     res_

     

    Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

    Cheers 

     

  • AlB's avatar
    AlB
    Icon for Community Champion rankCommunity Champion

    Hi Anonymous 

    This  can probably be done with pivot/unpivot.

    Can you share the original table in text format (instead of a screen pic) so that it can be copied

    And show also the expected result, what the final table should look like

     

    Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

    Cheers 

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      VariableValueVariable1Value1Variable2Value2Column7Column8Column9Column10Column11Column12

      IndexS&P 500P/E32.82EPS (ttm)8.43Insider Own0.63%Shs Outstand2.85BPerf Week1.46%
      Market Cap790.44BForward P/E26.39EPS next Y10.49Insider Trans-9.62%Shs Float2.39BPerf Month-0.37%
      Income25.28BPEG1.98EPS next Q3.19Inst Own79.90%Short Float0.97%Perf Quarter5.73%
      Sales78.98BP/S10.01EPS this Y-15.10%Inst Trans-0.25%Short Ratio1.25Perf Half Y21.89%
      Book/sh41.31P/B6.7EPS next Y12.21%ROA18.10%Target Price321.15Perf Year35.40%
      Cash/sh19.48P/C14.21EPS next 5Y16.60%ROE23.30%52W Range137.10 - 304.67Perf YTD34.85%
      Dividend-P/FCF41.19EPS past 5Y42.20%ROI17.40%52W High-9.15%Beta1.19
      Dividend %-Quick Ratio5.5Sales past 5Y41.50%Gross Margin81.00%52W Low101.88%ATR7.17
      Employees56653Current Ratio5.5Sales Q/Q21.60%Oper. Margin36.40%RSI (14)52.02Volatility2.46% 2.71%
      OptionableYesDebt/Eq0EPS Q/Q28.10%Profit Margin32.00%Rel Volume0.88Prev Close277.00
      ShortableYesLT Debt/Eq0EarningsOct 29 AMCPayout0.00%Avg Volume18.64MPrice276.78
      Recom1.80SMA20-0.0006SMA500.31%SMA20014.74%Volume16,361,201Change-0.08%
      • Jimmy801's avatar
        Jimmy801
        Icon for Community Champion rankCommunity Champion

        Hello Anonymous 

         

        you can try thi dynamically approach.

        First exctract all value and variable columns and transform this list into the real values of you columns. Then combine this two list into one and create with this data your final table. Here an example

        let
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bZRrb+owDIb/SjQJaZNYSNL7R8ZgQxriqjNNO/uQQQYVXctJwy7//jhOCj0XCUFsXD+287rPzxc/pM7la6EuunAsju7XuXjj4y2naJyi5Qwapz2cPy/d54txuVFfYCx/HhkT8YxEjIE56w3hOxA0tXmGsyW5NOb9Cs4pDW2WcVnnG6XJ9LMEi9E46Ngsu5pMj6Y2styACY9HNzab0m/kUak9nDkN4w6iJ1LvlSEDeQB3kjEahjZ4VOlPqTfElSBiGmS+hFJ9GfJkc0Bs1ipipWVZg32d0Vg0dYyKShosIshORUyq0uxsJKNB0vETWFfvdq4ioiLFyOEdFpqlbfDcDoRyzzW+8ySjGXPISpsTlNEs6TTQ+VFqozSYEU0CR13KQtmSkxQwCO0tXWeMe6rZ5TW2e80jyhGC4FO3jIrojF5Ik1dYt4ga8r0s3jCF4DTNHPmmqva92g4h5DTgiLYFxDT5Z86CCm4Ji2nfmqkvYyX1Fm5upvO1Qplwyk/MJyVtq0FEQ+aIA1nvHJFnNEyROLBWCOnbzAihMY2Zg6IAAhqgGYlH6LHcWiIPEiiFXJOAhTROTujVrSWHIDtHvs0/QCCoxWvEjgYj1zhvRHWQtQeH0K0Hjy0jwQYc+D7f7pzAOI78RhmJs4Y8bRDpeNT8mK/3pzuJqB0P3nkbyGmEhDtd1TWBfdjmVlMpp+xEfqg+URdwgan19VcLKxvKEwQP3w9F9a1QS1EcR3Y3B0etVWn+S5/35k4ObsbTg9L0TA5i3/NiOSaXPLzCIijDl0lVQL4iN9+4VLDFRNCEu0FPD4Aq/YvqCau5Va+mN/xll8GP2rMbFc109ZabFlz4theqIEA74lYy6BuD1QcZFFWNm5okEOr2yGr/D+7DivyFlrrMy639b7o2RGSkP7Hym8nv6uiW1XH7H9szF8QehxMkO5WLBDYkRehCwSsDrz+1gOWkL5hbSMZY7DwRw8wB7g9GMCf5JOy4aXpO3A1i3hW49oOdF7hNBdf98vIb", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t, #"(blank).3" = _t, #"(blank).4" = _t, #"(blank).5" = _t, #"(blank).6" = _t, #"(blank).7" = _t, #"(blank).8" = _t, #"(blank).9" = _t, #"(blank).10" = _t, #"(blank).11" = _t]),
            #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}, {"(blank).2", type text}, {"(blank).3", type text}, {"(blank).4", type text}, {"(blank).5", type text}, {"(blank).6", type text}, {"(blank).7", type text}, {"(blank).8", type text}, {"(blank).9", type text}, {"(blank).10", type text}, {"(blank).11", type text}}),
            #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
            #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Variable", type text}, {"Value", type text}, {"Variable1", type text}, {"Value1", Int64.Type}, {"Variable2", type text}, {"Value2", type text}, {"Variable3", type text}, {"Value3", Percentage.Type}, {"", type text}, {"_1", type text}, {"_2", type text}, {"_3", type text}}),
            GetVariables = List.Combine(List.Transform(List.Select(Table.ColumnNames(#"Changed Type1"), each Text.Contains(_, "Variable")), each Table.Column(#"Changed Type1", _))),
            GetValues =List.Combine(List.Transform(List.Select(Table.ColumnNames(#"Changed Type1"), each Text.Contains(_, "Value")), each Table.Column(#"Changed Type1", _))),
            CreateFinalTable= Table.FromColumns
            (
                {
                    GetVariables,
                    GetValues
                },
                {
                    "Variables",
                    "Values"
                }
            )
        in
            CreateFinalTable

        Copy paste this code to the advanced editor in a new blank query to see how the solution works.

        If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
        Kudoes are nice too

        Have fun

        Jimmy

  • Jimmy801's avatar
    Jimmy801
    Icon for Community Champion rankCommunity Champion

    Hello Anonymous 

     

    did any of the answer suit your requirements? If yes, please mark them as solution please

     

    BR

     

    Jimmy