简体   繁体   English

Power BI Desktop中的转换

[英]Transformation in Power BI Desktop

A table with dice throws like this one: 一张带有骰子的桌子会像这样抛出:

Day   Throw 1   Throw  2
1     1         3
2     3         2
3     6         6

Must be transformed into this table: 必须转换成此表:

Day   1   2   3   4   5   6
1     1       1
2         1   1
3                         2

So instead of noting the individual throws per day, I want all possible results in columns so I can read the count of occurrences of that number. 因此,我不想记录每天的单个投掷次数,而是希望将所有可能的结果放在列中,以便我可以读取该次数的出现次数。 (The order of the throws can be disregarded.) (可以忽略抛出的顺序。)

I know I can do this in an SQL Server ETL process, but I was wondering if I can get this done in Power BI Desktop? 我知道我可以在SQL Server ETL流程中做到这一点,但是我想知道是否可以在Power BI Desktop中做到这一点?

The purpose of this transformation is to get a performant way of reporting the number of occurrences of certain dice throws in certain day ranges. 这种转换的目的是获得一种报告在特定日期范围内发生某些掷骰子的次数的有效方式。 Maybe it is possible to get that reported without such transformation? 也许不进行这种转换就可以得到报告?

Step 1 第1步

In addition to the table you have above, you'll want a table that represents each of the possible die throws. 除了上面的表格之外,您还需要一个表格,该表格代表每种可能的掷骰子。 (This might not be necessary if numbers 1-6 are already represented in your results, but since your example doesn't reflect that, I'm assuming that can't be assumed) (如果结果中已经显示了1-6,这可能不是必需的,但是由于您的示例没有反映出这一点,因此我假设这不能假定)

Die
---
1
2
3
4
5
6

Step 2 第2步

Load both tables into Power BI Desktop. 将两个表都加载到Power BI Desktop中。 The Die table can be loaded without modification. 模具表可以不加修改地加载。 The results table above (which I'm going to call "Throws") will need to be unpivoted. 上面的结果表(我将其称为“ Throws”)将需要取消设置。 Click to "Edit Query" and highlight the Throw 1 & Throw 2 columns, and click Transform > Unpivot Columns > Unpivot Columns . 单击“编辑查询”并突出显示“投掷1”和“投掷2”列,然后单击“ 转换”>“取消透视列”>“取消透视列” Rename the unpivoted columns. 重命名未透视的列。 I ended up with: 我最终得到了:

Day | Throw   | Result
--------------------
1   | Throw 1 | 1
1   | Throw 2 | 3
2   | Throw 1 | 3
... | ....... | ...

Step 3 第三步

Once the 2 tables are loaded into your data model, relate them to each other (the die column to the result column). 将这两个表加载到数据模型后,将它们彼此关联(die列与result列)。

Step 4 第四步

Create a new measure: 创建一个新的度量:

Count of Result = COUNT(Throws[Result])

assuming your table is called Throws, and the result column is called Result 假设您的表称为Throws,结果列称为Result

Step 5 第5步

On your canvas, choose the matrix visual. 在画布上,选择矩阵视觉。 Use Count of Result as your value, Die as your column, and Day for your Rows. “结果计数”用作值,将“ 模具”用作列,将“ 天”用作行。 Then, on the right-hand side, click on the little down-arrow beside Die and select Show items with no data to get numbers 4 & 5 from your example to show up, even though they weren't thrown. 然后,在右侧,单击Die旁边的小向下箭头,然后选择“ 显示无数据的项目”以从示例中获取数字4和5来显示,即使它们没有被抛出。 You should now get a result that mirrors your desired result (with totals). 现在,您应该得到一个反映您期望的结果(总计)的结果。


Hope this helps! 希望这可以帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM