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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Carlo1975
Helper I
Helper I

Matrix table with sum

Hi, I have a problem and I don't know how to overcome it. I don't know if the solution is Dax or power query. I'm sorry.

I try to explain.

 

I have a table like this. With 2 clusters columns, one column with the quarters, and one with an amount:

 

Cluster1Cluster2QuarterAmount
Buy01. > 5.0ml3                             150
Buy02. 2.5 - 5.0ml2                             250
Buy01. > 5.0ml1                             150
Buy01. > 5.0ml2                             150
Buy03. 1.0 - 2.5ml1                             330
Buy03. 1.0 - 2.5ml3                             330
Buy02. 2.5 - 5.0ml1                             140
Buy02. 2.5 - 5.0ml3                             140
Buy01. > 5.0ml3                             550
Buy03. 1.0 - 2.5ml2                             180
Sell02. 2.5 - 5.0ml2                             256
Sell03. 1.0 - 2.5ml2                             987
Sell01. > 5.0ml2                          6,321
Sell01. > 5.0ml1                          6,321
Sell02. 2.5 - 5.0ml1                          1,211
Sell03. 1.0 - 2.5ml1                          2,586
Sell01. > 5.0ml1                          1,211
Sell01. > 5.0ml3                          6,321
Sell02. 2.5 - 5.0ml3                          1,211
Sell03. 1.0 - 2.5ml3                          2,586

 

If I try to do a matrix table I don't have any problem

 

up to here everything is quite simple.

 

The problem is that, for each row, I have to sum the previous value with the following. And so, if you look at first row, you have for the first quarter 150, the second 150 and on the last one 700:

 

Cattura.JPG

 

In my new Matrix Table I have to do this: On the first quarter I will have 150, on the second quarter 300 (because I have to sum the 150 on the first quarter, to the second). In the third quarter, I will have 1000 because I will sum 150 (first quarter), 150 (second quarter) and 700 (last one). The new first row will be:

 

Cattura2.JPG

 

This one, for all the row and all the quarters.

 

thank you.

 

1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

Hi, @Carlo1975 , you might want to use this measure,

Accum = 
VAR __qtr = MAX ( Table1[Quarter] )
RETURN
    CALCULATE ( SUM ( Table1[Amount] ), Table1[Quarter] <= __qtr )

Screenshot 2020-11-27 202216.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

2 REPLIES 2
AlB
Community Champion
Community Champion

Hi @Carlo1975 

Certainly best to do this in DAX. If you want it in PQ, place the following M code in a blank query to see the steps:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("pdIxDsMgDAXQqyBmatkmULrmCh3TjNnomKG3L1KrSATJhjYSS4an728vi533l3UWCcxjR/SbCYDPXH758oz0UUC7ukNgMAzBXA6ANYBroI1AgxFaQc1wEjwYAixDlFH6IngvA2qNJ6CpUS9hkgF9kZPSoioEpUV9DekD3Lecfz2mWAnjGW7pWgmj1xSdZ1IEeZmtMHgN5JhIrkEG2IUU/5qhjTB6Tx0tyEBHCzLwbWF9Aw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Cluster1 = _t, Cluster2 = _t, Quarter = _t, Amount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cluster1", type text}, {"Cluster2", type text}, {"Quarter", Int64.Type}, {"Amount", type number}}),

    #"Grouped Rows" = Table.Group(#"Changed Type", {"Cluster1", "Cluster2", "Quarter"}, {{"Amount", each List.Sum([Amount]), type nullable number}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each List.Sum(Table.SelectRows(#"Grouped Rows", (inner)=>Record.SelectFields(_, {"Cluster1", "Cluster2"}) = Record.SelectFields(inner, {"Cluster1", "Cluster2"}) and (inner)[Quarter]<=[Quarter])[Amount])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Amount"}),
    #"Sorted Rows" = Table.Sort(#"Removed Columns",{{"Cluster1", Order.Ascending}, {"Cluster2", Order.Ascending}, {"Quarter", Order.Ascending}}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Sorted Rows", {{"Quarter", type text}}), List.Distinct(Table.TransformColumnTypes(#"Sorted Rows", {{"Quarter", type text}})[Quarter]), "Quarter", "Custom", List.Sum)
in
    #"Pivoted Column"

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

CNENFRNL
Community Champion
Community Champion

Hi, @Carlo1975 , you might want to use this measure,

Accum = 
VAR __qtr = MAX ( Table1[Quarter] )
RETURN
    CALCULATE ( SUM ( Table1[Amount] ), Table1[Quarter] <= __qtr )

Screenshot 2020-11-27 202216.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

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.