Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
saipawar
Helper IV
Helper IV

How to Pivot and Group a table?

Hi, 

I have events data with metrics and year that I'd like to use to build views for comparitive study.

 

Here,'s the input and desired output in Power BI.

 

Input Data - 

pbi1.PNG

 

Desired Output - 

On the right side are the slicers that I'd like to use. Highlighted in green are the selected options - 

pbi2.PNG

 

Please feel free to make any recommendations on views as well!!

 

Thanks,

Sailee

1 ACCEPTED SOLUTION
dm-p
Super User
Super User

Hi @saipawar,

This can be done, but there's a little extra prep required, to give you one row per event per metric per year. I've mocked-up your data and we're going to edit the query, e.g.:

image.png

Now, select the Event Name and Metric Name columns, and then in the Transform Menu, expand the Unpivot Columns operation and then select Unpivot Other Columns, e.g.:

image.png

The reason we do this is that if additional years appear in your source data then Power Query will know to add them to the unpivot operation. After this we'll be left with a table that has the grain we want, e.g.:

image.png

(I've renamed the Value column to Year - I'd suggest you do this too).

Now we can Close & Apply and revisit our report. We can now create a matrix visual, with Metric Name on RowsEvent Name on Columns and Value in Values, e.g.:

image.png

We then just add slicers to the canvas for Event Name and Year, e.g.:

image.png

Getting it Exactly Right (as per your requirements)

Note that because everything is in the same table, your slicer won't show all values as you filter, so you might want to turn Event/Metric and Year into dimensions and relate them to your main 'fact' table (Events).

If you haven't dealt with these concepts before, this is essential reading

For this concept concept you can expand the query as follows:

  1. In the query editor, right-click the query and choose Duplicate, e.g.:
    image.png
  2. This will create a duplicate table. We're going to rename this Event (you can call it whatever you like but I prefer plurals for fact tables and singular for dimension tables).
  3. Right Click the Event Name column and select Remove Other Columns, e.g.:
    image.png
  4. This will leave only the Event Name column. We now want to remove the duplicates by right-clicking this column and selecting Remove Duplicates,. At this point, we're left with the unique values, e.g.:
    image.png
  5. Repeat steps 1-4 for the Metric Name and Year attributes so they have unique queries. Note that later you may wish to explore using a Date Table, but for now, just the Year will be fine.
  6. When done, you should have something like this:
    Metric QueryMetric Query
    Date QueryDate Query
  7. Now, we can Close & Apply again.

I've now got separate tables that can filter the Events table. My model now looks like this, with relationships:

image.png

Note that I've hidden the corresponding field names in the Events table, as they are used for joining and should not be used in your visuals.

We now replace the columns, rows and slicers to use the new tables and we get something like your desired output, e.g.:

image.png

I've tried to expose as much of the field list ont he right as possible to help illustrate the changes.

Hopefully this makes sense - I've uploaded this as a workbook if you want to explore further or see more into my workings.

Good luck!

Daniel


If my post solves your challenge, then please consider accepting as a solution to help other forum members find the answer more quickly 🙂





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!


On how to ask a technical question, if you really want an answer (courtesy of SQLBI)




View solution in original post

2 REPLIES 2
dm-p
Super User
Super User

Hi @saipawar,

This can be done, but there's a little extra prep required, to give you one row per event per metric per year. I've mocked-up your data and we're going to edit the query, e.g.:

image.png

Now, select the Event Name and Metric Name columns, and then in the Transform Menu, expand the Unpivot Columns operation and then select Unpivot Other Columns, e.g.:

image.png

The reason we do this is that if additional years appear in your source data then Power Query will know to add them to the unpivot operation. After this we'll be left with a table that has the grain we want, e.g.:

image.png

(I've renamed the Value column to Year - I'd suggest you do this too).

Now we can Close & Apply and revisit our report. We can now create a matrix visual, with Metric Name on RowsEvent Name on Columns and Value in Values, e.g.:

image.png

We then just add slicers to the canvas for Event Name and Year, e.g.:

image.png

Getting it Exactly Right (as per your requirements)

Note that because everything is in the same table, your slicer won't show all values as you filter, so you might want to turn Event/Metric and Year into dimensions and relate them to your main 'fact' table (Events).

If you haven't dealt with these concepts before, this is essential reading

For this concept concept you can expand the query as follows:

  1. In the query editor, right-click the query and choose Duplicate, e.g.:
    image.png
  2. This will create a duplicate table. We're going to rename this Event (you can call it whatever you like but I prefer plurals for fact tables and singular for dimension tables).
  3. Right Click the Event Name column and select Remove Other Columns, e.g.:
    image.png
  4. This will leave only the Event Name column. We now want to remove the duplicates by right-clicking this column and selecting Remove Duplicates,. At this point, we're left with the unique values, e.g.:
    image.png
  5. Repeat steps 1-4 for the Metric Name and Year attributes so they have unique queries. Note that later you may wish to explore using a Date Table, but for now, just the Year will be fine.
  6. When done, you should have something like this:
    Metric QueryMetric Query
    Date QueryDate Query
  7. Now, we can Close & Apply again.

I've now got separate tables that can filter the Events table. My model now looks like this, with relationships:

image.png

Note that I've hidden the corresponding field names in the Events table, as they are used for joining and should not be used in your visuals.

We now replace the columns, rows and slicers to use the new tables and we get something like your desired output, e.g.:

image.png

I've tried to expose as much of the field list ont he right as possible to help illustrate the changes.

Hopefully this makes sense - I've uploaded this as a workbook if you want to explore further or see more into my workings.

Good luck!

Daniel


If my post solves your challenge, then please consider accepting as a solution to help other forum members find the answer more quickly 🙂





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!


On how to ask a technical question, if you really want an answer (courtesy of SQLBI)




This is amazing!! Thanks @dm-p . You were super prompt with your response!!

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors