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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Grouping by and separating by year

Hello,

I have the following table:

Order NoOrder YearCityStateCustomer TypeOrder PriorityProduct CategoryProfit MarginOrder Quantity
5014-12021MelbourneVICHome OfficeHighOffice Supplies2.0629
5016-12020MelbourneVICHome OfficeHighOffice Supplies1.8729
5018-12021SydneyNSWCorporateCriticalOffice Supplies1.342
5019-12021MelbourneVICHome OfficeHighTechnology45.976
5020-12020SydneyNSWConsumerCriticalOffice Supplies1.217
5023-12021MelbourneVICHome OfficeLowOffice Supplies1.5147
5024-12021SydneyNSWCorporateCriticalOffice Supplies63.5432
5025-12021SydneyNSWConsumerCriticalOffice Supplies0.5933
5027-12021SydneyNSWSmall BusinessNot SpecifiedOffice Supplies36.198
5029-12021SydneyNSWCorporateNot SpecifiedOffice Supplies3.2748
5031-12021MelbourneVICCorporateMediumOffice Supplies2.7518
5033-12021SydneyNSWCorporateMediumOffice Supplies36.193
5034-12021SydneyNSWCorporateHighOffice Supplies22.0850
5036-12021MelbourneVICCorporateCriticalOffice Supplies0.4350
5037-12021SydneyNSWHome OfficeCriticalOffice Supplies4.9216
5038-12021MelbourneVICCorporateLowOffice Supplies0.4338
5039-12020SydneyNSWCorporateMediumOffice Supplies2.6922
5040-12020SydneyNSWSmall BusinessLowOffice Supplies1.5848
5043-12020SydneyNSWSmall BusinessHighOffice Supplies1.1622

I need to group by city, state and customer type, calculating the aggregated sum of order quantity and profit margin. But also i need to differentiate by year, so my guess is that the fourth grouping has to be year. What I did was the following:

  • I created a table with this DAX formula:
Agg =
GROUPBY (
Data,
Data[City],
Data[State],
Data[Customer Type],
Data[Order Year],
"Revenue agg", SUMX ( CURRENTGROUP (), Data[Profit Margin] ),
"Quantity agg", SUMX ( CURRENTGROUP (), Data[Order Quantity] )
)

 

 

  • The formula creates this table:
Data_CityData_StateData_Customer TypeData_Order YearRevenue aggQuantity agg
MelbourneVICHome Office2021$49.5482
MelbourneVICHome Office2020$1.8729
SydneyNSWCorporate2021$126.38175
SydneyNSWConsumer2020$1.217
SydneyNSWConsumer2021$0.5933
SydneyNSWSmall Business2021$36.198
MelbourneVICCorporate2021$3.61106
SydneyNSWHome Office2021$4.9216
SydneyNSWCorporate2020$2.6922
SydneyNSWSmall Business2020$2.7470

 

The problem that I have is that I don't know how to get from the previous table, to the following desired output:

Data_CityData_StateData_Customer TypeRevenue agg 2020Quantity agg 2020Revenue agg 2021Quantity agg 2021
MelbourneVICHome Office1.872949.5482
SydneyNSWConsumer1.2170.5933
SydneyNSWCorporate2.6922126.38175
SydneyNSWSmall Business2.747036.198

 

  • The desired output has the revenue agg and quantity agg separated by year (in the same row). Also, the grouping by city, state and customer type has to be the same in each row.
  • Notice that there is no match in the three groupings if one year doesn't have the same data. This means that the following data from 2021 is not included in the desired output:

 

Data_CityData_StateData_Customer TypeData_Order YearRevenue aggQuantity agg
MelbourneVICCorporate20213.61106
SydneyNSWHome Office20214.9216

 

You could say that the desired output is some kind of merge between data of both years or something, but I'm not really sure how to proceed. Maybe there is an advanced option while grouping for separating by year but I'm also not sure. 

The desired output is needed for creating new columns that contains calculations between data of both years (given that they have the same groupings), which are alot easier if they are side by side in the same table.

 

Any suggestion would be deeply appreciated,

Thanks.

1 ACCEPTED SOLUTION
bcdobbs
Community Champion
Community Champion

I think you'd be better skipping the calculated table and setting up some measures which you can drop into a table. Have a look at this demo: Revenue Example 


There is a basic measure for both Quantity and Revenue and then versions that filter by year using CALCULATE. That could be made more dynamic.

Lastly there is a measure that checks which rows to include which is put on the visual level filter of the table and set to 1:

bcdobbs_0-1640203917165.png

 





Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!

View solution in original post

2 REPLIES 2
bcdobbs
Community Champion
Community Champion

I think you'd be better skipping the calculated table and setting up some measures which you can drop into a table. Have a look at this demo: Revenue Example 


There is a basic measure for both Quantity and Revenue and then versions that filter by year using CALCULATE. That could be made more dynamic.

Lastly there is a measure that checks which rows to include which is put on the visual level filter of the table and set to 1:

bcdobbs_0-1640203917165.png

 





Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!
Anonymous
Not applicable

Thanks @bcdobbs !! That was exactly what I needed.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.