Excel Power查询。未分明的年份只是为了在每行中创建多个措施

发布于 2025-02-11 10:22:56 字数 2548 浏览 1 评论 0原文

我正在尝试将Excel提供的一些数据归一化。数据由多个维列组成,然后是随着时间的流逝的几个度量列。不幸的是,数据带有单个“度量/年”标识符,这意味着,如果有10年的数据和4个措施,将有40列。

我无法选择特定的列来启动,因为列的数量会随着时间的推移而变化,我想完全自动化它。

简化的数据样本看起来像这样(在此示例中仅显示了3年的2个测量方法 - 但在不断增加的年中可能有5个措施)。

国家类别产品QTY_2018QTY_2019QTY_2020value_2018value_2019value_2020
法国水果苹果102030112233
法国水果橙色4050 6040 60445566
德国蔬菜蔬菜7080 9090 90778899

...我想取得的

目标...我想取得的目标数量价值
法国水果苹果20181011
法国水果苹果20192022
法国水果苹果20203033
法国水果20194044
法国水果20205066
法国水果橙色20186077
德国蔬菜胡萝卜德国702018
素食胡萝卜55
德国蔬菜胡萝卜20209099

到目前为止,我选择了所有非量化列,然后应用了“ undivot ofervot of voth ofther列”,然后创建2个自定义列以获取测量名称(QTY或在本示例中)和年度。这解决了各种程度的测量列的问题,但这只会让我走得太远。

我现在有看起来像这个

国家类别产品年度的数据水果
法国法国Apple2018QTY10
水果量表Apple201811

,等等...

注意:

  • 测量标签列将始终“ susuroName_yyyy”
  • 名称列表是有限的(也许是4或5)因此,更新此信息以支持更多的措施名称,如果添加的措施可以很好,因为这很少见。每年的年数将增加,但由于我希望最终用户能够根据其更新的表内容(上面的示例数据)刷新查询,因此必须在查询中处理不同的时期。

如果可以在Datamodel中完成此操作,我也很乐意使用。

到目前为止,我可能会以错误的方式解决这个错误的方法,但是我的力量查询知识是基本的,因此请感谢任何帮助。

I'm trying to normalise some data that is supplied in Excel. The data is made up of a number of dimension columns followed by several measure columns over time. Unfortunately the data comes in with a single "Measure/Year" identifier which means that if there are 10 years of data and 4 measures, there will be 40 measure columns.

I can't select specific columns to unpivot as the number of columns will change over time and I want to automate this completely.

A simplified sample of data looks like this (just showing 2 measures over 3 years in this example - but potentially 5 measures over an ever increasing number of years).

CountryCategoryProductQTY_2018QTY_2019QTY_2020Value_2018Value_2019Value_2020
FranceFruitApple102030112233
FranceFruitOrange405060445566
GermanyVegCarrot708090778899

What I would like to achieve is...

CountryCategoryProductYearQTYValue
FranceFruitApple20181011
FranceFruitApple20192022
FranceFruitApple20203033
FranceFruitOrange20184044
FranceFruitOrange20195055
FranceFruitOrange20206066
GermanyVegCarrot20187077
GermanyVegCarrot20198088
GermanyVegCarrot20209099

So far I have selected all the non-measure columns and then applied a transform "Unpivot other columns", and then creating 2 custom columns to get the measure name (Qty or Value in this example) and the year. This gets around the problem of the varying number of measure columns but that only gets me so far.

I now have data that looks like this

CountryCategoryProductYearMeasureAmount
FranceFruitApple2018QTY10
FranceFruitApple2018Value11

and so on...

Notes:

  • The measure label column will always 'measurename_YYYY'
  • The list of measure names is finite (4 or 5 maybe) so updating this to support more measure names if any are added will be fine as this will be rare. The number of years will increase each year but as I want end users to be able to refresh the query based on the contents of a sheet they update (the sample data above) then the varying periods must be handled in the query.

If this can be done in the datamodel I'm happy to go with that too.

I maybe going about this the wrong way with my attempts so far but my Power Query knowledge is pretty basic so any help would be gratefully received.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此生挚爱伱 2025-02-18 10:22:56

你快到了。只需在您的“测量”列上旋转即可完成输出:

Unpivoted = Table.UnpivotOtherColumns(Source, {"Country", "Category", "Product"}, "Attribute", "Value"),
#"Split Column" = Table.SplitColumn(Unpivoted, "Attribute", Splitter.SplitTextByEachDelimiter({"_"}, QuoteStyle.Csv, false), {"Measure", "Year"}),
#"Pivoted Column" = Table.Pivot(#"Split Column", List.Distinct(#"Split Column"[Measure]), "Measure", "Value")

You're nearly there. Just Pivot on your "Measure" column, to complete the output:

Unpivoted = Table.UnpivotOtherColumns(Source, {"Country", "Category", "Product"}, "Attribute", "Value"),
#"Split Column" = Table.SplitColumn(Unpivoted, "Attribute", Splitter.SplitTextByEachDelimiter({"_"}, QuoteStyle.Csv, false), {"Measure", "Year"}),
#"Pivoted Column" = Table.Pivot(#"Split Column", List.Distinct(#"Split Column"[Measure]), "Measure", "Value")
独木成林 2025-02-18 10:22:56

您应该只能在新的量度列上重复使用即可获得所需的结果。

You should be able to just repivot on the new Measure column to get your desired result now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文