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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Quixote
Frequent Visitor

Filling empties in a dataset with column averages

I have a dataset where all the values are numbers minus some of them which are empty values.

 

I need to find how to fill those empties with the column averages using Get Data/Power Query.

 

Thanks,

PS: Sorry for my English

1 ACCEPTED SOLUTION
jahida
Impactful Individual
Impactful Individual

Your English is fine, don't worry about it. Here's the M code in two steps (you can plug this into the advanced editor or make a garbage step and change the code):

 

    #"Ave" = List.Average(#"Changed Type"[Numbers]),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Numbers] = null then #"Ave" else [Numbers]),

 

Then you can delete the initial column (in this case, [Numbers]). This should only calculate the average once.

View solution in original post

5 REPLIES 5
KGrice
Memorable Member
Memorable Member

Is there a need for this to be in the Get Data part, as opposed to after loading the data? This is probably much simpler as a calculated column with DAX.

 

For example, I created a list of random numbers, then loaded it into Power BI Desktop. I left the blanks alone in Get Data, then went to the Data pane to look at the table (not strictly necessary, but helps to visualize what's happening). From the Modeling tab in the ribbon, select New Column, and set the formula as

Column = IF(ISBLANK(TableName[Numbers]), AVERAGE(TableName[Numbers]), TableName[Numbers])

 

Here's what I end up with:

 

ReplaceBlanks.PNG

 

You can also round the average number, if you need it to be more like the other numbers in the column.

jahida
Impactful Individual
Impactful Individual

Original question asked for a solution in M, so although KGrice's solution will work, here's the M alternative:

 

Add a custom column using this code:

= if [Numbers] = null then List.Average(#"Added Custom"[Numbers]) else [Numbers]

 

Obviously you'll have to change the "Added Custom" and "Numbers" to the relevant step and column names. Additionally, this formula might be really slow. I'd imagine a decent query processor would be able to optimize to only do the Average calculation once but maybe not... I don't have a large dataset to test on. If it is really bad for efficiency, you can pull out the Average into another step. Let me know if you'd like M code for that.

Thank you very much Kgrice and jahida!

 

I prefer the M solution because I think is better in order to have a live connection with de source data.

 

And yes, I would like to have the M code for pull out the Average into another step. Thanks again jahida.

 

PS: Sorry for my English.

jahida
Impactful Individual
Impactful Individual

Your English is fine, don't worry about it. Here's the M code in two steps (you can plug this into the advanced editor or make a garbage step and change the code):

 

    #"Ave" = List.Average(#"Changed Type"[Numbers]),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Numbers] = null then #"Ave" else [Numbers]),

 

Then you can delete the initial column (in this case, [Numbers]). This should only calculate the average once.

Very nice. Thanks.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.