简体   繁体   English

是否存在将数据从一个单元格转换为表格的公式?

[英]Is there a formula to transform data from one cell into a table?

I am using Amazon Mechanical Turk to transcribe receipt data. 我正在使用Amazon Mechanical Turk转录收据数据。 Amazon returns a CSV that is quite unreadable at first sight. 亚马逊返回的CSV乍一看是不可读的。 URL to CSV file: https://drive.google.com/file/d/1QR4cgdVrkYwRni3YM5Dc_umIKFGiX_0k/view?usp=sharing CSV文件的网址: https : //drive.google.com/file/d/1QR4cgdVrkYwRni3YM5Dc_umIKFGiX_0k/view?usp=sharing

But when you import it into excel a set the Delimiter to Comma it is at least readable. 但是,当您将其导入excel中时,至少可以将分隔符设置为逗号。 Here is a URL to the excel file(Please download it and open with excel this makes it a lot better): https://drive.google.com/file/d/1Noj4UUMd-p1iYKIWDgKURQUzCdhu5Ck1/view?usp=sharing 这是excel文件的网址(请下载并用excel打开,这样会更好): https : //drive.google.com/file/d/1Noj4UUMd-p1iYKIWDgKURQUzCdhu5Ck1/view?usp=sharing

But then Excel puts all the answers of the transcriber in one cell called "Answer.taskAnswers". 但是,然后Excel将转录器的所有答案都放在一个称为“ Answer.taskAnswers”的单元格中。

Desired outcome: The values of the transciber in an table like this (check this URL: https://i.ibb.co/vjf0t0c/Prefered-formatting-of-cell-Answer-task-Answers-2.png ) 期望的结果:像这样的表中的Transciber值(请检查以下URL: https ://i.ibb.co/vjf0t0c/Prefered-formatting-of-cell-Answer-task-Answers-2.png)

Possible solution 1: A way to format the CSV file to make it look something like the table from "desired outcome". 可能的解决方案1:一种格式化CSV文件的方式,使其看起来像“期望结果”中的表格。

Possible solution 2: A formula that generates another table (possibly on another sheet) of "Answer.taskAnswers" that looks like the table from "desired outcome". 可能的解决方案2:一个公式,该公式生成“ Answer.taskAnswers”的另一个表(可能在另一张纸上),该表看起来像是“所需结果”中的表。

Does anyone know a fix for this? 有人知道解决办法吗?

EDIT: M-code changed to allow for varying numbers of columns (products) in the csv JSON string 编辑: M代码已更改,以允许csv JSON字符串中的列(产品)数量不同

From the appearance of your output, I am guessing that you used Power Query (aka Get & Transform ) to input the data. 从输出的外观来看,我猜您是使用Power Query (又名Get & Transform )来输入数据。

If that is the case, you can edit the Query to obtain the output you are looking for. 在这种情况下,您可以编辑查询以获取所需的输出。 (If not, you can just use it anyway for the whole process). (如果没有,您仍然可以在整个过程中使用它)。

The column from which you want the output parsed is in JSON format, and PQ has a built-in parser. 您要从中解析输出的列为JSON格式,PQ具有内置的解析器。

I worked from your original CSV file you provided. 我使用的是您提供的原始CSV文件。

We delete the irrelevant columns and blank rows, parse the JSON string, and then rearrange the data. 我们删除不相关的列和空白行,解析JSON字符串,然后重新排列数据。

All of the steps except the custom column formula, can be done from the GUI. 除自定义列公式外的所有步骤都可以从GUI完成。

The custom column formula extracts the elements from the JSON string in the relevant column: =Json.Document([Answer.taskAnswers]) 自定义列公式从相关列中的JSON字符串中提取元素: =Json.Document([Answer.taskAnswers])

You can just paste the M-code into the Advanced Editor in PQ, and then examine the steps in the GUI to see what's going on. 您可以将M代码粘贴到PQ的“高级编辑器”中,然后检查GUI中的步骤以查看发生了什么。
You will also have to edit the Source line to reflect where you are actually getting the source data (and that can be a URL instead of a file) 您还必须编辑“ Source行,以反映您实际在何处获取源数据(并且可以是URL而不是文件)

M-Code M代码

let
    Source = Csv.Document(File.Contents("C:\Users\ron\Desktop\Stackoverflow data for question about cell formating (1).csv"),[Delimiter=",", Columns=31, Encoding=1252, QuoteStyle=QuoteStyle.None]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Removed Other Columns" = Table.SelectColumns(#"Promoted Headers",{"Answer.taskAnswers"}),
    #"Removed Blank Rows" = Table.SelectRows(#"Removed Other Columns", each not List.IsEmpty(List.RemoveMatchingItems(Record.FieldValues(_), {"", null}))),
    #"Added Custom" = Table.AddColumn(#"Removed Blank Rows", "strJSON", each Json.Document([Answer.taskAnswers])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Answer.taskAnswers"}),
    #"Expanded strJSON" = Table.ExpandListColumn(#"Removed Columns", "strJSON"),
    #"Expanded strJSON1" = Table.ExpandRecordColumn(#"Expanded strJSON", "strJSON", List.Union(List.Transform(#"Expanded strJSON"[strJSON], each Record.FieldNames(_)))),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Expanded strJSON1", {"purchaseTime", "purchaseDate", "storeName"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByEachDelimiter({"-"}, QuoteStyle.Csv, true), {"Attribute.1", "Attribute.2"}),
    #"Sorted Rows" = Table.Sort(#"Split Column by Delimiter",{{"Attribute.2", Order.Ascending}}),
    #"Pivoted Column" = Table.Pivot(#"Sorted Rows", List.Distinct(#"Sorted Rows"[Attribute.1]), "Attribute.1", "Value"),
    #"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Attribute.2"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns1",{"storeName", "purchaseDate", "purchaseTime", "product", "price", "weight", "quantity"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Reordered Columns",{{"purchaseDate", type date}, {"purchaseTime", type time}, {"price", Currency.Type}, {"quantity", Int64.Type}})
in
    #"Changed Type"

The original GUI generated M-code had this line which names the JSON columns specifically. 原始GUI生成的M代码具有此行,该行专门命名JSON列。 It would not adapt to changes in numbers of products. 它不会适应产品数量的变化。

#"Expanded strJSON1" = Table.ExpandRecordColumn(#"Expanded strJSON", "strJSON", {"price-1", "price-2", "price-3", "price-4", "price-5", "product-1", "product-2", "product-3", "product-4", "product-5", "purchaseDate", "purchaseTime", "quantity-1", "quantity-2", "quantity-3", "quantity-4", "quantity-5", "storeName", "weight-1", "weight-5", "weight-3"}, {"price-1", "price-2", "price-3", "price-4", "price-5", "product-1", "product-2", "product-3", "product-4", "product-5", "purchaseDate", "purchaseTime", "quantity-1", "quantity-2", "quantity-3", "quantity-4", "quantity-5", "storeName", "weight-1", "weight-5", "weight-3"}),

So I have modified that line in the M-Code above, so as to take care of that problem. 因此,我在上面的M代码中修改了该行,以解决该问题。

Output 产量

在此处输入图片说明

GUI Steps GUI步骤

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTML 表格到 Excel - 将数据转换为表格 - 将所有内容粘贴到一个单元格中 - HTML Table to Excel - Issue transform data into a table - Pasting all in one cell Excel单元格公式,用于计算一个表中的键,如果它们在另一个表中的值是特定条件 - Excel cell formula to count keys from one table, if their values in another table are a specific criteria 用于对另一个表中的新表中的数据进行排名的公式 - Formula to rank data in a new table from another one IF公式-EXCEL-从另一个单元格获取数据 - IF formula - EXCEL - obtain data from another cell 从另一个单元格中的一个数字减去一个单元格中的数字范围的公式 - Formula for subtracting number range in one cell from a number in another cell 在另一个单元格的同一单元格中添加更多一个公式 - Add More One Formula In same cell from another cell 如何通过使用Excel公式自动将单元格数据的最大值从一个选项卡复制到另一选项卡 - How to copy max value of a cell data from one tab to another tab automatically by using Excel formula EXCEL,VBA 公式,用于检查单元格中的数据是否是列表中的一项或多项 - EXCEL, VBA formula to check if data in cell is one or more item from the list 多个公式在一个单元格中 - Multiple formula In One Cell VLOOKUP 和转换公式中的表格 - VLOOKUP and Transform a table inside the formula
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM