Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Hi all,
I have a set of data and want to transform to a new data model in PowerBI. Below is the screenshot of data transformation.
There are two difficult parts for me:
1. combine all the customers and use comma to separate them.
2. Add a type for each shop.
Number of customer number=1, "Bad"
1<Number of customer number<=3, "Normal"
Nubmber of customer number>=5, "Good"
Thanks in advance.
Proud to be a Super User!
Solved! Go to Solution.
Hi @ryan_mayu
@AkhilAshok's solution looks really good. If you want to do it with DAX instead, you could create a table based in yours with something like this:
Table = ADDCOLUMNS ( ADDCOLUMNS ( SUMMARIZECOLUMNS ( Table1[Shop]; Table1[Month] ); "Customer"; CALCULATE ( CONCATENATEX ( VALUES ( Table1[Customer] ); Table1[Customer]; ", " ) ); "Amount"; CALCULATE ( SUMX ( Table1; Table1[Amount] ) ) ); "Type"; VAR NumCustomers = LEN ( [Customer] ) - LEN ( SUBSTITUTE ( [Customer]; ","; "" ) ) + 1 RETURN SWITCH ( NumCustomers; 1; "Bad"; 2; "Normal"; 3; "Normal"; "Good" ) )
Where it is assumed that number of customers >=4 is of type 'Good'
You can do this using DAX or Power Query. In DAX it will be as simple as using CONCATNATEX funciton in measure for string aggregation. You can do this approach if you want the aggregation to happen dynamically at Visual level.
But, if you want to already bring the data in the format you showed, then you can use Power Query Editor:
1. Go to Transform -> Group By
2. Click Advanced:
Add Shop, Month as group by columns
Add CustomerCount with Operation Count Rows, TotalAmount with operation Sum of Amount & AllColumns with operation All Rows as aggregations
3. After this you get 5 columns.
4. Go to Add Columns -> Custom Column, and give = [AllColumns][Customer]
5. Click on expand button on top right corner of the new Custom column, and select Extract Values. Select coma as the seperator.
6. Again go to Add Columns -> Custom Column, and give = if [CustomerCount]=1 then "Bad" else if [CustomerCount] <=3 then "Normal" else if [CustomerCount] >=5 then "Good" else "NA"
7. Now you can remove unnecessary columns.
Hope this helps.
Thanks for your reply. Although someone has provided the solution by using DAX in this subject, i still like to learn your solution.
I tried your solution again today. Even if I chose count distinct for customer. I still got duplciated customer names after I expand the column. The results of count and distinct count are the same.
Any thoughts on that? Appreciate your help. Thanks in advance
Proud to be a Super User!
If you have duplicate customer while grouping, then in Step 4, you could do like this:
= List.Distinct([AllColumns][Customer])
Many thanks for your help on this. Reaaly appreciate that.
Thanks and BR
Ryan
Proud to be a Super User!
@AkhilAshok Thanks for your reply.
I followed you steps and got stucked at step 4. Appreciate that if you can provide me some screenshots to make it easier to understand. Thanks in advance.
Proud to be a Super User!
Can you tell me what is the code you gave why creating a new column as per Step 4? Also, please click on the error and show the error details you get.
Hi @ryan_mayu
@AkhilAshok's solution looks really good. If you want to do it with DAX instead, you could create a table based in yours with something like this:
Table = ADDCOLUMNS ( ADDCOLUMNS ( SUMMARIZECOLUMNS ( Table1[Shop]; Table1[Month] ); "Customer"; CALCULATE ( CONCATENATEX ( VALUES ( Table1[Customer] ); Table1[Customer]; ", " ) ); "Amount"; CALCULATE ( SUMX ( Table1; Table1[Amount] ) ) ); "Type"; VAR NumCustomers = LEN ( [Customer] ) - LEN ( SUBSTITUTE ( [Customer]; ","; "" ) ) + 1 RETURN SWITCH ( NumCustomers; 1; "Bad"; 2; "Normal"; 3; "Normal"; "Good" ) )
Where it is assumed that number of customers >=4 is of type 'Good'
Thanks for your help. That was really helpful. However, I have another question .Appreciate that you can help me as well.
Is it possible to only keep Shop A's info in the new table?
Thanks and BR
Ryan
Proud to be a Super User!
Yeah, you can just add the filtering step to what we had before:
Table = FILTER ( ADDCOLUMNS ( ADDCOLUMNS ( SUMMARIZECOLUMNS ( Table1[Shop]; Table1[Month] ); "Customer"; CALCULATE ( CONCATENATEX ( VALUES ( Table1[Customer] ); Table1[Customer]; ". " ) ); "Amount"; CALCULATE ( SUMX ( Table1; Table1[Amount] ) ) ); "Type"; VAR NumCustomers = LEN ( [Customer] ) - LEN ( SUBSTITUTE ( [Customer]; "."; "" ) ) + 1 RETURN SWITCH ( NumCustomers; 1; "Bad"; 2; "Normal"; 3; "Normal"; "Good" ) ); [Shop] = "A" )
Or another option to get the same result is adding the filtering within SUMMARIZECOLUMNS instead of at the end:
Table = ADDCOLUMNS ( ADDCOLUMNS ( SUMMARIZECOLUMNS ( Table1[Shop]; Table1[Month]; FILTER ( ALL ( Table1[Month] ); Table1[Month] = "A" ) ); "Customer"; CALCULATE ( CONCATENATEX ( VALUES ( Table1[Customer] ); Table1[Customer]; ". " ) ); "Amount"; CALCULATE ( SUMX ( Table1; Table1[Amount] ) ) ); "Type"; VAR NumCustomers = LEN ( [Customer] ) - LEN ( SUBSTITUTE ( [Customer]; "."; "" ) ) + 1 RETURN SWITCH ( NumCustomers; 1; "Bad"; 2; "Normal"; 3; "Normal"; "Good" ) )
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
113 | |
97 | |
85 | |
70 | |
61 |
User | Count |
---|---|
151 | |
121 | |
104 | |
87 | |
67 |