The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.
I have table in the below format which I want to transform into a shape where columns 3 and 4 are rows and the Amounts are summed. I want to achieve this using DAX only (not Power Query). Is this possible ? Year can be a filter and therefore number of columns need to be controlled by the slicer year range.
Can this be achieved in DAX query?
ID | Year | Category 1 Sales | Category 2 Sales |
1 | 2019 | 10 | 9 |
1 | 2019 | 12 | 3 |
2 | 2019 | 7 | 6 |
2 | 2020 | 6 | 9 |
2 | 2020 | 4 | 6 |
Format Expected:
ID | Measure | 2019 | 2020 |
1 | Category 1 Sales | 22 | blank or 0 |
1 | Category 2 Sales | 12 | blank or 0 |
2 | Category 1 Sales | 7 | 10 |
2 | Category 2 Sales | 6 | 15 |
Solved! Go to Solution.
Hi @Hoping
Try the linked video :
https://www.youtube.com/watch?v=9Xv8COs59tc
If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly
Hi @Hoping ,
Why not do it in Power Query? It will be real pain doing it in DAX and very manual at that. You will have to update your formula everytime a new Category column and Year is added.
Yes, you can achieve this transformation using DAX. However, please note that DAX is not as flexible as Power Query when it comes to reshaping data. Here’s how you can do it:
First, you need to create two separate tables for each category of sales. You can do this using the SUMMARIZE function in DAX:
Category1Sales = SUMMARIZE( FILTER(YourTable, YourTable[Year] >= MIN(YearSlicer[Year]) && YourTable[Year] <= MAX(YearSlicer[Year])), "ID", YourTable[ID], "Measure", "Category 1 Sales", "Year", YourTable[Year], "Sales", SUM(YourTable[Category 1 Sales]) ) Category2Sales = SUMMARIZE( FILTER(YourTable, YourTable[Year] >= MIN(YearSlicer[Year]) && YourTable[Year] <= MAX(YearSlicer[Year])), "ID", YourTable[ID], "Measure", "Category 2 Sales", "Year", YourTable[Year], "Sales", SUM(YourTable[Category 2 Sales]) )
Then, you can union these two tables together:
FinalTable = UNION(Category1Sales, Category2Sales)
This will give you a table with the columns “ID”, “Measure”, “Year”, and “Sales”. You can then use this table to create a matrix visual in Power BI, with “ID” and “Measure” on the rows, “Year” on the columns, and “Sales” in the values.
Please replace YourTable and YearSlicer with the actual names of your table and slicer. Also, ensure that your slicer is connected to a table that contains all the possible years, and that this table is related to your main table.
Remember, DAX operates on tables and columns, not on individual cells or rows. Therefore, transformations that require cell-by-cell operations are often more complex in DAX than i...1.
Yes, you can achieve this transformation using DAX. However, please note that DAX is not as flexible as Power Query when it comes to reshaping data. Here’s how you can do it:
First, you need to create two separate tables for each category of sales. You can do this using the SUMMARIZE function in DAX:
Category1Sales = SUMMARIZE( FILTER(YourTable, YourTable[Year] >= MIN(YearSlicer[Year]) && YourTable[Year] <= MAX(YearSlicer[Year])), "ID", YourTable[ID], "Measure", "Category 1 Sales", "Year", YourTable[Year], "Sales", SUM(YourTable[Category 1 Sales]) ) Category2Sales = SUMMARIZE( FILTER(YourTable, YourTable[Year] >= MIN(YearSlicer[Year]) && YourTable[Year] <= MAX(YearSlicer[Year])), "ID", YourTable[ID], "Measure", "Category 2 Sales", "Year", YourTable[Year], "Sales", SUM(YourTable[Category 2 Sales]) )
Then, you can union these two tables together:
FinalTable = UNION(Category1Sales, Category2Sales)
This will give you a table with the columns “ID”, “Measure”, “Year”, and “Sales”. You can then use this table to create a matrix visual in Power BI, with “ID” and “Measure” on the rows, “Year” on the columns, and “Sales” in the values.
Please replace YourTable and YearSlicer with the actual names of your table and slicer. Also, ensure that your slicer is connected to a table that contains all the possible years, and that this table is related to your main table.
Remember, DAX operates on tables and columns, not on individual cells or rows. Therefore, transformations that require cell-by-cell operations are often more complex in DAX than i...1.
Hi @Hoping ,
Why not do it in Power Query? It will be real pain doing it in DAX and very manual at that. You will have to update your formula everytime a new Category column and Year is added.
Hi @Hoping
Try the linked video :
https://www.youtube.com/watch?v=9Xv8COs59tc
If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly
User | Count |
---|---|
69 | |
65 | |
63 | |
55 | |
28 |
User | Count |
---|---|
112 | |
81 | |
65 | |
48 | |
42 |