Forum Discussion

Mirna's avatar
Mirna
Frequent Visitor
6 years ago
Solved

Find and extract a value

Hi, I wonder if you could help me with the following challenge: I have 30 columns, called Bag Collection materials and quatities 1, 2, 3.....30. In each column I need to find and extract a value(s)...
  • Smauro's avatar
    6 years ago

    Hi Mirna,

     

    If your data comes always in the form of "0x tile, 0x timber, 0x brick, 0x plaster, 0x hebel, 0x mixed", then this should work as intented for any number of columns named [Bag Collection Materials And Quantities x].

        Columns = List.FindText(Table.ColumnNames(PreviousStep), "Bag Collection Materials And Quantities "),
        #"Replace timber" = Table.ReplaceValue(PreviousStep, " 0x timber,", "", Replacer.ReplaceText, Columns),
        #"Replace brick" = Table.ReplaceValue(#"Replace timber", " 0x brick,", "", Replacer.ReplaceText, Columns),
        #"Replace plaster" = Table.ReplaceValue(#"Replace brick", " 0x plaster,", "", Replacer.ReplaceText, Columns),
        #"Replace hebel" = Table.ReplaceValue(#"Replace plaster", " 0x hebel,", "", Replacer.ReplaceText, Columns),
        #"Replace mixed" = Table.ReplaceValue(#"Replace hebel", ", 0x mixed", "", Replacer.ReplaceText, Columns),
        #"Replace x" = Table.ReplaceValue(#"Replace mixed", "x ", " ", Replacer.ReplaceText, Columns),
        #"Replace tile" =
            let Remake = (BeforeTable as table, n as number) =>
                if n > 0 then 
                    let 
                        Substitute = Table.TransformColumns(BeforeTable, {{"Bag Collection Materials And Quantities " & Number.ToText(n), each if Text.StartsWith(_, "0 tile") then Text.AfterDelimiter(_, "0 tile, ") else _}}),
                        Rest = @Remake(Substitute, n-1)
                    in Rest
                else BeforeTable
            in Remake(#"Replace x", List.Count(Columns))
    in
        #"Replace tile"

    Where PreviousStep is, as it says, your previous step. Tell me if you'd like me to explain something.

    Cheers,
    Spyros