我的目标是在一定条件下将单元格的值拖动到另一列中的相应行。
我有下表:
第1栏 | 第2栏 |
---|---|
35 | 空 |
27欧元 | 空 |
13 | 空 |
如果 Column1 中的单元格包含“€”,则应清空该单元格并将值带到 Column2。
输出应该是:
第1栏 | 第2栏 |
---|---|
35 | 空 |
空 | 27欧元 |
13 | 空 |
条件非常简单,但我不熟悉查询编辑器的逻辑以及如何表达输出。
(我知道这可以在 Excel 或 VBA 上轻松完成,但我想知道如何直接使用 Query 执行这些类型的任务)。
您的电量查询代码应如下所示
let
Source = [YourSourceHere], // Replace this with your actual source step
AddedCustom2 = Table.AddColumn(Source, "New Column2", each if Text.Contains([Column1], "€") then null else [Column1]),
AddedCustom1 = Table.AddColumn(AddedCustom2, "New Column1", each if Text.Contains([Column1], "€") then [Column1] else null),
RemovedOriginals = Table.RemoveColumns(AddedCustom1, {"Column1", "Column2"}),
RenamedColumns = Table.RenameColumns(RemovedOriginals, {{"New Column1", "Column1"}, {"New Column2", "Column2"}})
in
RenamedColumns
这是另一种不添加列的方法,使用
Table.TransformRows
函数:
let
//Change next line to reflect actual data source
Source = Excel.CurrentWorkbook(){[Name="Table4"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type any}}),
#"Move €" = Table.FromRecords(
Table.TransformRows(#"Changed Type", (r)=>
Record.TransformFields(r, {
{"Column1", each if Text.EndsWith(_,"€") then null else _},
{"Column2", each if Text.EndsWith(r[Column1],"€") then r[Column1] else _}
}
)
)
)
in
#"Move €"
假设源单元格是文本,则预先附加一个 |然后拆分该角色
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
AddSplit = Table.TransformColumns(Source,{{"Column1", each if Text.Contains(_,"€") then "|"&_ else _, type text}}),
#"Split Column by Delimiter" = Table.SplitColumn(AddSplit, "Column1", Splitter.SplitTextByEachDelimiter({"|"}, QuoteStyle.Csv, false), {"Column1.1", "Column1.2"})
in #"Split Column by Delimiter"