英文:
Taking multiple values in one cell and making it so a pivot table can be run
问题
最终目标: 制作数据透视表
问题: 数据输入混乱,需要进行处理以便进行数据透视表操作。
所有数值都在一个单元格中(见下文)
需要将其更改为每行一个数值,以便进行数据透视表操作(见下文)
我可以将这些数值拆分到不同的列中(见下文)
但目前的情况并没有真正帮助。
英文:
Final goal: Make Pivot Tables
Problem: Data coming in poorly and needs to be worked on in order to do pivots.
All the values come in one cell (see below)
Need to change it to one value per row in order to run pivots (see below)
I can split the values into different columns (see below)
But, as it is, it doesn't really help.
答案1
得分: 3
使用公式,您可以尝试以下方法之一:
• 单元格中使用的公式 <kbd>D1</kbd>
=REDUCE(A1:B1,SEQUENCE(ROWS(A2:B6)),
LAMBDA(x,y,VSTACK(x,IFERROR(HSTACK(INDEX(A2:B6,y,1),
TEXTSPLIT(INDEX(A2:B6,y,2),,",")),INDEX(A2:B6,y,1)))))
不使用 <kbd>LAMBDA( )</kbd> 的备用方法:
• 单元格中使用的公式 <kbd>D1</kbd>
=LET(
α,A1:A6,
ß,B1:B6,
σ,TEXTBEFORE(TEXTAFTER(","&ß,",",SEQUENCE(,MAX(LEN(ß))))&",",","),
φ,HSTACK(TOCOL(IFNA(α,SEQUENCE(,COLUMNS(σ)))),TOCOL(σ)),
FILTER(φ,ISTEXT(INDEX(φ,,2))))
然而,使用 <kbd>POWER QUERY</kbd> 将是最简单的方法。
要执行上述任务,请按照以下步骤进行操作:
- 首先将源范围转换为表格,并相应地命名它,例如,我已将其命名为
CStbl
- 接下来,从 <kbd>数据</kbd> 选项卡打开一个空白查询 --> <kbd>获取和转换数据</kbd> --> <kbd>获取数据</kbd> --> <kbd>从其他源获取</kbd> --> <kbd>空白查询</kbd>
- 上述步骤会打开 Power Query 窗口,现在从 <kbd>主页</kbd> 选项卡 --> <kbd>高级编辑器</kbd> --> 并粘贴以下 <kbd>M-Code</kbd>,删除看到的任何内容,然后按 <kbd>完成</kbd>
let
Source = Excel.CurrentWorkbook(){[Name="CStbl"]}[Content],
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(Source, {{"Comma Separated Values", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Comma Separated Values")
in
#"Split Column by Delimiter"
- 最后,要导入回 Excel --> 点击 <kbd>关闭并加载</kbd> 或 <kbd>关闭并加载到</kbd> --> 点击第一个将创建一个包含所需输出的新工作表,而后者将提示一个窗口,询问您要将结果放在哪里 --> 由于您需要的最终结果是 <kbd>数据透视表</kbd>,您可以选择 <kbd>数据透视表</kbd>,或者您可以使用 <kbd>表格</kbd>,然后从 Excel 转换为 <kbd>数据透视表</kbd>,或者您可以点击 <kbd>仅创建连接</kbd> 以使用它。
英文:
Using formula, you can try something along the lines of:
• Formula used in cell <kbd>D1</kbd>
=REDUCE(A1:B1,SEQUENCE(ROWS(A2:B6)),
LAMBDA(x,y,VSTACK(x,IFERROR(HSTACK(INDEX(A2:B6,y,1),
TEXTSPLIT(INDEX(A2:B6,y,2),,",")),INDEX(A2:B6,y,1)))))
Alternative approach without using <kbd>LAMBDA( )</kbd>:
• Formula used in cell <kbd>D1</kbd>
=LET(
α,A1:A6,
ß,B1:B6,
σ,TEXTBEFORE(TEXTAFTER(","&ß,",",SEQUENCE(,MAX(LEN(ß))))&",",","),
φ,HSTACK(TOCOL(IFNA(α,SEQUENCE(,COLUMNS(σ)))),TOCOL(σ)),
FILTER(φ,ISTEXT(INDEX(φ,,2))))
However, using <kbd>POWER QUERY</kbd> will be the easiest approach.
To perform the above task, follow the steps:
- First convert the source range into a table and name it accordingly, for this example I have named it as
CStbl
- Next, open a blank query from <kbd>Data</kbd> Tab --> <kbd>Get & Transform Data</kbd> --> <kbd>Get Data</kbd> --> <kbd>From Other Sources</kbd> --> <kbd>Blank Query</kbd>
- The above lets the Power Query window opens, now from <kbd>Home</kbd> Tab --> <kbd>Advanced Editor</kbd> --> And paste the following <kbd>M-Code</kbd> by removing whatever you see, and press <kbd>Done</kbd>
let
Source = Excel.CurrentWorkbook(){[Name="CStbl"]}[Content],
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(Source, {{"Comma Separated Values", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Comma Separated Values")
in
#"Split Column by Delimiter"
- Lastly, to import it back to Excel --> Click on <kbd>Close & Load</kbd> or <kbd>Close & Load To</kbd> --> The first one which clicked shall create a new sheet with the required output while the latter will prompt a window asking you where to place the result --> Since you need the end result as <kbd>Pivot Table</kbd>, you can select the <kbd>Pivot Table</kbd> or you can use <kbd>Table</kbd> and then transform to <kbd>Pivot Table</kbd> from Excel or you can click on <kbd>Only Create Connection</kbd> to use it.
答案2
得分: 2
这个公式如描述的那样,虽然 Power Query 是最好的方法,但以下公式也能完成任务:
=LET(a, A2:A6,
b, B2:B6,
c, DROP(REDUCE("",SEQUENCE(ROWS(a)),
LAMBDA(x,y,
VSTACK(x,
INDEX(a,y)&","&TEXTSPLIT(INDEX(b,y),",",))))),
HSTACK(TEXTBEFORE(c,","),
TEXTAFTER(c,",")))
英文:
While power query does the trick best, this formula does as described:
=LET(a, A2:A6,
b, B2:B6,
c, DROP(REDUCE("",SEQUENCE(ROWS(a)),
LAMBDA(x,y,
VSTACK(x,
INDEX(a,y)&","&TEXTSPLIT(INDEX(b,y),,",")))),
1),
HSTACK(TEXTBEFORE(c,","),
TEXTAFTER(c,",")))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论