简体   繁体   中英

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?

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

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)

Die
---
1
2
3
4
5
6

Step 2

Load both tables into 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. Click to "Edit Query" and highlight the Throw 1 & Throw 2 columns, and click Transform > Unpivot Columns > Unpivot Columns . 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).

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

Step 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. You should now get a result that mirrors your desired result (with totals).


Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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