Forum Discussion

alexbalazsalex's avatar
8 years ago
Solved

Data Type by row

Dear all,   I would need your suport if possible, in case i have a table where every second row is a percentage... is there any way to change the data type to percentage ? so i would have a mixed c...
  • MarcelBeug's avatar
    MarcelBeug
    8 years ago

    My solution would require some typing as you need to provide formulas for each column.

     

    It looks like in your latest data, the first 3 columns shouildn't be converted ("Sub_BU", "Column1" and "Values").

     

    So my proposal would be a dynamic solution. With the code below, all numbers in rows 2, 4, 6, from the 4th column onwards, will be converted to textual percentages and all numbers in rows 1, 3, 5, etcetera, from the 4th column onwards, will be converted to text.

     

    So a prerequisite is that, after your filter step, the percentages are in the even row numbers, and the numbers in the uneven row numbers.

     

    Just replace your code with the code below.

     

    let
        Source = Excel.Workbook(File.Contents("C:\Users\60060109\Desktop\tutorial\OTD Support for slide up to Nov17.xlsx"), null, true),
        OTD_Table = Source{[Item="OTD",Kind="Table"]}[Data],
        #"Changed Type" = Table.TransformColumnTypes(OTD_Table,{{"Sub_BU", type text}, {"Column1", Int64.Type}, {"Values", type text}, {"Jul16", type number}, {"Aug16", type number}, {"Sep16", type number}, {"Oct16", type number}, {"Nov16", type number}, {"Dec16", type number}, {"Jan17", type number}, {"Feb17", type number}, {"Mar17", type number}, {"Apr17", type number}, {"May17", type number}, {"Jun17", type number}, {"Acc. 16/17", type number}, {"Jul17", type number}, {"Aug17", type number}, {"Sep17", type number}, {"Oct17", type number}, {"Nov17", type number}, {"Dec17", type any}, {"Jan18", type any}, {"Feb18", type any}, {"Mar18", type any}, {"Apr18", type any}, {"May18", type any}, {"Jun18", type any}, {"Acc. 17/18", type number}}),
        #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Sub_BU] <> "COPI")),
        TransformOperations = List.Transform(List.Skip(Table.ColumnNames(#"Filtered Rows"),3),each {_, each Number.ToText(_,"P1"), type text}),
        typeTransformations = List.Transform(TransformOperations, each {_{0}, _{2}}),
        #"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),
        #"Removed Alternate Rows" = Table.AlternateRows(#"Added Index",0,1,1),
        Percentages = Table.TransformColumns(#"Removed Alternate Rows", TransformOperations),
        #"Removed Alternate Rows1" = Table.AlternateRows(#"Added Index",1,1,1),
        Numbers = Table.TransformColumnTypes(#"Removed Alternate Rows1",typeTransformations),
        NumbersAndPercentages = Numbers & Percentages,
        #"Sorted Rows" = Table.Sort(NumbersAndPercentages,{{"Index", Order.Ascending}})
    in
        #"Sorted Rows"

     

    Should you have additional questions, then please share a link to a file with representative sample data.