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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
saeidrasti
Frequent Visitor

average and max of grouped sum by two columns when one column is a range

I have a dataset with 5 columns as follows. bucket range is from 1 to 96. bucket_start and bucket_end show the range of valid buckets for given row. For example, first row is reapeated for buckets 1,2, and 3.  First, i want to group data by two columns day and bucket and sum over values. Second, I want to calculate average and max of sums over buckets. I do not want to expand the table by reapeating rows for baucket ranges.

I would appreciate if you can help me to figure this out. 

 

unit_numdayvaluestart_bucketend_bucket
16/10/20231013
16/10/2023524
26/11/2023812
     
     
DaybucketSum of value  
6/10/2023110  
6/10/2023215  
6/10/2023315  
6/10/202345  
6/11/202318  
6/11/202328  
     
bucketaveragemax  
1910  
211.515  
31515  
455  
1 ACCEPTED SOLUTION
ManuelBolz
Responsive Resident
Responsive Resident

Hello @saeidrasti,

If my post helped you, please give me a 👍kudos and mark this post with Accept as Solution.

Replace the first "SOURCE STEP" with your Source Step:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTLTNzTQNzIwMgayDQ1ABBAbK8XqYEqbArEREJuAZY0gsoYwWQuoXiOl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [unit_num = _t, day = _t, value = _t, start_bucket = _t, end_bucket = _t]),
    Type = Table.TransformColumnTypes(Source,{{"value", Int64.Type}, {"unit_num", Int64.Type}, {"day", type date}, {"start_bucket", Int64.Type}, {"end_bucket", Int64.Type}}),

    BucketList = Table.AddColumn(Type, "BucketList", each List.Numbers([start_bucket], [end_bucket] - [start_bucket] + 1)),
    ExpandBucketList = Table.ExpandListColumn(BucketList, "BucketList"),
    Type2 = Table.TransformColumnTypes(ExpandBucketList,{{"BucketList", Int64.Type}}),
    GroupedBucketList = Table.Group(Type2, {"day", "BucketList"}, {{"Sum of value", each List.Sum([value]), type number}}),
    GroupedByBucket = Table.Group(GroupedBucketList, {"BucketList"}, {
        {"average", each List.Average([Sum of value]), type number},
        {"max", each List.Max([Sum of value]), type number}
    })
in
    GroupedByBucket



Best regards from Germany
Manuel Bolz


🟦Follow me on LinkedIn
🟨How to Get Your Question Answered Quickly
🟩Fabric Community Conference
🟪My Solutions on Github

View solution in original post

1 REPLY 1
ManuelBolz
Responsive Resident
Responsive Resident

Hello @saeidrasti,

If my post helped you, please give me a 👍kudos and mark this post with Accept as Solution.

Replace the first "SOURCE STEP" with your Source Step:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTLTNzTQNzIwMgayDQ1ABBAbK8XqYEqbArEREJuAZY0gsoYwWQuoXiOl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [unit_num = _t, day = _t, value = _t, start_bucket = _t, end_bucket = _t]),
    Type = Table.TransformColumnTypes(Source,{{"value", Int64.Type}, {"unit_num", Int64.Type}, {"day", type date}, {"start_bucket", Int64.Type}, {"end_bucket", Int64.Type}}),

    BucketList = Table.AddColumn(Type, "BucketList", each List.Numbers([start_bucket], [end_bucket] - [start_bucket] + 1)),
    ExpandBucketList = Table.ExpandListColumn(BucketList, "BucketList"),
    Type2 = Table.TransformColumnTypes(ExpandBucketList,{{"BucketList", Int64.Type}}),
    GroupedBucketList = Table.Group(Type2, {"day", "BucketList"}, {{"Sum of value", each List.Sum([value]), type number}}),
    GroupedByBucket = Table.Group(GroupedBucketList, {"BucketList"}, {
        {"average", each List.Average([Sum of value]), type number},
        {"max", each List.Max([Sum of value]), type number}
    })
in
    GroupedByBucket



Best regards from Germany
Manuel Bolz


🟦Follow me on LinkedIn
🟨How to Get Your Question Answered Quickly
🟩Fabric Community Conference
🟪My Solutions on Github

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.