Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Power Query
Unanswered

How to fill null values in column with median value for each category?

(0) ShareShare
ReportReport
Posted on by

I have this table (there columns I need):

 

Screenshot 2024-03-28 123848.png

 

I need to replace null values in original_eur column with median value for each group (group columns are event_id and category).

 

let
Source = Csv.Document(File.Contents("E:\Kwork\Copy_Ticombo_parse_data.csv"),[Delimiter=";", Columns=40, Encoding=1251, QuoteStyle=QuoteStyle.None]),


#"Upper heads" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),


#"Changed types" = Table.TransformColumnTypes(#"Upper heads",{{"Name", type text}, {"id", type text}, {"entity_id", type text}, {"competition_name", type text}, ... etc }),


#"Add column with median values" = Table.AddColumn(#"Changed types", "median_values", each Table.SelectRows(
Table.Group(
#"Changed types",
{"event_id", "category"},
{"median", each List.Median(_[original_eur]), Int64.Type}
),
each _ = [event_id = "event_id", category = "category"]
))
in
#"Add column with median values"

 

At last step:

1) I make grouped table with medians;

At this stage all works.

But how select right value for each row?

 

 

 

  • JR-20121253-0 Profile Picture
    on at
    Re: How to fill null values in column with median value for each category?

    Please try the below:

    let
    Source = Csv.Document(File.Contents("E:\Kwork\Copy_Ticombo_parse_data.csv"),[Delimiter=";", Columns=40, Encoding=1251, QuoteStyle=QuoteStyle.None]),
    #"Upper heads" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Changed types" = Table.TransformColumnTypes(#"Upper heads",{{"Name", type text}, {"id", type text}, {"entity_id", type text}, {"competition_name", type text}, ... etc }),
    #"Grouped for Median" = Table.Group(#"Changed types", {"event_id", "category"}, {"median", each List.Median([original_eur]), type nullable number}),
    #"Joined with Median" = Table.Join(#"Changed types", {"event_id", "category"}, #"Grouped for Median", {"event_id", "category"}, JoinKind.LeftOuter),
    #"Replaced Nulls" = Table.TransformColumns(#"Joined with Median", {"original_eur", each if _ = null then [median] else _, type nullable number}),
    #"Removed Median Column" = Table.RemoveColumns(#"Replaced Nulls",{"median"})
    in
    #"Removed Median Column"

    Group for Median: First, create a table grouped by event_id and category with the median of original_eur calculated for each group.

    Join with Median: Then, perform a left outer join of the original table with the median table on event_id and category. This step merges the median value into each corresponding row of the original dataset.

    Replace Nulls: After the join, use Table.TransformColumns to replace null values in the original_eur column with the corresponding median value.

    Remove Median Column: Finally, remove the temporary median column as it's no longer needed after replacing the null values.

     

    Thanks,

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Leaderboard > Power Apps - Power Query

#1
mmbr1606 Profile Picture

mmbr1606 9 Super User 2025 Season 1

#2
stampcoin Profile Picture

stampcoin 7

#3
SD-13050734-0 Profile Picture

SD-13050734-0 6

Overall leaderboard

Featured topics