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

Geometric Mean for Grouped Data

Hello Team,

 

Could you please help me with this I want to get the GeoMean for below example. I have groped data and I want to creat a cutom column that calculate a the Geometric Mean for the Duration. I want to creat the column inside the power query not do it AS MEASURE or COLUMN on dax. below table showning the data that are grouped by and I calculate the Geomean on excel so How I can do it in the same way in the Power Query any idea ? or Formula it can help me please?

 

DirectionJob TypeConveyanceField NameBaseDurationGeo Mean
UPOHWirelineAAAOIL1.53.170662853
UPOHWirelineAAAOIL2.53.170662853
UPOHWirelineAAAOIL8.53.170662853
DOWNVSPDrillpipeBBBWater1.54.181723304
DOWNVSPDrillpipeBBBWater6.54.181723304
DOWNVSPDrillpipeBBBWater7.54.181723304
UPFTSWirelineCCCOIL2.53.708099244
UPFTSWirelineCCCOIL5.53.708099244

 

Thank you in Advance for helping

1 ACCEPTED SOLUTION
ImkeF
Community Champion
Community Champion

Hi @WaiamMalibari ,
I would recommend to do a grouping using the UI with a dummy aggregation and then replace it with the formula for the geometric mean: 

Number.Power(List.Product([Duration]), 1 / List.Count([Duration]))

Please paste the following code into the advanced editor and follow the steps:

let
    Source = Table.FromRows(
        Json.Document(
            Binary.Decompress(
                Binary.FromText(
                    "i45WCg1Q0lHy9wAS4ZlFqTmZealApqOjI0jU0wdIGuqZKsXqEKHQiFiFFlCFLv7hfkBuWDBIg0tRZk5OQWYBSLGTkxNId2JJahGSA4hUbkaacnNkV7uFBKM629nZGbv/8Ko0BamMBQA=",
                    BinaryEncoding.Base64
                ),
                Compression.Deflate
            )
        ),
        let
            _t = ((type nullable text) meta [Serialized.Text = true])
        in
            type table [
                Direction = _t,
                #"Job Type" = _t,
                Conveyance = _t,
                #"Field Name" = _t,
                Base = _t,
                Duration = _t
            ]
    ),
    #"Changed Type" = Table.TransformColumnTypes(
        Source,
        {
            {"Direction", type text},
            {"Job Type", type text},
            {"Conveyance", type text},
            {"Field Name", type text},
            {"Base", type text},
            {"Duration", type number}
        }
    ),
    #"Grouped Rows" = Table.Group(
        #"Changed Type",
        {"Direction", "Job Type", "Conveyance", "Field Name", "Base"},
        {
            {
                "Geo Mean",
                each Number.Power(List.Product([Duration]), 1 / List.Count([Duration])),
                type nullable number
            },
            {"Partition", each _}
        }
    ),
    #"Expanded Partition" = Table.ExpandTableColumn(
        #"Grouped Rows",
        "Partition",
        {"Duration"},
        {"Duration"}
    )
in
    #"Expanded Partition"​



Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

1 REPLY 1
ImkeF
Community Champion
Community Champion

Hi @WaiamMalibari ,
I would recommend to do a grouping using the UI with a dummy aggregation and then replace it with the formula for the geometric mean: 

Number.Power(List.Product([Duration]), 1 / List.Count([Duration]))

Please paste the following code into the advanced editor and follow the steps:

let
    Source = Table.FromRows(
        Json.Document(
            Binary.Decompress(
                Binary.FromText(
                    "i45WCg1Q0lHy9wAS4ZlFqTmZealApqOjI0jU0wdIGuqZKsXqEKHQiFiFFlCFLv7hfkBuWDBIg0tRZk5OQWYBSLGTkxNId2JJahGSA4hUbkaacnNkV7uFBKM629nZGbv/8Ko0BamMBQA=",
                    BinaryEncoding.Base64
                ),
                Compression.Deflate
            )
        ),
        let
            _t = ((type nullable text) meta [Serialized.Text = true])
        in
            type table [
                Direction = _t,
                #"Job Type" = _t,
                Conveyance = _t,
                #"Field Name" = _t,
                Base = _t,
                Duration = _t
            ]
    ),
    #"Changed Type" = Table.TransformColumnTypes(
        Source,
        {
            {"Direction", type text},
            {"Job Type", type text},
            {"Conveyance", type text},
            {"Field Name", type text},
            {"Base", type text},
            {"Duration", type number}
        }
    ),
    #"Grouped Rows" = Table.Group(
        #"Changed Type",
        {"Direction", "Job Type", "Conveyance", "Field Name", "Base"},
        {
            {
                "Geo Mean",
                each Number.Power(List.Product([Duration]), 1 / List.Count([Duration])),
                type nullable number
            },
            {"Partition", each _}
        }
    ),
    #"Expanded Partition" = Table.ExpandTableColumn(
        #"Grouped Rows",
        "Partition",
        {"Duration"},
        {"Duration"}
    )
in
    #"Expanded Partition"​



Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

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.