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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
MakZH
Frequent Visitor

Add a "Total" value in a table with product categories

Dear community

I am looking for a way to get a table with the total sales of the product categories and a "Total" row with the sum of all the total sales of each product categories (see the prefered outcome below > Table 1)

MakZH_0-1689067045371.png

Table 1: Prefered outcome

Currently the table contains only the product categories (see the screenshot below > Table 2)

MakZH_1-1689067307685.png

Table 2: Current table

To calculate the total I intend to create a new table with the following DAX script, which generates unfortunately not the expected outcome:

CategoriesWithTotals = 
--This script creates a table with the product categories from the Product Category
VAR _ProductCategories = 
DISTINCT(
    UNION(
        SUMMARIZE('Test Data (3)', 'Test Data (3)'[Product Category]),
        DATATABLE("Product Category", STRING, {{"Total"}} --Create a table with the data type "STRING"
        )
    )
)

VAR _TotalSum =
    SUMX('Test Data (3)',[Total])

VAR _SetTotal =
SWITCH(
    TRUE(),
    _ProductCategories <> "Total", 123,
    _ProductCategories = "Total", SUMX('Test Data (3)',[Total]),
    BLANK()
)

Return
ADDCOLUMNS(
    _ProductCategories,
    "Total", _TotalSum
)

 Thank you for your support. 

2 ACCEPTED SOLUTIONS
v-cgao-msft
Community Support
Community Support

Hi @MakZH ,

 

Please try this:

Table2 = 
VAR _ProductCategories =
    UNION (
        SELECTCOLUMNS ( 'Table1', "Product Category", 'Table1'[Product Category] ),
        ROW ( "Product Category", "Total" )
    )
RETURN
    ADDCOLUMNS (
        _ProductCategories,
        "Sum Product Category",
        IF (
            [Product Category] = "Total",
            SUM ( 'Table1'[Sum Product Category] ),
            CALCULATE ( SUM ( 'Table1'[Sum Product Category] ), 'Table1'[Product Category] = EARLIER ( [Product Category] ) )
        )
    )

vcgaomsft_0-1689216405185.png

 

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

View solution in original post

Thank you Gao, this works perfectly 🙂

I also had a similar approach, first to create a calculated table with the following script

CategoriesWithTotals = 
--This script creates a table with the product categories from the Product Category
DISTINCT(
    UNION(
        SUMMARIZE('Test Data (3)', 'Test Data (3)'[Product Category]),
        DATATABLE("Product Category", STRING, {{"Total"}} --Create a table with the data type "STRING"
        )
    )
) 

 
and then create a measure for the totals

Average Selling Price incl Total = 

Var Total =
CALCULATE(SUMX('Test Data (3)', [Total]), ALL(CategoriesWithTotals[Product Category]))

RETURN
IF (
    SELECTEDVALUE(CategoriesWithTotals[Product Category])="Total",
    Total,
    SUMX('Test Data (3)', [Total])
)

View solution in original post

2 REPLIES 2
v-cgao-msft
Community Support
Community Support

Hi @MakZH ,

 

Please try this:

Table2 = 
VAR _ProductCategories =
    UNION (
        SELECTCOLUMNS ( 'Table1', "Product Category", 'Table1'[Product Category] ),
        ROW ( "Product Category", "Total" )
    )
RETURN
    ADDCOLUMNS (
        _ProductCategories,
        "Sum Product Category",
        IF (
            [Product Category] = "Total",
            SUM ( 'Table1'[Sum Product Category] ),
            CALCULATE ( SUM ( 'Table1'[Sum Product Category] ), 'Table1'[Product Category] = EARLIER ( [Product Category] ) )
        )
    )

vcgaomsft_0-1689216405185.png

 

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

Thank you Gao, this works perfectly 🙂

I also had a similar approach, first to create a calculated table with the following script

CategoriesWithTotals = 
--This script creates a table with the product categories from the Product Category
DISTINCT(
    UNION(
        SUMMARIZE('Test Data (3)', 'Test Data (3)'[Product Category]),
        DATATABLE("Product Category", STRING, {{"Total"}} --Create a table with the data type "STRING"
        )
    )
) 

 
and then create a measure for the totals

Average Selling Price incl Total = 

Var Total =
CALCULATE(SUMX('Test Data (3)', [Total]), ALL(CategoriesWithTotals[Product Category]))

RETURN
IF (
    SELECTEDVALUE(CategoriesWithTotals[Product Category])="Total",
    Total,
    SUMX('Test Data (3)', [Total])
)

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors