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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
tonyclifton
Helper III
Helper III

Add aggregated rows in Query Editor?

Hello community,

is it possible to add aggregated rows for all existing columns in a table within the query editor?

In below image I want to aggregate based on YearMonth and Company so that a new "company" ("A+B") is created that holds the sum of all existing companies for all columns.

2020-04-24 14_15_26-Mappe2 - Excel.png

 

The reason I need to do this is because I have a budget table with the company name "A+B" and its values.

In order to compare actual and budget I need this company in my actual table.

 

If there are other options with DAX or so please feel free to share.

Thank you.

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

So, you could create a second query, reference your first query. Filter to just A and B. Create a column = "A+B". Now do a Group By to group by Date and your new column, appropriate aggregations.  Now add an Append step and Append your original query.

 

DAX would be similar using SUMMARIZE/GROUPBY and UNION.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

4 REPLIES 4
v-lid-msft
Community Support
Community Support

Hi @tonyclifton ,

 

We can create a calculated table to meet your requirement:

 

Actual =
UNION (
    'Table',
    SELECTCOLUMNS (
        ADDCOLUMNS (
            CROSSJOIN (
                DISTINCT ( 'Table'[YearMonth] ),
                FILTER (
                    CROSSJOIN (
                        SELECTCOLUMNS ( DISTINCT ( 'Table'[Company] ), "C1", [Company] ),
                        SELECTCOLUMNS ( DISTINCT ( 'Table'[Company] ), "C2", [Company] )
                    ),
                    [C1] < [C2]
                )
            ),
            "Value1",
            VAR co1 = [C1]
            VAR co2 = [C2]
            VAR ym = [YearMonth]
            RETURN
                CALCULATE (
                    SUM ( 'Table'[Value1] ),
                    'Table'[Company] IN { co1, co2 },
                    'Table'[YearMonth] = ym
                ),
            "Value2",
            VAR co1 = [C1]
            VAR co2 = [C2]
            VAR ym = [YearMonth]
            RETURN
                CALCULATE (
                    SUM ( 'Table'[Value2] ),
                    'Table'[Company] IN { co1, co2 },
                    'Table'[YearMonth] = ym
                ),
            "Value3",
            VAR co1 = [C1]
            VAR co2 = [C2]
            VAR ym = [YearMonth]
            RETURN
                CALCULATE (
                    SUM ( 'Table'[Value3] ),
                    'Table'[Company] IN { co1, co2 },
                    'Table'[YearMonth] = ym
                )
        ),
        "YearMonth", [YearMonth],
        "Company", [C1] & "+" & [C2],
        "Value1", [Value1],
        "Value2", [Value2],
        "Value3", [Value3]
    )
)

 

11.jpg


By the way, PBIX file as attached.


Best regards,

 

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks for all the suggestions. I went one step "back" and unpivoted the table in order to create (reference) a new table like @Mariusz suggested so that I could create a sum column since there are many Value columns that I don't want to type in a query.
I think this works fine now.

Mariusz
Community Champion
Community Champion

Hi @tonyclifton 

 

Sure if you want to use your approach, please provide a sample table that can be copied.

 

You can always look at the DAX solution for Budget patterns below
https://www.daxpatterns.com/budget-patterns/  

But I imagine that you would need an extra column that creates a hierarchy, like

column  extra column
A AB
B AB
C C

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

Greg_Deckler
Super User
Super User

So, you could create a second query, reference your first query. Filter to just A and B. Create a column = "A+B". Now do a Group By to group by Date and your new column, appropriate aggregations.  Now add an Append step and Append your original query.

 

DAX would be similar using SUMMARIZE/GROUPBY and UNION.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.