在 Excel 中使用 3 种分隔符分隔数据 - 空格、连字符和 =>

问题描述 投票:0回答:2

我最近收到一个Excel文件,我正在努力将佣金栏分开,以便我以后的工作更轻松。 我有 3 种不同类型的百分比分隔符 - 空格、连字符和 =>。

这是我的样本数据:

供应商 日期 佣金
德科斯 2023 年 1 月 1 日 intre 4-6 百万 $ - 2% 6-10 百万 $ - 3% >10 百万 $ - 5%
吉格尔 2023 年 5 月 7 日 intre 3-5 百万美元 => 3% 5-8 百万美元 => 5% >8 百万美元 => 7%
弗洛里斯 2023 年 4 月 4 日 内部 0.5-2 百万美元 1% 2-3 百万美元 2% 3-5 百万美元 3.5% 5-6 百万美元 4.5% >6 百万美元 6%

我尝试使用 Power Query 使用 textafter 函数、texsplit 函数,但我无法在所需输出中获取数据,如您所见:

供应商 日期 佣金 百分比
德科斯 2023 年 1 月 1 日 4-6百万美元 2%
德科斯 2023 年 1 月 1 日 6-1000 万美元 3%
德科斯 2023 年 1 月 1 日 >1000万美元 5%
吉格尔 2023 年 5 月 7 日 3-5百万美元 3%
吉格尔 2023 年 5 月 7 日 5-8百万美元 5%
吉格尔 2023 年 5 月 7 日 >800万美元 7%
弗洛里斯 2023 年 4 月 4 日 0-2百万美元 1%
弗洛里斯 2023 年 4 月 4 日 2-3百万美元 2%
弗洛里斯 2023 年 4 月 4 日 3-5百万美元 3.5%
弗洛里斯 2023 年 4 月 4 日 5-6百万美元 4.5%
弗洛里斯 2023 年 4 月 4 日 >600万美元 6%

您能帮忙解决使用 Office 365 或 Power Query 中的公式的任何解决方案吗?

提前谢谢您

excel excel-formula powerquery m
2个回答
0
投票

我确信有更干净的方法,但这是一个解决方案。

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XY/dDoIwDEZfpVncHWU/XcEbn2RyYXCSJSgJ6Ps7AkPhrv3WntN5L2730LbDNIlCGGWU1ZZSGV/vMYDDCp6xhxMgWAkVGr31JOH60ZrCX8ZSNIUXXexCnyCs6j2PkNfZy7I7UxjPh5Az+vhQL/xHP4xxPtgptxfoktGuS0aCRVqbdP1PTiXP2vw3V27CHFVJ1HwB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [supplier = _t, date = _t, commision = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"supplier", type text}, {"date", type date}, {"commision", type text}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type"," - ","|",Replacer.ReplaceText,{"commision"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value"," => ","|",Replacer.ReplaceText,{"commision"}),
    #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1","$ ","$|",Replacer.ReplaceText,{"commision"}),
    #"Replaced Value3" = Table.ReplaceValue(#"Replaced Value2","%","%~",Replacer.ReplaceText,{"commision"}),
    #"Replaced Value4" = Table.ReplaceValue(#"Replaced Value3","intre","",Replacer.ReplaceText,{"commision"}),
    #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Replaced Value4", {{"commision", Splitter.SplitTextByDelimiter("~", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "commision"),
    #"Filtered Rows" = Table.SelectRows(#"Split Column by Delimiter", each [commision] <> ""),
    #"Split Column by Delimiter1" = Table.SplitColumn(#"Filtered Rows", "commision", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"commision", "percent"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"commision", type text}, {"percent", Percentage.Type}})
in
    #"Changed Type1"

0
投票

不优雅,但很有效:

=LET(res,
 WRAPROWS(
     TOCOL(
       TEXTSPLIT(
           TEXTAFTER(
               SUBSTITUTE(
                  SUBSTITUTE(
                     SUBSTITUTE(
                        SUBSTITUTE(
                           SUBSTITUTE(TRIM(C2:C4),"% ","%|"&A2:A4&"|"&B2:B4&"|"),
                           "$ -","$"),
                        "$ =>","$"),
                     "$","|"),
                  "intre","|"&A2:A4&"|"&B2:B4&"|"),
              "|",SEQUENCE(,99)),
          "|"),
        2),
    4),
IFERROR(--res,res))

© www.soinside.com 2019 - 2024. All rights reserved.