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
DominicBrien
Frequent Visitor

Aggregate in same table

Hello, I am trying to do aggregation in the same table...

Here is what I am looking for....

Sample.data.PNG

Basically I want the SUM(Value) Group by CustomerID.

The capture above came from my work around an example I found in here.

My code looks like this..

let
Source = Table.FromRecords({[CustomerID = 1, Name = "Bob", Value = 10],[CustomerID = 1, Name = "Bob", Value = 11],[CustomerID = 3, Name = "Paul", Value = 25],[CustomerID = 4, Name = "Ringo", Value = 42]}),
#"AddingIdx" = Table.AddIndexColumn(Source,"Index"),
#"AddingTot" = Table.AddColumn(#"AddingIdx", "Total", each Table.Range(#"AddingIdx",0,[Index]+1)),
#"Aggr" = Table.AggregateTableColumn(#"AddingTot", "Total", {{"Value", List.Sum, "cSales"}}),
#"Select" = Table.AddColumn(#"Aggr", "SubTotal", each Table.SelectRows(#"Aggr", each ([CustomerID]=1))),
#"Aggr2" = Table.AggregateTableColumn(#"Select", "SubTotal", {{"Value", List.Sum, "cSales2"}})
in
#"Aggr2"

I have been struggling around the syntax to replace my red hardcoded "1" by the current rows CustomerID... Can anyone help or am I workin on an unachievable misson?

 

Thanks in advance 🙂

1 ACCEPTED SOLUTION

Hi @DominicBrien,

 

=> I am working toward building a summarized visualisation that would translate in the context of the provided example to.

 

Did you mean that you want to create a new calculated table with SUMMARIZED() function based on the original table?

 

Maybe something like:

 

Table =
SUMMARIZE (
    'TableName',
    "Between 0 and 10", CALCULATE (
        COUNT ( 'TableName'[CustomerID] ),
        FILTER ( 'TableName', 'TableName'[Value] <= 10 )
    ),
    "Between 10 and 20", CALCULATE (
        COUNT ( 'TableName'[CustomerID] ),
        FILTER ( 'TableName', 'TableName'[Value] > 10 && 'TableName'[Value] <= 20 )
    )
)

Thanks,
Xi Jin.

View solution in original post

6 REPLIES 6
v-xjiin-msft
Solution Sage
Solution Sage

Hi @DominicBrien,

 

You can achieve this with simple DAX expressions:

 

Calculated Column:

 

SUM Value Column =
CALCULATE (
    SUM ( SampleTable[Value] ),
    FILTER (
        SampleTable,
        SampleTable[CustomerID] = EARLIER ( SampleTable[CustomerID] )
    )
)

Measure:

 

SUM Value Measure =
CALCULATE (
    SUM ( SampleTable[Value] ),
    ALLEXCEPT ( SampleTable, SampleTable[CustomerID] )
)

6.PNG

 

Thanks,
Xi Jin.

Thanks, I did have it at the DAX level before I am now working on a new model where again I was trying to add the aggregation at the Report level.

Because I build the model using Excel I cannot add DAX column at the report level.

The measure works whenb displaying a list that has the one item per "Customer"...

The problem I get working with DAX measures is that the the end result also involves another level of aggregation that is not at the customer level.

I am working toward building a summarized visualisation that would translate in the context of the provided example to (values in the original sample wouls not result in this out but I hope you can understand where I am tryinng to go).

Number of customers

Total value range

3

Between 0 and 10

1

Between 10 and 20

Hi @DominicBrien,

 

=> I am working toward building a summarized visualisation that would translate in the context of the provided example to.

 

Did you mean that you want to create a new calculated table with SUMMARIZED() function based on the original table?

 

Maybe something like:

 

Table =
SUMMARIZE (
    'TableName',
    "Between 0 and 10", CALCULATE (
        COUNT ( 'TableName'[CustomerID] ),
        FILTER ( 'TableName', 'TableName'[Value] <= 10 )
    ),
    "Between 10 and 20", CALCULATE (
        COUNT ( 'TableName'[CustomerID] ),
        FILTER ( 'TableName', 'TableName'[Value] > 10 && 'TableName'[Value] <= 20 )
    )
)

Thanks,
Xi Jin.

Thanks, I changed my model quite a bit and was able to produce the result I wanted. Now I am struggeling with a new issue so It's probably better to start a new thread....

MarkS
Resolver IV
Resolver IV

Hi @DominicBrien,

Use the Group By function and the advanced functions,

Group By - CustomerID

Aggregations - Sum of Value Column

                      -  ALL Rows

 

Then expand the Table

 

Here if the M code appended to the end of your code 

let
Source = Table.FromRecords({[CustomerID = 1, Name = "Bob", Value = 10],[CustomerID = 1, Name = "Bob", Value = 11],[CustomerID = 3, Name = "Paul", Value = 25],[CustomerID = 4, Name = "Ringo", Value = 42]}),
#"AddingIdx" = Table.AddIndexColumn(Source,"Index"),
#"AddingTot" = Table.AddColumn(#"AddingIdx", "Total", each Table.Range(#"AddingIdx",0,[Index]+1)),
#"Aggr" = Table.AggregateTableColumn(#"AddingTot", "Total", {{"Value", List.Sum, "cSales"}}),
#"Select" = Table.AddColumn(#"Aggr", "SubTotal", each Table.SelectRows(#"Aggr", each ([CustomerID]=1))),
#"Aggr2" = Table.AggregateTableColumn(#"Select", "SubTotal", {{"Value", List.Sum, "cSales2"}}),
    #"Grouped Rows" = Table.Group(Aggr2, {"CustomerID"}, {{"GroupTotal", each List.Sum([Value]), type number}, {"GroupedTable", each _, type table}}),
    #"Expanded GroupedTable" = Table.ExpandTableColumn(#"Grouped Rows", "GroupedTable", {"Name", "Value", "Index", "cSales", "cSales2"}, {"Name", "Value", "Index", "cSales", "cSales2"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded GroupedTable",{"CustomerID", "Name", "Value", "Index", "cSales", "cSales2", "GroupTotal"})
in
#"Reordered Columns"
Ashish_Mathur
Super User
Super User

Hi,

 

This can be donw quite easily with DAX (you are trying to do it with M).  If you are OK with a DAX solution, post back.  Also, do you want a calculated column solution or a measure solution?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

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.