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
vivran22
Community Champion
Community Champion

Replacing null value with average of the column

Hi,

 

Looking for help in the following scenario:

 

I want to replace null value in the column with the average value of the column. How can I achieve this? I am interested in using conditional column or something of this sort.

 

Regards,

Vivek

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @vivran22 ,

 

 

let
    Source = YourTable,
    #"SetType" = Table.TransformColumnTypes(#"Source ",{{"Sample", type number}}),
    #"ReplaceAvg" = Table.ReplaceValue(#"SetType",null,List.Average(#"SetType"[Sample]),Replacer.ReplaceValue,{"Sample"})
in
    ReplaceAvg

Is this ok ? I assumed your initial table is YourTable and the column is named "Sample"

Tell me if there is any problem. 

 

 

Regards, 

Etienne

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

can you upload some sample data?  if not, basically:

  1. Right click the column where you will get the aveage from --> as new query
  2. That will give you a list, then under Transform select avearage
  3. Back in your main table, use the menu to replace nulls, with say 0 ( can be anything, doesnt matter)
  4. Then in the menu bar, change where it says 0, to name of list from #2

That should put the average where nulls used to be.

 

@Anonymous 

 

Thank you, Nick for your response. The challenge with the resposne you shared is that it is not dynamic. If in case, there are additional rows of data, then the average value of the column will change. The need is to make it dynamic, ie using Power Query/M I can calculate the average of the column and then replaces all the null values with it instead of calculating average manually.

 

Regards,

Vivek

Anonymous
Not applicable

sure, DAX and M can pretty much do anything within reason. Can you upload a sample pbix file?

@Anonymous 

 

I won't be able to share the exact data file, but following is what I am looking for:

 

This is the sample data:

Sample 1
1
2
3
4
null
5

 

The requirement is that the null should be replaced as 3 (average of the dataset)

 

and in case there are addition in the table then null should update accordingly:

 

Sample 2
1
2
3
4
null
5
6
7

 

In Sample 2, the null should be replaced as 4.

 

Hope this helps.

 

Rgds,

Vivek

Anonymous
Not applicable

Hi @vivran22 ,

 

 

let
    Source = YourTable,
    #"SetType" = Table.TransformColumnTypes(#"Source ",{{"Sample", type number}}),
    #"ReplaceAvg" = Table.ReplaceValue(#"SetType",null,List.Average(#"SetType"[Sample]),Replacer.ReplaceValue,{"Sample"})
in
    ReplaceAvg

Is this ok ? I assumed your initial table is YourTable and the column is named "Sample"

Tell me if there is any problem. 

 

 

Regards, 

Etienne

Hi,

 

How can I expand the script, to calculate the average by another column? So don't just want the average of the whole column, I want the average by another category (see below fruit type):

 

Fruit typeValueaverage by fruit type (no need this column, its just a representation)
apple1 
apple 2 
applenullexpected result: 1,5
orange3 
orange5 
orangenullexpected result: 4

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.

Top Solution Authors