Forum Discussion

rohit_goyal's avatar
rohit_goyal
New Member
11 months ago
Solved

Different Groupby Column Names in Summarize

Hello Everyone,

 

I was working with a dataset and wanted to use different column names for the grouped by Columns in summarize function. We have option to change the agreegated columns, but can we change the coulmn names of the groupped columns?

  • Hi rohit_goyal ,

     

    You are correct, that is a frequent requirement when creating summary tables in Power BI. In DAX, you can't directly rename the columns you are grouping by from within the SUMMARIZE function itself. The SUMMARIZE function is designed to perform the grouping, and it will always return those grouping columns with their original, fully qualified names like 'Table'[Column].

     

    To assign custom names to both your grouped and aggregated columns, the standard and most effective pattern is to wrap your SUMMARIZE logic inside the SELECTCOLUMNS function. This function is designed specifically to construct a new table by picking columns from an existing table (or a table expression like the one SUMMARIZE creates), allowing you to rename them in the process.

     

    Here is an example of how you would create a table that summarizes sales by product category, renaming the category column to "Product Category" and creating a new "Total Sales" column. The SUMMARIZE function handles the grouping, and SELECTCOLUMNS handles the final column naming and structure.

    NewSummarizedTable =
    SELECTCOLUMNS(
        SUMMARIZE(
            'Sales',
            'Product'[Category]
        ),
        "Product Category", 'Product'[Category],
        "Total Sales", SUM(Sales[Sales Amount])
    )

    In this formula, SUMMARIZE first creates a temporary table containing just the unique values from the 'Product'[Category] column. Then, SELECTCOLUMNS takes that temporary table as its first argument and builds the final table. It creates a column named "Product Category" by referencing the original group-by column and adds a second column named "Total Sales" by providing the aggregation expression. This pattern is very powerful for creating clean, custom tables for your visuals.

     

    Best regards,

2 Replies

  • Hi rohit_goyal ,

     

    You are correct, that is a frequent requirement when creating summary tables in Power BI. In DAX, you can't directly rename the columns you are grouping by from within the SUMMARIZE function itself. The SUMMARIZE function is designed to perform the grouping, and it will always return those grouping columns with their original, fully qualified names like 'Table'[Column].

     

    To assign custom names to both your grouped and aggregated columns, the standard and most effective pattern is to wrap your SUMMARIZE logic inside the SELECTCOLUMNS function. This function is designed specifically to construct a new table by picking columns from an existing table (or a table expression like the one SUMMARIZE creates), allowing you to rename them in the process.

     

    Here is an example of how you would create a table that summarizes sales by product category, renaming the category column to "Product Category" and creating a new "Total Sales" column. The SUMMARIZE function handles the grouping, and SELECTCOLUMNS handles the final column naming and structure.

    NewSummarizedTable =
    SELECTCOLUMNS(
        SUMMARIZE(
            'Sales',
            'Product'[Category]
        ),
        "Product Category", 'Product'[Category],
        "Total Sales", SUM(Sales[Sales Amount])
    )

    In this formula, SUMMARIZE first creates a temporary table containing just the unique values from the 'Product'[Category] column. Then, SELECTCOLUMNS takes that temporary table as its first argument and builds the final table. It creates a column named "Product Category" by referencing the original group-by column and adds a second column named "Total Sales" by providing the aggregation expression. This pattern is very powerful for creating clean, custom tables for your visuals.

     

    Best regards,